package com.anlia.pageturn.view; import android.annotation.TargetApi; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Region; import android.graphics.drawable.GradientDrawable; import android.os.Trace; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.Scroller; import com.anlia.pageturn.R; import com.anlia.pageturn.bean.MyPoint; /** * Created by anlia on 2017/10/19. */ public class BookPageView extends View { private Paint pointPaint;//绘制各标识点的画笔 private Paint bgPaint;//背景画笔 private Paint pathAPaint;//绘制A区域画笔 private Paint pathBPaint;//绘制B区域画笔 private Paint pathCPaint;//绘制C区域画笔 private Paint textPaint;//绘制文字画笔 private Paint pathCContentPaint;//绘制C区域内容画笔 private MyPoint a,f,g,e,h,c,j,b,k,d,i; private Path pathA; private Path pathB; private Path pathC; private int defaultWidth;//默认宽度 private int defaultHeight;//默认高度 private int viewWidth; private int viewHeight; float lPathAShadowDis = 0;//A区域左阴影矩形短边长度参考值 float rPathAShadowDis = 0;//A区域右阴影矩形短边长度参考值 private float[] mMatrixArray = { 0, 0, 0, 0, 0, 0, 0, 0, 1.0f }; private Matrix mMatrix; private Scroller mScroller; private String style; public static final String STYLE_LEFT = "STYLE_LEFT";//点击左边区域 public static final String STYLE_RIGHT = "STYLE_RIGHT";//点击右边区域 public static final String STYLE_MIDDLE = "STYLE_MIDDLE";//点击中间区域 public static final String STYLE_TOP_RIGHT = "STYLE_TOP_RIGHT";//f点在右上角 public static final String STYLE_LOWER_RIGHT = "STYLE_LOWER_RIGHT";//f点在右下角 private GradientDrawable drawableLeftTopRight; private GradientDrawable drawableLeftLowerRight; private GradientDrawable drawableRightTopRight; private GradientDrawable drawableRightLowerRight; private GradientDrawable drawableHorizontalLowerRight; private GradientDrawable drawableBTopRight; private GradientDrawable drawableBLowerRight; private GradientDrawable drawableCTopRight; private GradientDrawable drawableCLowerRight; private Bitmap pathAContentBitmap;//A区域内容Bitmap private Bitmap pathBContentBitmap;//B区域内容Bitmap private Bitmap pathCContentBitmap;//C区域内容Bitmap public BookPageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context,attrs); } private void init(Context context, @Nullable AttributeSet attrs){ defaultWidth = 600; defaultHeight = 1000; a = new MyPoint(); f = new MyPoint(); g = new MyPoint(); e = new MyPoint(); h = new MyPoint(); c = new MyPoint(); j = new MyPoint(); b = new MyPoint(); k = new MyPoint(); d = new MyPoint(); i = new MyPoint(); pointPaint = new Paint(); pointPaint.setColor(Color.RED); pointPaint.setTextSize(25); pointPaint.setStyle(Paint.Style.STROKE); bgPaint = new Paint(); bgPaint.setColor(Color.GREEN); pathAPaint = new Paint(); pathAPaint.setColor(Color.GREEN); pathAPaint.setAntiAlias(true);//设置抗锯齿 pathBPaint = new Paint(); pathBPaint.setColor(getResources().getColor(R.color.blue_light)); pathBPaint.setAntiAlias(true);//设置抗锯齿 // pathBPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));我们不需要单独绘制path了,记得注释掉 pathCPaint = new Paint(); pathCPaint.setColor(Color.YELLOW); pathCPaint.setAntiAlias(true);//设置抗锯齿 // pathCPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); // pathCPaint.setStyle(Paint.Style.STROKE); pathCContentPaint = new Paint(); pathCContentPaint.setColor(Color.YELLOW); pathCContentPaint.setAntiAlias(true);//设置抗锯齿 textPaint = new Paint(); textPaint.setColor(Color.BLACK); textPaint.setTextAlign(Paint.Align.CENTER); textPaint.setSubpixelText(true);//设置自像素。如果该项为true,将有助于文本在LCD屏幕上的显示效果。 textPaint.setTextSize(30); pathA = new Path(); pathB = new Path(); pathC = new Path(); style = STYLE_LOWER_RIGHT; mScroller = new Scroller(context,new LinearInterpolator()); mMatrix = new Matrix(); createGradientDrawable(); } private void drawPathAContentBitmap(Bitmap bitmap,Paint pathPaint){ Canvas mCanvas = new Canvas(bitmap); //下面开始绘制区域内的内容... mCanvas.drawPath(getPathDefault(),pathPaint); mCanvas.drawText("这是在A区域的内容...AAAA", viewWidth-260, viewHeight-100, textPaint); //结束绘制区域内的内容... } private void drawPathBContentBitmap(Bitmap bitmap,Paint pathPaint){ Canvas mCanvas = new Canvas(bitmap); //下面开始绘制区域内的内容... mCanvas.drawPath(getPathDefault(),pathPaint); mCanvas.drawText("这是在B区域的内容...BBBB", viewWidth-260, viewHeight-100, textPaint); //结束绘制区域内的内容... } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = measureSize(defaultHeight, heightMeasureSpec); int width = measureSize(defaultWidth, widthMeasureSpec); setMeasuredDimension(width, height); viewWidth = width; viewHeight = height; a.x = -1; a.y = -1; pathAContentBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.RGB_565); drawPathAContentBitmap(pathAContentBitmap,pathAPaint); pathBContentBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.RGB_565); drawPathBContentBitmap(pathBContentBitmap,pathBPaint); pathCContentBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.RGB_565); drawPathAContentBitmap(pathCContentBitmap,pathCPaint); } private int measureSize(int defaultSize,int measureSpec) { int result = defaultSize; int specMode = View.MeasureSpec.getMode(measureSpec); int specSize = View.MeasureSpec.getSize(measureSpec); if (specMode == View.MeasureSpec.EXACTLY) { result = specSize; } else if (specMode == View.MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } return result; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.YELLOW); if(a.x==-1 && a.y==-1){ drawPathAContent(canvas,getPathDefault()); }else { if(f.x==viewWidth && f.y==0){ drawPathAContent(canvas,getPathAFromTopRight()); drawPathCContent(canvas,getPathAFromTopRight()); drawPathBContent(canvas,getPathAFromTopRight()); }else if(f.x==viewWidth && f.y==viewHeight){ beginTrace("drawPathA"); drawPathAContent(canvas,getPathAFromLowerRight()); endTrace(); beginTrace("drawPathC"); drawPathCContent(canvas,getPathAFromLowerRight()); endTrace(); beginTrace("drawPathB"); drawPathBContent(canvas,getPathAFromLowerRight()); endTrace(); } } //绘制各标识点 /*canvas.drawText("a",a.x,a.y,pointPaint); canvas.drawText("f",f.x,f.y,pointPaint); canvas.drawText("g",g.x,g.y,pointPaint); canvas.drawText("e",e.x,e.y,pointPaint); canvas.drawText("h",h.x,h.y,pointPaint); canvas.drawText("c",c.x,c.y,pointPaint); canvas.drawText("j",j.x,j.y,pointPaint); canvas.drawText("b",b.x,b.y,pointPaint); canvas.drawText("k",k.x,k.y,pointPaint); canvas.drawText("d",d.x,d.y,pointPaint); canvas.drawText("i",i.x,i.y,pointPaint);*/ } @TargetApi(18) private void beginTrace(String tag){ Trace.beginSection(tag); } @TargetApi(18) private void endTrace(){ Trace.endSection(); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { float x = mScroller.getCurrX(); float y = mScroller.getCurrY(); if(style.equals(STYLE_TOP_RIGHT)){ setTouchPoint(x,y,STYLE_TOP_RIGHT); }else { setTouchPoint(x,y,STYLE_LOWER_RIGHT); } if (mScroller.getFinalX() == x && mScroller.getFinalY() == y){ setDefaultPath(); } } } @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: float x = event.getX(); float y = event.getY(); if(x<=viewWidth/3){//左 style = STYLE_LEFT; setTouchPoint(x,y,style); }else if(x>viewWidth/3 && y<=viewHeight/3){//上 style = STYLE_TOP_RIGHT; setTouchPoint(x,y,style); }else if(x>viewWidth*2/3 && y>viewHeight/3 && y<=viewHeight*2/3){//右 style = STYLE_RIGHT; setTouchPoint(x,y,style); }else if(x>viewWidth/3 && y>viewHeight*2/3){//下 style = STYLE_LOWER_RIGHT; setTouchPoint(x,y,style); }else if(x>viewWidth/3 && xviewHeight/3 && y