Created
October 11, 2015 17:42
-
-
Save elevenetc/509494de5840fa0913da to your computer and use it in GitHub Desktop.
Brush.java
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
private static class Brush implements ValueAnimator.AnimatorUpdateListener { | |
private Bitmap brush; | |
private Bitmap buffer; | |
private int duration; | |
private TextSurface textSurface; | |
private Bitmap brushSample; | |
private Canvas canvasBuffer; | |
private Text text; | |
private final float x; | |
private final float y; | |
private float width; | |
private float height; | |
private Canvas brushCanvas; | |
private Rect src = new Rect(); | |
private Rect dst = new Rect(); | |
private final int sampleWidth; | |
private int startOffset; | |
private int endOffset; | |
private float px; | |
public Brush( | |
Text text, | |
float x, | |
float y, | |
float width, | |
float height, | |
int duration, | |
TextSurface textSurface, | |
Bitmap brushSample | |
) { | |
this.text = text; | |
this.x = x; | |
this.y = y; | |
this.width = width; | |
this.height = height; | |
this.duration = duration; | |
this.textSurface = textSurface; | |
this.brushSample = brushSample; | |
brush = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888); | |
buffer = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888); | |
brushCanvas = new Canvas(brush); | |
canvasBuffer = new Canvas(buffer); | |
sampleWidth = brushSample.getWidth(); | |
startOffset = 0; | |
endOffset = -sampleWidth + startOffset; | |
} | |
public void onDraw(Canvas canvas, Paint paint) { | |
moveBrush(paint); | |
buffer.eraseColor(Color.TRANSPARENT); | |
canvasBuffer.drawBitmap(brush, 0, 0, paint); | |
canvas.drawBitmap(buffer, x, y - height, paint); | |
canvas.drawText(text.getValue(), x, y, paint); | |
} | |
private void moveBrush(Paint paint) { | |
float x = (width + endOffset) * px; | |
src.set(0, 0, (int) (brushSample.getWidth() * px), brushSample.getHeight()); | |
dst.set(0, 0, brushSample.getWidth(), brushSample.getHeight()); | |
dst.offset((int) (x - startOffset), 0); | |
brushCanvas.drawBitmap(brushSample, src, dst, paint); | |
} | |
Animator getAnimator() { | |
ValueAnimator animator = ValueAnimator.ofFloat(0, 1); | |
animator.setInterpolator(new FastOutSlowInInterpolator()); | |
animator.addUpdateListener(this); | |
animator.setDuration(duration); | |
return animator; | |
} | |
@Override public void onAnimationUpdate(ValueAnimator animation) { | |
px = (float) animation.getAnimatedValue(); | |
textSurface.invalidate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment