-
-
Save ParticleCore/2aa3aff971d5423e9300e0fb489f7e0f to your computer and use it in GitHub Desktop.
a sample example of Shader.
according to http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
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
public class RoundedDrawable extends Drawable { | |
private static final boolean USE_VIGNETTE = true; | |
private RectF mRect = new RectF(); | |
private final Paint paint; | |
private final int mRadius; | |
private final BitmapShader bitmapShader; | |
public RoundedDrawable(Bitmap bitmap, int radius) { | |
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); | |
paint = new Paint(); | |
paint.setAntiAlias(true); | |
paint.setShader(bitmapShader); | |
mRadius = radius; | |
} | |
@Override | |
protected void onBoundsChange(Rect bounds) { | |
super.onBoundsChange(bounds); | |
mRect.set(0, 0, bounds.width(), bounds.height()); | |
if (USE_VIGNETTE) { | |
RadialGradient vignette = new RadialGradient( | |
mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f, | |
new int[]{0, 0, 0x7f000000}, new float[]{0.0f, 0.7f, 1.0f}, | |
Shader.TileMode.CLAMP); | |
Matrix oval = new Matrix(); | |
oval.setScale(1.0f, 0.7f); | |
vignette.setLocalMatrix(oval); | |
paint.setShader( | |
new ComposeShader(bitmapShader, vignette, PorterDuff.Mode.SRC_OVER)); | |
} | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
canvas.drawRoundRect(mRect, mRadius, mRadius, paint); | |
// canvas.drawCircle(mRect.right / 2, mRect.bottom / 2, mRadius, paint); | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
paint.setAlpha(alpha); | |
} | |
@Override | |
public void setColorFilter(ColorFilter cf) { | |
paint.setColorFilter(cf); | |
} | |
@Override | |
public int getOpacity() { | |
return PixelFormat.TRANSLUCENT; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment