public void clearDrawing()
{
Utils.Log("RaghunandanDraw, how to clear....");
setDrawingCacheEnabled(false);
// don't forget that one and the match below,
// or you just keep getting a duplicate when you save.
onSizeChanged(width, height, width, height);
invalidate();
setDrawingCacheEnabled(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
width = w; // don't forget these
height = h;
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
Many have asked how to "save" the drawing. Here's how to do that:
public DrawingView(Context c)
{
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
etc...
// in the class where you set up the view, add this:
setDrawingCacheEnabled( true );
}
public void saveDrawing()
{
Bitmap whatTheUserDrewBitmap = getDrawingCache();
// don't forget to clear it (see above) or you just get duplicates
// almost always you will want to reduce res from the very high screen res
whatTheUserDrewBitmap =
ThumbnailUtils.extractThumbnail(whatTheUserDrewBitmap, 256, 256);
// NOTE that's an incredibly useful trick for cropping/resizing squares
// while handling all memory problems etc
// http://stackoverflow.com/a/17733530/294884
// you can now save the bitmap to a file, or display it in an ImageView:
ImageView testArea = ...
testArea.setImageBitmap( whatTheUserDrewBitmap );
// these days you often need a "byte array". for example,
// to save to parse.com or other cloud services
ByteArrayOutputStream baos = new ByteArrayOutputStream();
whatTheUserDrewBitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);
byte[] yourByteArray;
yourByteArray = baos.toByteArray();
}
I think it's important to add a thing, if you use the layout inflation that constructor in the drawview is not correct, add these constructors in the class:
public DrawingView(Context c, AttributeSet attrs) {
super(c, attrs);
...
}
public DrawingView(Context c, AttributeSet attrs, int defStyle) {
super(c, attrs, defStyle);
...
}
or the android system fails to inflate the layout file. I hope this could to help.
public class DrawingCanvas extends View {
private Paint mPaint;
private Path mPath;
private boolean isUserInteractionEnabled = false;
public DrawingCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(10);
mPath = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(mPath, mPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (isUserInteractionEnabled) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPath.moveTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_UP:
break;
}
}
return true;
}
public void moveCursorTo(float x, float y) {
mPath.moveTo(x, y);
}
public void makeLine(float toX, float toY) {
mPath.lineTo(toX, toY);
}
public void setUserInteractionEnabled(boolean userInteractionEnabled) {
isUserInteractionEnabled = userInteractionEnabled;
}
}
然后把它当成
drawingCanvas.setUserInteractionEnabled(true) // to enable user interaction
drawingCanvas.setUserInteractionEnabled(true) // to disable user interaction
以编程方式绘制
drawingCanvas.moveCursorTo(70f, 70f) // Move the cursor (Define starting point)
drawingCanvas.makeLine(200f, 200f) // End point (To where you need to draw)