package com.xin.book.view;
import java.util.Date;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Shader;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
public class BookLayout extends FrameLayout
{
public static final String LOG_TAG = "liaowenxin";
private int totalPageNum;
private Context mContext;
private boolean hasInit = false;
private final int defaultWidth = 600, defaultHeight = 400;
private int contentWidth = 0;
private int contentHeight = 0;
private View currentPage, middlePage, nextPage, prevPage;
private LinearLayout invisibleLayout;
private LinearLayout mainLayout;
private BookView mBookView;
private Handler aniEndHandle;
private static boolean closeBook = false;
private Corner mSelectCorner;
private final int clickCornerLen = 250 * 250; // 50dip
private float scrollX = 0, scrollY = 0;
private int indexPage = 0;
private BookState mState;
private Point aniStartPos;
private Point aniStopPos;
private Date aniStartTime;
private long aniTime = 800;
private long timeOffset = 10;
// private Listener mListener;
private BookAdapter mPageAdapter;
private GestureDetector mGestureDetector;
private BookOnGestureListener mGestureListener;
public BookLayout(Context context)
{
super(context);
Init(context);
}
public BookLayout(Context context, AttributeSet attrs)
{
super(context,attrs);
Init(context);
}
public BookLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context,attrs,defStyle);
Init(context);
}
public void setPageAdapter(BookAdapter pa)
{
Log.d(LOG_TAG, "setPageAdapter");
mPageAdapter = pa;
}
public void Init(Context context)
{
Log.d(LOG_TAG, "Init");
totalPageNum = 0;
mContext = context;
mSelectCorner = Corner.None;
mGestureListener = new BookOnGestureListener();
mGestureDetector = new GestureDetector(mGestureListener);
mGestureDetector.setIsLongpressEnabled(false);
aniEndHandle = new Handler();
this.setOnTouchListener(touchListener);
this.setLongClickable(true);
}
protected void dispatchDraw(Canvas canvas)
{
Log.d(LOG_TAG, "dispatchDraw");
super.dispatchDraw(canvas);
if (!hasInit)
{
hasInit = true;
indexPage = 0;
if (mPageAdapter == null)
{
throw new RuntimeException("please set the PageAdapter on init");
}
totalPageNum = mPageAdapter.getCount();
mainLayout = new LinearLayout(mContext);
mainLayout.setLayoutParams(new LayoutParams(contentWidth, contentHeight));
mainLayout.setBackgroundColor(0xffffffff);
mState = BookState.READY;
invisibleLayout = new LinearLayout(mContext);
invisibleLayout.setLayoutParams(new LayoutParams(contentWidth, contentHeight));
this.addView(invisibleLayout);
this.addView(mainLayout);
mBookView = new BookView(mContext);
mBookView.setLayoutParams(new LayoutParams(contentWidth, contentHeight));
this.addView(mBookView);
updatePageView();
invalidate();
}
else if (mState == BookState.READY)
{
mBookView.update();
}
}
/**
* 此方法做过修改,改成一页显示
*/
public void updatePageView()
{
Log.d(LOG_TAG, "updatePageView");
if (indexPage < 0 || indexPage > totalPageNum - 1)
{
return;
}
invisibleLayout.removeAllViews();
mainLayout.removeAllViews();
/* 当前页 */
currentPage = mPageAdapter.getView(indexPage);
if (currentPage == null)
{
currentPage = new WhiteView(mContext);
}
currentPage.setLayoutParams(new LayoutParams(contentWidth, contentHeight));
mainLayout.addView(currentPage);
/* 背景页 */
middlePage = new WhiteView(mContext);
middlePage.setLayoutParams(new LayoutParams(contentWidth, contentHeight));
invisibleLayout.addView(middlePage);
/* 前一页 */
prevPage = null;
if (indexPage > 0)
{
prevPage = mPageAdapter.getView(indexPage - 1);
}
if (prevPage == null)
{
prevPage = new WhiteView(mContext);
}
prevPage.setLayoutParams(new LayoutParams(contentWidth, contentHeight));
invisibleLayout.addView(prevPage);
/* 后一页 */
nextPage = null;
if (indexPage < totalPageNum - 1)
{
nextPage = mPageAdapter.getView(indexPage + 1);
}
if (nextPage == null)
{
nextPage = new WhiteView(mContext);
}
nextPage.setLayoutParams(new LayoutParams(contentWidth, contentHeight));
invisibleLayout.addView(nextPage);
Log.d(LOG_TAG, "updatePageView finish");
}
OnTouchListener touchListener = new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
Log.d(LOG_TAG, "onTouch " + " x: " + event.getX() + " y: " + event.getY() + " mState:" + mState);
mGestureDetector.onTouchEvent(event);
int action = event.getAction();
if (action == MotionEvent.ACTION_UP && mSelectCorner != Corner.None
&& mState == BookState.TRACKING)
{
if (mState == BookState.ANIMATING)
return false;
if (mSelectCorner == Corner.LeftTop)
{
if (scrollX < contentWidth / 2)
{
aniStopPos = new Point(0, 0);
}
else
{
aniStopPos = new Point(2 * contentWidth, 0);
}
}
else if (mSelectCorner == Corner.RightTop)
{
if (scrollX < contentWidth / 2)
{
aniStopPos = new Point(-contentWidth, 0);
}
else
{
aniStopPos = new Point(contentWidth, 0);
}
}
else if (mSelectCorner == Corner.LeftBottom)
{
if (scrollX < contentWidth / 2)
{
aniStopPos = new Point(0, contentHeight);
}
else
{
aniStopPos = new Point(2 * contentWidth, contentHeight);
}
}
else if (mSelectCorner == Corner.RightBottom)
{
if (scrollX < contentWidth / 2)
{
aniStopPos = new Point(-contentWidth, contentHeight);
}
else