- /**
- * 手指绘图
- * @author Administrator
- *
- */
- public class FingerPaint extends GraphicsActivity
- implements ColorPickerDialog.OnColorChangedListener {
-
- private Paint mPaint;
- private MaskFilter mEmboss;
- private MaskFilter mBlur;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new MyView(this));
- //创建画笔实例,反锯齿...
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setDither(true);
- mPaint.setColor(0xFFFF0000);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeJoin(Paint.Join.ROUND);
- mPaint.setStrokeCap(Paint.Cap.ROUND);
- mPaint.setStrokeWidth(12);
-
- mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
- 0.4f, 6, 3.5f);
- mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
- }
- public void colorChanged(int color) {
- mPaint.setColor(color);
- }
- private static final int COLOR_MENU_ID = Menu.FIRST;
- private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
- private static final int BLUR_MENU_ID = Menu.FIRST + 2;
- private static final int ERASE_MENU_ID = Menu.FIRST + 3;
- private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;
- //选项菜单
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
- menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
- menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
- menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
- menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');
- return true;
- }
-
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
- super.onPrepareOptionsMenu(menu);
- return true;
- }
- //菜单选择事件处理
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- mPaint.setXfermode(null);
- mPaint.setAlpha(0xFF);
- switch (item.getItemId()) {
- case COLOR_MENU_ID:
- new ColorPickerDialog(this, this, mPaint.getColor()).show();
- return true;
- case EMBOSS_MENU_ID:
- if (mPaint.getMaskFilter() != mEmboss) {
- mPaint.setMaskFilter(mEmboss);
- } else {
- mPaint.setMaskFilter(null);
- }
- return true;
- case BLUR_MENU_ID:
- if (mPaint.getMaskFilter() != mBlur) {
- mPaint.setMaskFilter(mBlur);
- } else {
- mPaint.setMaskFilter(null);
- }
- return true;
- case ERASE_MENU_ID:
- mPaint.setXfermode(new PorterDuffXfermode(
- PorterDuff.Mode.CLEAR));
- return true;
- case SRCATOP_MENU_ID:
- mPaint.setXfermode(new PorterDuffXfermode(
- PorterDuff.Mode.SRC_ATOP));
- mPaint.setAlpha(0x80);
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * 内部view的子类
- * canvas作为画布,paint作为画笔
- * 根据手指的触屏事件更改mpath路径
- * onDraw实时带出mpath路径
- * @author Administrator
- *
- */
- public class MyView extends View {
-
- private static final float MINP = 0.25f;
- private static final float MAXP = 0.75f;
-
- private Bitmap mBitmap;
- private Canvas mCanvas;
- private Path mPath;
- private Paint mBitmapPaint;
-
- public MyView(Context c) {
- super(c);
- //产生前景图
- mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
- mCanvas = new Canvas(mBitmap);
- mPath = new Path();
- mBitmapPaint = new Paint(Paint.DITHER_FLAG);
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- }
- //自动重绘
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawColor(0xFFAAAAAA);
- //绘前景
- canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
- //给手指轨迹
- canvas.drawPath(mPath, mPaint);
- }
-
- private float mX, mY;
- private static final float TOUCH_TOLERANCE = 4;
-
- private void touch_start(float x, float y) {
- mPath.reset();
- mPath.moveTo(x, y);
- mX = x;
- mY = y;
- }
- private void touch_move(float x, float y) {
- float dx = Math.abs(x - mX);
- float dy = Math.abs(y - mY);
- if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
- mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
- mX = x;
- mY = y;
- }
- }
- private void touch_up() {
- mPath.lineTo(mX, mY);
- // commit the path to our offscreen
- mCanvas.drawPath(mPath, mPaint);
- // kill this so we don't double draw
- mPath.reset();
- }
- //角屏事件
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- float x = event.getX();
- float y = event.getY();
-
- switch (event.getAction()) {
- //按下
- case MotionEvent.ACTION_DOWN:
- touch_start(x, y);
- invalidate();
- break;
- //滑动
- case MotionEvent.ACTION_MOVE:
- touch_move(x, y);
- invalidate();
- break;
- //抬起
- case MotionEvent.ACTION_UP:
- touch_up();
- invalidate();
- break;
- }
- return true;
- }
- }
-
- }
复制代码 |