Created
October 14, 2015 09:10
-
-
Save vedant1811/99bf00fbd678fcc03a5b to your computer and use it in GitHub Desktop.
Draws lines on the set drawable based on the user touches. sample: http://imgur.com/RNO6MA0
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.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.ViewTreeObserver; | |
import android.widget.ImageView; | |
/** | |
* Draws lines on the set drawable based on the user touches. The input is limited by the bounds | |
* of the first set image, if {@link #setAdjustViewBounds} or its XML attribute is used. | |
* TODO: adjust bounds when the drawable is changed | |
* Created by vedant on 13/10/15. | |
*/ | |
public class FingerDrawView extends ImageView { | |
private static final String TAG = "FingerDrawView"; | |
private static final float MINP = 0.25f; | |
private static final float MAXP = 0.75f; | |
private Bitmap mBitmap; | |
private Canvas mCanvas; | |
private Path mPath; | |
private Paint mBitmapPaint; | |
private Paint mPaint; | |
public FingerDrawView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
mPath = new Path(); | |
mBitmapPaint = new Paint(Paint.DITHER_FLAG); | |
mPaint = new Paint(); | |
mPaint.setAntiAlias(true); | |
mPaint.setDither(true); | |
mPaint.setColor(Color.RED); | |
mPaint.setStyle(Paint.Style.STROKE); | |
mPaint.setStrokeJoin(Paint.Join.ROUND); | |
mPaint.setStrokeCap(Paint.Cap.ROUND); | |
mPaint.setStrokeWidth(12); | |
ViewTreeObserver vto = getViewTreeObserver(); | |
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { | |
public boolean onPreDraw() { | |
Drawable drawable = getDrawable(); | |
// wait for drawable to be set | |
if (drawable != null) { | |
// Remove after the first run so it doesn't fire forever | |
getViewTreeObserver().removeOnPreDrawListener(this); | |
int w, h; | |
w = drawable.getIntrinsicWidth(); | |
h = drawable.getIntrinsicHeight(); | |
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); | |
mCanvas = new Canvas(mBitmap); | |
LogWrapper.d(TAG, "drawable != null Height: " + h + " Width: " + w); | |
} | |
return true; | |
} | |
}); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if (mBitmap == null) | |
return; | |
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); | |
canvas.drawPath(mPath, mPaint); | |
} | |
private float mX, mY; | |
private static final float TOUCH_TOLERANCE = 4; | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
float x = event.getX(); | |
float y = event.getY(); | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
mPath.reset(); | |
mPath.moveTo(x, y); | |
mX = x; | |
mY = y; | |
break; | |
case MotionEvent.ACTION_MOVE: | |
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; | |
} | |
break; | |
case MotionEvent.ACTION_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(); | |
break; | |
} | |
invalidate(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment