Created
February 17, 2016 18:06
-
-
Save JohnAZoidberg/c78ca595f5844c006352 to your computer and use it in GitHub Desktop.
Android-Activity for drawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.widget.Toast; | |
public class CanvasActivity extends AppCompatActivity { | |
PaintView paintView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
paintView = new PaintView(this); | |
setContentView(paintView); | |
mPaint = new Paint(); | |
mPaint.setAntiAlias(true); | |
mPaint.setDither(true); | |
mPaint.setColor(0xFF000000); | |
mPaint.setStyle(Paint.Style.STROKE); | |
mPaint.setStrokeJoin(Paint.Join.ROUND); | |
mPaint.setStrokeCap(Paint.Cap.ROUND); | |
mPaint.setStrokeWidth(80); | |
} | |
private Paint mPaint; | |
public class PaintView extends View { | |
private Bitmap mBitmap; | |
private Canvas mCanvas; | |
private Path mPath; | |
private Paint mBitmapPaint; | |
private float mX, mY; | |
private static final float TOUCH_TOLERANCE = 4; | |
public void removeLastStroke() { | |
} | |
public void clearCanvas() { | |
mCanvas.drawColor(Color.WHITE); | |
invalidate(); | |
} | |
public PaintView(Context c) { | |
super(c); | |
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); | |
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); | |
mCanvas = new Canvas(mBitmap); | |
mCanvas.drawColor(Color.WHITE); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
canvas.drawColor(Color.WHITE); | |
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); | |
canvas.drawPath(mPath, mPaint); | |
} | |
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(); | |
if(mX != 0 && mY != 0) { | |
Toast.makeText(CanvasActivity.this, "(" + mX + ", " + mY + ")", Toast.LENGTH_SHORT).show(); | |
} | |
mX = 0; | |
mY = 0; | |
} | |
@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; | |
} | |
} | |
@Override | |
public void onBackPressed() { | |
//removeLastStroke(); | |
clearCanvas(); | |
} | |
public void removeLastStroke() { | |
paintView.removeLastStroke(); | |
} | |
public void clearCanvas() { | |
paintView.clearCanvas(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment