上代码,然后没了
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import com.example.culturecloud.R;
import java.util.HashMap;
/**
* Created by leafly on 2019/12/12.
*/
public class ErasureView extends ImageView {
private int height,width;
private Bitmap front_Bitmap,back_Bitmap;
private Bitmap fore_bitmap;
String str = "";
private Paint paint;
private Canvas canvasTemp;//声明画布类
Path path;//画图类
HashMap<Integer,PointBean> points=new HashMap<Integer, PointBean>();
private Boolean is_prepare_size = false,is_prepare_bitmap = false,is_prepare = true;
public ErasureView(Context context) {
super(context);
init();
}
public ErasureView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
paint =new Paint();
init();
}
void init_paint(){
paint.setColor(Color.RED);//设置画笔颜色
paint.setStrokeWidth(25);//设置描边宽度
BlurMaskFilter bmf = new BlurMaskFilter(40, BlurMaskFilter.Blur.NORMAL);//指定了一个模糊的样式和半径来处理Paint的边缘。
paint. setMaskFilter(bmf);//为Paint分配边缘效果。
paint.setStyle(Paint.Style.FILL);//让画出的图形是空心的
//paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//它的作用是用此画笔后,画笔划过的痕迹就变成透明色了。画笔设置好了后,就可以调用该画笔进行橡皮痕迹的绘制了
paint.setStrokeJoin(Paint.Join.ROUND);//设置结合处的样子 Miter:结合处为锐角, Round:结合处为圆弧:BEVEL:结合处为直线。
paint.setStrokeCap(Paint.Cap.ROUND);//画笔笔刷类型 方形形状
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
path = new Path();//实例化画图类
}
void init_canvas() {
int width = this.getWidth(); // 获取宽度
int height =this.getHeight(); // 获取高度
Bitmap bitmap = resizeBitmap(front_Bitmap,width ,height);
fore_bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
canvasTemp = new Canvas(fore_bitmap);
canvasTemp.drawColor(Color.TRANSPARENT);
canvasTemp.drawBitmap(bitmap,0,0,null);
is_prepare = true;
}
public void setImages(Bitmap front_Bitmap,Bitmap back_Bitmap){
this.front_Bitmap= front_Bitmap;
this.setImageBitmap(back_Bitmap);
is_prepare_bitmap = true;
if(is_prepare_size){
init_canvas();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(is_prepare){
int pointerCount = event.getPointerCount();
HashMap<Integer,PointBean> new_points=new HashMap<Integer, PointBean>();
int id = 0;
long time=System.currentTimeMillis();
if (canvasTemp != null) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_MOVE:
for (int i = 0; i < pointerCount; i++) {
int point_id=(int) event.getPointerId(i);
int x = (int) event.getX(i);
int y = (int) event.getY(i);
new_points.put(point_id,new PointBean(x,y,time));
if(points.containsKey(point_id)) {
PointBean old_point=points.get(point_id);
if(time-old_point.mCurTime<150) canvasTemp.drawLine(old_point.x,old_point.y,x,y,paint);
}
}
points=new_points;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
}
}
postInvalidate();//刷新界面
}
return true;
}
private void init(){
init_paint();
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int new_width = getWidth();
int new_height = getHeight();
if(new_height!=height||new_width!=width){
width = new_width;
height = new_height;
is_prepare_size =true;
if (is_prepare_bitmap){
init_canvas();
}
}
postInvalidate();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(is_prepare){
canvas.drawBitmap(fore_bitmap, 0, 0, null);//绘制图像
}
}
public Bitmap resizeBitmap(Bitmap bitmap,int bitmap_width,int bitmap_height){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = (float)bitmap_width/width;
float scaleHeight =(float)bitmap_height/height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,true);
return newbm;
}
public class PointBean {
public long mCurTime=0;
public int x;
public int y;
public PointBean(int x, int y, long mCurTime)
{
this.x=x;
this.y=y;
this.mCurTime=mCurTime;
}
}
}