12.18android绘图,橡皮擦

百度上找到的:

package com.yunxunzh.wlyxh100.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;

import com.yunxunzh.wlyxh100.R;

public class DrawEraser extends View {
	private final static String TAG = "DrawView";
	private int x = 0;
	private int y = 0;
	private int r = 0;
	private Paint mPaint = null;
	Bitmap bitmap = null;
	Canvas mcanvas = null;

	public DrawEraser(Context context ) {
		super(context);

		//设置背景,如果不设置,擦掉后变白
		setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(),
				R.drawable.welcome_bg)));
		
		mPaint = new Paint();
		mPaint.setAlpha(0);
		mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		mPaint.setAntiAlias(true);
		
		Bitmap bm = BitmapFactory.decodeResource(getResources(),
				R.drawable.welcome_bg);

		bitmap = Bitmap.createBitmap(300, 400, Config.ARGB_8888);
		mcanvas = new Canvas();
		mcanvas.setBitmap(bitmap);
		mcanvas.drawBitmap(bm, 0, 0, null);
		this.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				x = (int) event.getX();
				y = (int) event.getY();
				r = (int) 20;
				invalidate();
				return true;
			}
		});
	}

	@Override
	protected void onDraw(Canvas canvas) {
		mcanvas.drawCircle(x, y, r, mPaint);
		canvas.drawBitmap(bitmap, 0, 0, null);
		super.onDraw(canvas);
	}

}







你可能感兴趣的:(android)