Last active
June 6, 2017 18:11
-
-
Save ssaurel/d7e99317dd709a1e324ae318c42e65cc to your computer and use it in GitHub Desktop.
Path View Step 2 for a tutorial on the SSaurel's Channel
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
package com.ssaurel.mypaint; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BlurMaskFilter; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.EmbossMaskFilter; | |
import android.graphics.MaskFilter; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.util.AttributeSet; | |
import android.util.DisplayMetrics; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import java.util.ArrayList; | |
public class PaintView extends View { | |
public static int BRUSH_SIZE = 20; | |
public static final int DEFAULT_COLOR = Color.RED; | |
public static final int DEFAULT_BG_COLOR = Color.WHITE; | |
private static final float TOUCH_TOLERANCE = 4; | |
private float mX, mY; | |
private Path mPath; | |
private Paint mPaint; | |
private ArrayList<FingerPath> paths = new ArrayList<>(); | |
private int currentColor; | |
private int backgroundColor = DEFAULT_BG_COLOR; | |
private int strokeWidth; | |
private boolean emboss; | |
private boolean blur; | |
private MaskFilter mEmboss; | |
private MaskFilter mBlur; | |
private Bitmap mBitmap; | |
private Canvas mCanvas; | |
private Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG); | |
public PaintView(Context context) { | |
this(context, null); | |
} | |
public PaintView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
mPaint = new Paint(); | |
mPaint.setAntiAlias(true); | |
mPaint.setDither(true); | |
mPaint.setColor(DEFAULT_COLOR); | |
mPaint.setStyle(Paint.Style.STROKE); | |
mPaint.setStrokeJoin(Paint.Join.ROUND); | |
mPaint.setStrokeCap(Paint.Cap.ROUND); | |
mPaint.setXfermode(null); | |
mPaint.setAlpha(0xff); | |
mEmboss = new EmbossMaskFilter(new float[] {1, 1, 1}, 0.4f, 6, 3.5f); | |
mBlur = new BlurMaskFilter(5, BlurMaskFilter.Blur.NORMAL); | |
} | |
public void init(DisplayMetrics metrics) { | |
int height = metrics.heightPixels; | |
int width = metrics.widthPixels; | |
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | |
mCanvas = new Canvas(mBitmap); | |
currentColor = DEFAULT_COLOR; | |
strokeWidth = BRUSH_SIZE; | |
} | |
public void normal() { | |
emboss = false; | |
blur = false; | |
} | |
public void emboss() { | |
emboss = true; | |
blur = false; | |
} | |
public void blur() { | |
emboss = false; | |
blur = true; | |
} | |
public void clear() { | |
backgroundColor = DEFAULT_BG_COLOR; | |
paths.clear(); | |
normal(); | |
invalidate(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
canvas.save(); | |
mCanvas.drawColor(backgroundColor); | |
for (FingerPath fp : paths) { | |
mPaint.setColor(fp.color); | |
mPaint.setStrokeWidth(fp.strokeWidth); | |
mPaint.setMaskFilter(null); | |
if (fp.emboss) | |
mPaint.setMaskFilter(mEmboss); | |
else if (fp.blur) | |
mPaint.setMaskFilter(mBlur); | |
mCanvas.drawPath(fp.path, mPaint); | |
} | |
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); | |
canvas.restore(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment