Created
September 22, 2015 09:23
-
-
Save jbruchanov/5328c8034909e9803b17 to your computer and use it in GitHub Desktop.
RevealDrawable.java
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.smallplanet.android.planned.drawable; | |
import android.graphics.Canvas; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.drawable.Drawable; | |
import android.view.View; | |
/** | |
* Created by JBruchanov on 21/09/2015. | |
*/ | |
public class RevealDrawable extends Drawable { | |
private int mRevealColor; | |
private int mBackgroundColor; | |
private int mDrawBackgroundColor; | |
private int mDrawForegroundColor; | |
private Paint mPaint; | |
private long mStarted; | |
private int mDuration = 1000; | |
private int mX, mY,mRad; | |
public RevealDrawable(int backgroundColor, int revealColor) { | |
mBackgroundColor = mDrawBackgroundColor = backgroundColor; | |
mRevealColor = mDrawForegroundColor = revealColor; | |
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
float progress = getAnimationProgress(); | |
if (progress == 1) { | |
canvas.drawColor(mDrawForegroundColor); | |
} else if (progress == 0) { | |
canvas.drawColor(mDrawBackgroundColor); | |
} else { | |
canvas.drawColor(mDrawBackgroundColor); | |
canvas.drawCircle(mX, mY, progress * mRad, mPaint); | |
invalidateSelf(); | |
} | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
mPaint.setAlpha(alpha); | |
} | |
@Override | |
public void setColorFilter(ColorFilter colorFilter) { | |
mPaint.setColorFilter(colorFilter); | |
} | |
@Override | |
public int getOpacity() { | |
return mPaint.getAlpha(); | |
} | |
public void start() { | |
start(-1, -1); | |
} | |
public void start(int x, int y) { | |
Callback callback = getCallback(); | |
if (callback != null && callback instanceof View) { | |
View holder = (View) callback; | |
int width = holder.getWidth(); | |
int height = holder.getHeight(); | |
if (x < 0 || y < 0) { | |
x = width / 2; | |
y = height / 2; | |
} | |
start(x, y, Math.max(width, height)); | |
} | |
} | |
public int getBackgroundColor() { | |
return mBackgroundColor; | |
} | |
public void setBackgroundColor(int backgroundColor) { | |
mBackgroundColor = backgroundColor; | |
mStarted = 0; | |
if (mDrawBackgroundColor != backgroundColor) { | |
invalidateSelf(); | |
} | |
} | |
public int getRevealColor() { | |
return mRevealColor; | |
} | |
public void setRevealColor(int revealColor) { | |
mRevealColor = revealColor; | |
} | |
protected float getAnimationProgress() { | |
long diff = System.currentTimeMillis() - mStarted; | |
if (mStarted == 0) { | |
return 0; | |
} else if (diff > mDuration) { | |
return 1; | |
} else { | |
return diff / (float) mDuration; | |
} | |
} | |
protected void start(int x, int y, int radius) { | |
mDrawBackgroundColor = mBackgroundColor; | |
mDrawForegroundColor = mRevealColor; | |
mX = x; | |
mY = y; | |
mRad = radius; | |
mStarted = System.currentTimeMillis(); | |
mPaint.setColor(mDrawForegroundColor); | |
invalidateSelf(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment