Android绘图开发教程 - 手写板


效果图:

View代码:activity里设置显示该view即可

  1. package com.tszy.view;  
  2.   
  3. import Android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Bitmap.Config;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Path;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12.   
  13. /** 
  14.  * 双缓冲区绘图demo 手写板 
  15.  *  
  16.  * @author JianbinZhu 
  17.  *  
  18.  */  
  19. public class View7 extends View {  
  20.     private Paint paint;  
  21.     private Canvas cacheCanvas;  
  22.     private Bitmap cachebBitmap;  
  23.     private Path path;  
  24.   
  25.     public View7(Context context) {  
  26.         super(context);  
  27.         // TODO Auto-generated constructor stub   
  28.         paint = new Paint();  
  29.         paint.setAntiAlias(true);  
  30.         paint.setStrokeWidth(3);  
  31.         paint.setStyle(Paint.Style.STROKE);  
  32.         paint.setColor(Color.CYAN);  
  33.   
  34.         path = new Path();  
  35.         cachebBitmap = Bitmap.createBitmap(480320, Config.ARGB_8888);  
  36.         cacheCanvas = new Canvas(cachebBitmap);  
  37.     }  
  38.   
  39.     @Override  
  40.     protected void onDraw(Canvas canvas) {  
  41.         canvas.drawColor(Color.BLACK);  
  42.   
  43.         //绘制上一次的,否则不连贯   
  44.         canvas.drawBitmap(cachebBitmap, 00null);  
  45.         canvas.drawPath(path, paint);  
  46.     }  
  47.   
  48.     private float cur_x, cur_y;  
  49.     private boolean isMoving;  
  50.     @Override  
  51.     public boolean onTouchEvent(MotionEvent event) {  
  52.         // TODO Auto-generated method stub   
  53.         float x = event.getX();  
  54.         float y = event.getY();  
  55.   
  56.         switch (event.getAction()) {  
  57.             case MotionEvent.ACTION_DOWN : {  
  58.                 cur_x = x;  
  59.                 cur_y = y;  
  60.                 path.moveTo(cur_x, cur_y);  
  61.                 isMoving = true;  
  62.                 break;  
  63.             }  
  64.   
  65.             case MotionEvent.ACTION_MOVE : {  
  66.                 // 二次曲线方式绘制   
  67.                 path.quadTo(cur_x, cur_y, x, y);  
  68.                 // 下面这个方法貌似跟上面一样   
  69.                 // path.lineTo(x, y);   
  70.                 cur_x = x;  
  71.                 cur_y = y;  
  72.                 break;  
  73.             }  
  74.   
  75.             case MotionEvent.ACTION_UP : {  
  76.                 // 鼠标弹起保存最后状态   
  77.                 cacheCanvas.drawPath(path, paint);  
  78.                 path.reset();  
  79.                 isMoving = false;  
  80.                 break;  
  81.             }  
  82.         }  
  83.   
  84.         // 刷新界面   
  85.         invalidate();  
  86.   
  87.         return true;  
  88.     }  
  89. }  

相关内容