Created
November 3, 2017 08:56
-
-
Save s1ntoneli/b9c19306fb16d886d2f69fabcfe292a1 to your computer and use it in GitHub Desktop.
任意角度的渐变图片 Drawable
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
| import android.graphics.Canvas; | |
| import android.graphics.ColorFilter; | |
| import android.graphics.LinearGradient; | |
| import android.graphics.Paint; | |
| import android.graphics.Rect; | |
| import android.graphics.RectF; | |
| import android.graphics.Shader; | |
| import android.graphics.drawable.Drawable; | |
| import android.support.annotation.ColorInt; | |
| import android.support.annotation.IntRange; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.Nullable; | |
| public class DegreeGradientDrawable extends Drawable { | |
| @ColorInt | |
| private int[] gradientColors; | |
| private float[] positions; | |
| /** | |
| * from left to right, by clockwise. range 0 - 360. | |
| */ | |
| private int degree; | |
| private float radius; | |
| private boolean isRectDirty; | |
| private RectF mRect = new RectF(); | |
| private RectF mBoundRectF = new RectF(); | |
| private final Paint mFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
| private final Paint mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
| public DegreeGradientDrawable(int degree, int[] colors, float[] positions) { | |
| this.degree = degree; | |
| this.gradientColors = colors; | |
| this.positions = positions; | |
| this.isRectDirty = true; | |
| } | |
| @Override | |
| public void draw(@NonNull Canvas canvas) { | |
| if (!ensureValidRect()) { | |
| return; | |
| } | |
| canvas.drawRoundRect(mRect, radius, radius, mFillPaint); | |
| canvas.drawRoundRect(mRect, radius, radius, mStrokePaint); | |
| } | |
| @Override | |
| public void setAlpha(@IntRange(from = 0, to = 255) int alpha) { | |
| } | |
| @Override | |
| public void setColorFilter(@Nullable ColorFilter colorFilter) { | |
| } | |
| @Override | |
| public int getOpacity() { | |
| return 0; | |
| } | |
| public void setCornerRadius(float radius) { | |
| this.radius = radius; | |
| } | |
| public void setStroke(int width, @ColorInt int color) { | |
| setStrokeInternal(width, color); | |
| this.isRectDirty = true; | |
| } | |
| private void setStrokeInternal(int width, int color) { | |
| mStrokePaint.setStyle(Paint.Style.STROKE); | |
| mStrokePaint.setStrokeWidth(width); | |
| mStrokePaint.setColor(color); | |
| } | |
| @Override | |
| protected void onBoundsChange(Rect bounds) { | |
| isRectDirty = true; | |
| } | |
| private boolean ensureValidRect() { | |
| if (isRectDirty) { | |
| isRectDirty = false; | |
| Rect bounds = getBounds(); | |
| float inset = mStrokePaint.getStrokeWidth() * 0.5f; | |
| mRect.set(bounds.left + inset, bounds.top + inset, | |
| bounds.right - inset, bounds.bottom - inset); | |
| mBoundRectF.set(bounds); | |
| final float x0, x1, y0, y1; | |
| x0 = mRect.left; | |
| y0 = mRect.bottom; | |
| float _x1 = (float) (mRect.height() / Math.tan(Math.toRadians(degree))); | |
| float _y1 = (float) (mRect.width() * Math.tan(Math.toRadians(degree))); | |
| x1 = mRect.left + Math.max(mRect.right - mRect.left, _x1); | |
| y1 = mRect.bottom - Math.max(mRect.bottom - mRect.top, _y1); | |
| mFillPaint.setShader(new LinearGradient(x0, y0, x1, y1, gradientColors, positions, Shader.TileMode.CLAMP)); | |
| } | |
| return !mRect.isEmpty(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment