Finger Paint View : Paint « 2D Graphics « Android






Finger Paint View

   
//package com.aflyroc.doodle.view;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class FingerPaintView extends View {
  Path path;
  Bitmap bitmapBuff;
  Paint paint;
  Paint pen;
  Canvas canvas;
  Bitmap originBitmap;
  Bitmap pureBitmp;
  private float scaleWidth;
  private float scaleHeight;
  private float mX,mY;
  private static float TOUCH_TOLERANCE = 4;
  private Canvas printCanvas;
  Bitmap printBitmapBuff;
  private List<Path> pathList;
  int i = 0;

  public FingerPaintView(Context context, AttributeSet attrs) {
    super(context, attrs);
    loadBitmap(context);
    Log.i("Context context, AttributeSet attrs", "11");
  }

  public FingerPaintView(Context context) {
    super(context);
    loadBitmap(context);
    Log.i("Context context", "222");
  }

  public void loadBitmap(Context context) {
    
    pathList = new ArrayList<Path>();
    path = new Path();
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(1.6f);
    

    InputStream stream = null;//getResources().openRawResource(drawable.precheck);

    originBitmap = BitmapFactory.decodeStream(stream);

    int width = originBitmap.getWidth();

    int height = originBitmap.getHeight();
    
    DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics(); 

    int newWidth = metrics.widthPixels;
    
    int newHeight = metrics.heightPixels;

    // 
    scaleWidth = ((float) newWidth / width);
    
    scaleHeight = ((float) newHeight / height);

    Matrix matrix = new Matrix();

    matrix.postScale(scaleWidth, scaleHeight);

    pureBitmp = Bitmap.createBitmap(originBitmap, 0, 0, width, height,matrix, true);

    bitmapBuff = Bitmap.createBitmap(pureBitmp.getWidth(),pureBitmp.getHeight(), Bitmap.Config.ARGB_8888);
    
    canvas = new Canvas(bitmapBuff);

    canvas.drawBitmap(pureBitmp, 0, 0, paint);

  }

  public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      touchStart(x, y);
      invalidate();
      break;
    case MotionEvent.ACTION_MOVE:
      touchMove(x, y);
      invalidate();
      break;
    case MotionEvent.ACTION_UP:
      touchUp(x, y);
      invalidate();
      break;
    }
    return true;
  }

  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // 

    canvas.drawBitmap(bitmapBuff, 0, 0, paint);
    // 
    canvas.drawPath(path, paint);

  }
  private void touchStart(float x, float y)
  {
    path.reset();
    path.moveTo(x, y);
    mX = x;
    mY=y;
    
  }
  private void touchMove(float x,float y)
  {
    float dx = Math.abs(x-mX);
    float dy = Math.abs(y-mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
      path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
            
        }
  }
  private void touchUp(float x,float y)
  {

    pathList.add(new Path(path));
    
    canvas.drawPath(path,paint);
    
    path.reset();
  }
  public void createPrintCanvas()
  {

    Bitmap printBitmap = Bitmap.createBitmap(originBitmap);
    
    printBitmapBuff = Bitmap.createBitmap(printBitmap.getWidth(),printBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    
    Matrix matrix = new Matrix();

    matrix.postScale(1/scaleWidth, 1/scaleHeight);
    
    printCanvas = new Canvas(printBitmapBuff);
    
    printCanvas.drawBitmap(printBitmap, 0, 0, paint);
    
    for(int i=0;i<pathList.size();i++)
    {
      Path tempPath = new Path(pathList.get(i));
      
      tempPath.transform(matrix);
      
      printCanvas.drawPath(tempPath, paint);
      
      Log.d("path", tempPath.toString());
      
    }
    
    printCanvas.save();

  }

  public float getScaleWidth() {
    return scaleWidth;
  }

  public float getScaleHeight() {
    return scaleHeight;
  }

}

   
    
    
  








Related examples in the same category

1.Paint.ANTI_ALIAS_FLAG
2.Paint.Style.STROKE
3.Finger Paint