Android Open Source - Donnect Drawing View






From Project

Back to project page Donnect.

License

The source code is released under:

Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...

If you think the Android project Donnect listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.donnect;
/*from  w  ww  . j  a  va  2  s  .co m*/
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;

public class DrawingView extends View {
  private float smallBrush, mediumBrush, largeBrush;
  
  private Path drawPath;
  private Paint drawPaint, canvasPaint;
  private int paintColor = R.color.Pink;
  private Canvas drawCanvas;
  private Bitmap canvasBitmap;
  
  public DrawingView(Context context, AttributeSet attrs){
      super(context, attrs);
      setupDrawing();
  }

  private void setupDrawing() {
    // TODO Auto-generated method stub
    drawPath = new Path();
    drawPaint = new Paint();
    
    
    smallBrush = getResources().getInteger(R.integer.small_size);
    mediumBrush = getResources().getInteger(R.integer.medium_size);
    largeBrush = getResources().getInteger(R.integer.large_size);
    
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(20);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
    drawPaint.setStrokeWidth(mediumBrush);
    
    canvasPaint = new Paint(Paint.DITHER_FLAG);
    //drawPaint.setColor(paintColor);
    
  }
  
  public void setColor(){
    //set color 
    invalidate();
    drawPaint.setColor(paintColor);
    }
  
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  //view given size
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
  }
  
  
  @Override
  protected void onDraw(Canvas canvas) {
  //draw view
    canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    canvas.drawPath(drawPath, drawPaint);
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
  //detect user touch     
    float touchX = event.getX();
    float touchY = event.getY();
    
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        drawPath.moveTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_MOVE:
        drawPath.lineTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_UP:
        drawCanvas.drawPath(drawPath, drawPaint);
        drawPath.reset();
        break;
    default:
        return false;
    }
    
    invalidate();
    return true;
  }
  
}




Java Source Code List

com.example.donnect.Donnect.java
com.example.donnect.DrawingView.java