Created
July 6, 2016 13:48
-
-
Save therealanshuman/52e467f4785833ab1836bb8bdac47384 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.example.zeus.neatometer; | |
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.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import java.util.ArrayList; | |
public class SketchView extends View { | |
private Path drawingPath; | |
private Paint canvasPaint; | |
private Paint drawingPaint; | |
private Canvas drawingCanvas; | |
private Bitmap canvasBitmap; | |
private float xcoor, ycoor; | |
private static final int PAINT_COLOR = 0xFF000000; | |
private static final float BRUSH_SIZE = 6; | |
private static final float TOUCH_TOLERANCE = 4; | |
private ArrayList<Path> paths; | |
private ArrayList<Path> undonePaths; | |
private void init() { | |
drawingPath = new Path(); | |
drawingPaint = new Paint(); | |
drawingPaint.setColor(PAINT_COLOR); | |
drawingPaint.setAntiAlias(true); | |
drawingPaint.setStrokeWidth(BRUSH_SIZE); | |
drawingPaint.setStyle(Paint.Style.STROKE); | |
drawingPaint.setStrokeJoin(Paint.Join.ROUND); | |
drawingPaint.setStrokeCap(Paint.Cap.ROUND); | |
canvasPaint = new Paint(Paint.DITHER_FLAG); | |
paths = new ArrayList<>(); | |
undonePaths = new ArrayList<>(); | |
} | |
public SketchView(Context context, AttributeSet attributeSet) { | |
super(context, attributeSet); | |
init(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
for (Path p:paths) { | |
canvas.drawPath(p, drawingPaint); | |
} | |
canvas.drawPath(drawingPath, drawingPaint); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); | |
drawingCanvas = new Canvas(canvasBitmap); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
float touchX = event.getX(); | |
float touchY = event.getY(); | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
touchActionDown(touchX, touchY); | |
invalidate(); | |
break; | |
case MotionEvent.ACTION_MOVE: | |
touchActionMove(touchX, touchY); | |
invalidate(); | |
break; | |
case MotionEvent.ACTION_UP: | |
touchActionUp(); | |
invalidate(); | |
break; | |
default: | |
return false; | |
} | |
return true; | |
} | |
private void touchActionDown(float x, float y) { | |
undonePaths.clear(); | |
drawingPath.reset(); | |
drawingPath.moveTo(x, y); | |
xcoor = x; | |
ycoor = y; | |
} | |
private void touchActionMove(float x, float y) { | |
float dx = Math.abs(x - xcoor); | |
float dy = Math.abs(y - ycoor); | |
if (dx>=TOUCH_TOLERANCE || dy>=TOUCH_TOLERANCE) { | |
drawingPath.quadTo(xcoor, ycoor, (x+xcoor)/2, (y+ycoor)/2); | |
xcoor = x; | |
ycoor = y; | |
} | |
} | |
private void touchActionUp() { | |
drawingPath.lineTo(xcoor, ycoor); | |
drawingCanvas.drawPath(drawingPath, drawingPaint); | |
paths.add(drawingPath); | |
drawingPath = new Path(); | |
} | |
public void onEraseAllPressed() { | |
drawingPath = new Path(); | |
paths.clear(); | |
drawingCanvas.drawColor(Color.WHITE); | |
invalidate(); | |
} | |
public void onUndoPressed() { | |
if (paths.size() > 0) { | |
undonePaths.add(paths.remove(paths.size()-1)); | |
invalidate(); | |
} | |
} | |
public void onRedoPressed() { | |
if (undonePaths.size() > 0) { | |
paths.add(undonePaths.remove(undonePaths.size()-1)); | |
invalidate(); | |
} | |
} | |
public Bitmap getSketchAsBitmap() { | |
Bitmap bitmap = Bitmap.createBitmap(canvasBitmap.getWidth(), canvasBitmap.getHeight(), Bitmap.Config.ARGB_8888); | |
return bitmap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment