package com.xiexj.ebook;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Shader;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
public class SinglePage extends View {
private boolean isLeftPage = false;
private int pageNo;
private int x;
private int y;
private int width;
private int height;
private Paint paint;
private int dis = 10;
//当前选中的角
private int chooseCorner = -1;
public static final int LEFT_UP_CORNER = 0;
public static final int LEFT_BOTTOM_CORNER = 1;
public static final int RIGHT_UP_CORNER = 2;
public static final int RIGHT_BOTTOM_CORNER = 3;
//A点的坐标是手指目前的坐标
private float aX = -1;
private float aY = -1;
//B点的坐标是A点翻起后跟下一页的交叉点
private float bX = -1;
private float bY = -1;
//C点的坐标是D点翻起后跟下一页的交叉点,可能与D点重复
private float cX = -1;
private float cY = -1;
//D点的坐标是本页的另一个角的点的坐标
private float dX = -1;
private float dY = -1;
//旋转的角度
private float angle = 0;
//翻动页背面的阴影角度
private float shadowAngle = 0;
//阴影长度
private float shadowLength = 0;
//阴影的坐标x
private float sdx = 0;
//阴影的坐标y
private float sdy = 0;
//是否隐藏层
private boolean isMask;
//是否是当前显示页
private boolean isLookPage;
//隐藏层的显示区域
private Path maskPath;
//翻动页的fx坐标
private float fx;
//翻动页的fy坐标
private float fy;
//背景色
private int bc;
//显示的角
private int cr;
private BookLayout blo;
private PageContent content;
public SinglePage(Context context,boolean isLeftPage,int pageNo,BookLayout blo,PageContent content) {
super(context);
this.isLeftPage = isLeftPage;
this.pageNo = pageNo;
this.blo = blo;
this.content = content;
paint = new Paint();
bc = Color.WHITE;
paint.setARGB(255, Color.red(bc), Color.green(bc), Color.blue(bc));
paint.setAntiAlias(true);
isMask = true;
isLookPage = false;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//抗锯齿
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
if(isMask){//如果是遮罩层
// if(blo.getCurrentFlipPage()!=null&&this==blo.getMaskPage(blo.getCurrentFlipPage())){
// Log.e("SinglePage", "------->pageNo:"+pageNo);
// }
if(maskPath==null) return;
float tx = 0;
float ty = 0;
switch(cr){
case LEFT_UP_CORNER:{
tx = fx;
ty = fy;
break;
}
case RIGHT_UP_CORNER:{
tx = fx-width;
ty = fy;
break;
}
case LEFT_BOTTOM_CORNER:{
tx = fx;
ty = fy-height;
break;
}
case RIGHT_BOTTOM_CORNER:{
tx = fx-width;
ty = fy-height;
break;
}
}
canvas.clipPath(maskPath);
canvas.save();
canvas.rotate(angle, fx, fy);
paint.setStyle(Style.FILL);
paint.setARGB(255, Color.red(bc), Color.green(bc), Color.blue(bc));
canvas.drawRect(tx,ty,tx+width,ty+height, paint);
content.setPosition(tx, ty);
content.draw(canvas);
paint.setStyle(Style.STROKE);
paint.setARGB(255,0,0,0);
canvas.drawRect(tx,ty,tx+width,ty+height, paint);
if(content!=null&&content.isContentPage()){
if(isLeftPage){
canvas.drawText("第"+(pageNo-1)+"页", tx+5, ty+height-5, paint);
}else{
canvas.drawText("第"+(pageNo-1)+"页", tx+width-5-paint.measureText("第"+(pageNo-1)+"页"), ty+height-5, paint);
}
}
canvas.restore();
canvas.save();
canvas.rotate(shadowAngle, sdx, sdy);
paint.setStyle(Style.FILL);
shadowLength = (float)Math.sqrt(width*width+height*height);
if(isLeftPage){
Shader linearShader = new LinearGradient(sdx-50,sdy,sdx,sdy,Color.argb(0, 0, 0, 0),Color.argb(33, 0, 0, 0),TileMode.CLAMP);
paint.setShader(linearShader);
canvas.drawRect(sdx-50, sdy, sdx, sdy+shadowLength, paint);
}else{
Shader linearShader = new LinearGradient(sdx,sdy,sdx+50,sdy,Color.argb(33, 0, 0, 0),Color.argb(0, 0, 0, 0),TileMode.CLAMP);
paint.setShader(linearShader);
canvas.drawRect(sdx, sdy, sdx+50, sdy+shadowLength, paint);
}
paint.setShader(null);
canvas.restore();
}else{//显示层
Path backPath = null;
if(aX!=-1&&aY!=-1){
backPath = new Path();
backPath.moveTo(aX, aY);
backPath.lineTo(bX, bY);
backPath.lineTo(cX, cY);
if(cX!=dX||cY!=dY){
backPath.lineTo(dX, dY);
}
backPath.lineTo(aX, aY);
float px = -1;
float py = -1;
float ox = -1;
float oy = -1;
float ex = -1;
float ey = -1;
switch(chooseCorner){
case RIGHT_UP_CORNER:{
px = x;
py = y;
ox = x;
oy = y+height;
ex = x+width;
ey = y+height;
break;
}
case RIGHT_BOTTOM_CORNER:{
px = x;
py = y+height;
ox = x;
oy = y;
ex = x+width;
ey = y;
break;
}
case LEFT_UP_CORNER:{
px = width+x;
py = y;
ox = width+x;
oy = height+y;
ex = x;
ey = y+height;
break;
}
case LEFT_BOTTOM_CORNER:{
px = width+x;
py = height+y;
ox = width+x;
oy = y;
ex = x;
ey = y;
break;
}
}
maskPath = new Path();
maskPath.moveTo(bX, bY);
maskPath.lineTo(px, py);
maskPath.lineTo(ox, oy);
if(cX==dX&&cY==dY){
maskPath.lineTo(ex, ey);
}
maskPath.lineTo(cX, cY);
maskPath.lineTo(bX, bY);
}
if(maskPath!=null){
canvas.clipPath(maskPath);
canvas.save();
}
paint.setStyle(Style.FILL);
paint.setARGB(255, Color.red(bc), Color.green(bc), Color.blue(bc));
canvas.drawRect(x,y,x+width,y+height, paint);
paint.setARGB(255,0,0,0);
paint.setStyle(Style.STROKE);
content.setPosition(x, y);
canvas.drawRect(x,y,x+width,y+height, paint);
content.draw(canvas);
if(content!=null&&content.isContentPage()){
if(isLeftPage){
canvas.drawText("第"+(pageNo-1)+"页", x+5, y+height-5, paint);
}else{
canvas.drawText("第"+(pageNo-1)+"页", x+width-5-paint.measureText("第"+(pageNo-1)+"页"), y+height-5, paint);
}
}
//显示书轴阴影
paint.setStyle(Style.FILL);
if(!blo.isFirstOrLastPage(this)){
if(isLeftPage){
Shader linearShader = new LinearGradient(x+width-50,y,x+width,y,Color.argb(0, 0, 0, 0),Color.argb(33, 0, 0, 0),TileMode.CLAMP);
paint.setShader(linearShader);
canvas.drawRect(x+width-50, y, x+width, y+height, paint);
}else{
Shader linearShader = new LinearGradient(x,y,x+50,y,Color.argb(33, 0, 0, 0),Color.argb(0, 0, 0, 0),TileMode.CLAMP);
paint.setShader(linearShader);
canvas.drawRect(x, y, x+50, y+height, paint);
}
paint.setShader(null);
}
if(maskPath!=null){
canvas.restore();
}
if(backPath!=null){
float sdx = -1;
float sdy = -1;
if(bY>cY){
sdy = cY;
sdx = cX;
}else{
sdy = bY;
sdx = bX;
}
blo.flipPage(this, backPath,angle,shadowAngle,aX,aY,sdx,sdy,chooseCorner);
}
}
}
/**
* 显示反页遮罩层的区域
* @param path
* @param angle
* @param x
* @param y
*/
public void onMaskPathDraw(Path path,float angle,float shadowAngle,float x,float y,float sdx,float sdy,int chooseCorner){
this.maskPath = path;
this.angle = angle;
this.shadowAngle = shadowAngle;
this.fx = x;
this.fy = y;
this.sdx = sdx;
this.sdy = sdy;
switch(chooseCorner){
case LEFT_UP_CORNER:{
cr = RIGHT_UP_CORNER;
break;
}