1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| public class EraseView extends View {
private boolean isMove = false; private Bitmap bitmap = null; private Bitmap frontBitmap = null; private Path path; private Canvas mCanvas; private Paint paint;
public EraseView(Context context) { this(context, null); }
public EraseView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public EraseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mCanvas == null) { createEraseBitmap(); } canvas.drawBitmap(bitmap, 0, 0, null); mCanvas.drawPath(path, paint); }
public void createEraseBitmap() { bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_4444); frontBitmap = createBitmap(Color.GRAY, getWidth(), getHeight());
paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); paint.setAntiAlias(true); paint.setDither(true); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(60);
path = new Path();
mCanvas = new Canvas(bitmap); mCanvas.drawBitmap(frontBitmap, 0, 0, null); }
@Override public boolean onTouchEvent(MotionEvent event) { float ax = event.getX(); float ay = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) { isMove = false; path.reset(); path.moveTo(ax, ay); invalidate(); return true; } else if (event.getAction() == MotionEvent.ACTION_MOVE) { isMove = true; path.lineTo(ax, ay); invalidate(); return true; } return super.onTouchEvent(event); }
public Bitmap createBitmap(int color, int width, int height) { int[] rgb = new int[width * height];
for (int i = 0; i < rgb.length; i++) { rgb[i] = color; }
return Bitmap.createBitmap(rgb, width, height, Config.ARGB_8888); }
}
|