Search in sources :

Example 86 with LinearGradient

use of android.graphics.LinearGradient in project platform_frameworks_base by android.

the class MinimizedDockShadow method updatePaint.

private void updatePaint(int left, int top, int right, int bottom) {
    int startColor = mContext.getResources().getColor(R.color.minimize_dock_shadow_start, null);
    int endColor = mContext.getResources().getColor(R.color.minimize_dock_shadow_end, null);
    final int middleColor = Color.argb((Color.alpha(startColor) + Color.alpha(endColor)) / 2, 0, 0, 0);
    final int quarter = Color.argb((int) (Color.alpha(startColor) * 0.25f + Color.alpha(endColor) * 0.75f), 0, 0, 0);
    if (mDockSide == WindowManager.DOCKED_TOP) {
        mShadowPaint.setShader(new LinearGradient(0, 0, 0, bottom - top, new int[] { startColor, middleColor, quarter, endColor }, new float[] { 0f, 0.35f, 0.6f, 1f }, Shader.TileMode.CLAMP));
    } else if (mDockSide == WindowManager.DOCKED_LEFT) {
        mShadowPaint.setShader(new LinearGradient(0, 0, right - left, 0, new int[] { startColor, middleColor, quarter, endColor }, new float[] { 0f, 0.35f, 0.6f, 1f }, Shader.TileMode.CLAMP));
    } else if (mDockSide == WindowManager.DOCKED_RIGHT) {
        mShadowPaint.setShader(new LinearGradient(right - left, 0, 0, 0, new int[] { startColor, middleColor, quarter, endColor }, new float[] { 0f, 0.35f, 0.6f, 1f }, Shader.TileMode.CLAMP));
    }
}
Also used : LinearGradient(android.graphics.LinearGradient) Paint(android.graphics.Paint)

Example 87 with LinearGradient

use of android.graphics.LinearGradient in project platform_frameworks_base by android.

the class GradientDrawable method ensureValidRect.

/**
     * This checks mGradientIsDirty, and if it is true, recomputes both our drawing
     * rectangle (mRect) and the gradient itself, since it depends on our
     * rectangle too.
     * @return true if the resulting rectangle is not empty, false otherwise
     */
private boolean ensureValidRect() {
    if (mGradientIsDirty) {
        mGradientIsDirty = false;
        Rect bounds = getBounds();
        float inset = 0;
        if (mStrokePaint != null) {
            inset = mStrokePaint.getStrokeWidth() * 0.5f;
        }
        final GradientState st = mGradientState;
        mRect.set(bounds.left + inset, bounds.top + inset, bounds.right - inset, bounds.bottom - inset);
        final int[] gradientColors = st.mGradientColors;
        if (gradientColors != null) {
            final RectF r = mRect;
            final float x0, x1, y0, y1;
            if (st.mGradient == LINEAR_GRADIENT) {
                final float level = st.mUseLevel ? getLevel() / 10000.0f : 1.0f;
                switch(st.mOrientation) {
                    case TOP_BOTTOM:
                        x0 = r.left;
                        y0 = r.top;
                        x1 = x0;
                        y1 = level * r.bottom;
                        break;
                    case TR_BL:
                        x0 = r.right;
                        y0 = r.top;
                        x1 = level * r.left;
                        y1 = level * r.bottom;
                        break;
                    case RIGHT_LEFT:
                        x0 = r.right;
                        y0 = r.top;
                        x1 = level * r.left;
                        y1 = y0;
                        break;
                    case BR_TL:
                        x0 = r.right;
                        y0 = r.bottom;
                        x1 = level * r.left;
                        y1 = level * r.top;
                        break;
                    case BOTTOM_TOP:
                        x0 = r.left;
                        y0 = r.bottom;
                        x1 = x0;
                        y1 = level * r.top;
                        break;
                    case BL_TR:
                        x0 = r.left;
                        y0 = r.bottom;
                        x1 = level * r.right;
                        y1 = level * r.top;
                        break;
                    case LEFT_RIGHT:
                        x0 = r.left;
                        y0 = r.top;
                        x1 = level * r.right;
                        y1 = y0;
                        break;
                    default:
                        /* TL_BR */
                        x0 = r.left;
                        y0 = r.top;
                        x1 = level * r.right;
                        y1 = level * r.bottom;
                        break;
                }
                mFillPaint.setShader(new LinearGradient(x0, y0, x1, y1, gradientColors, st.mPositions, Shader.TileMode.CLAMP));
            } else if (st.mGradient == RADIAL_GRADIENT) {
                x0 = r.left + (r.right - r.left) * st.mCenterX;
                y0 = r.top + (r.bottom - r.top) * st.mCenterY;
                float radius = st.mGradientRadius;
                if (st.mGradientRadiusType == RADIUS_TYPE_FRACTION) {
                    // Fall back to parent width or height if intrinsic
                    // size is not specified.
                    final float width = st.mWidth >= 0 ? st.mWidth : r.width();
                    final float height = st.mHeight >= 0 ? st.mHeight : r.height();
                    radius *= Math.min(width, height);
                } else if (st.mGradientRadiusType == RADIUS_TYPE_FRACTION_PARENT) {
                    radius *= Math.min(r.width(), r.height());
                }
                if (st.mUseLevel) {
                    radius *= getLevel() / 10000.0f;
                }
                mGradientRadius = radius;
                if (radius <= 0) {
                    // We can't have a shader with non-positive radius, so
                    // let's have a very, very small radius.
                    radius = 0.001f;
                }
                mFillPaint.setShader(new RadialGradient(x0, y0, radius, gradientColors, null, Shader.TileMode.CLAMP));
            } else if (st.mGradient == SWEEP_GRADIENT) {
                x0 = r.left + (r.right - r.left) * st.mCenterX;
                y0 = r.top + (r.bottom - r.top) * st.mCenterY;
                int[] tempColors = gradientColors;
                float[] tempPositions = null;
                if (st.mUseLevel) {
                    tempColors = st.mTempColors;
                    final int length = gradientColors.length;
                    if (tempColors == null || tempColors.length != length + 1) {
                        tempColors = st.mTempColors = new int[length + 1];
                    }
                    System.arraycopy(gradientColors, 0, tempColors, 0, length);
                    tempColors[length] = gradientColors[length - 1];
                    tempPositions = st.mTempPositions;
                    final float fraction = 1.0f / (length - 1);
                    if (tempPositions == null || tempPositions.length != length + 1) {
                        tempPositions = st.mTempPositions = new float[length + 1];
                    }
                    final float level = getLevel() / 10000.0f;
                    for (int i = 0; i < length; i++) {
                        tempPositions[i] = i * fraction * level;
                    }
                    tempPositions[length] = 1.0f;
                }
                mFillPaint.setShader(new SweepGradient(x0, y0, tempColors, tempPositions));
            }
            // maxed out so that alpha modulation works correctly.
            if (st.mSolidColors == null) {
                mFillPaint.setColor(Color.BLACK);
            }
        }
    }
    return !mRect.isEmpty();
}
Also used : RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) Rect(android.graphics.Rect) RadialGradient(android.graphics.RadialGradient) SweepGradient(android.graphics.SweepGradient) Paint(android.graphics.Paint)

Example 88 with LinearGradient

use of android.graphics.LinearGradient in project Douya by DreaminginCodeZH.

the class DrawableUtils method makeScrimDrawable.

// From Muzei, Copyright 2014 Google Inc.
public static Drawable makeScrimDrawable(int baseColor, int numStops, int gravity) {
    numStops = Math.max(numStops, 2);
    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());
    final int[] stopColors = new int[numStops];
    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);
    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtils.clamp((float) Math.pow(x, 3), 0, 1);
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }
    final float x0, x1, y0, y1;
    switch(gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.LEFT:
            x0 = 1;
            x1 = 0;
            break;
        case Gravity.RIGHT:
            x0 = 0;
            x1 = 1;
            break;
        default:
            x0 = 0;
            x1 = 0;
            break;
    }
    switch(gravity & Gravity.VERTICAL_GRAVITY_MASK) {
        case Gravity.TOP:
            y0 = 1;
            y1 = 0;
            break;
        case Gravity.BOTTOM:
            y0 = 0;
            y1 = 1;
            break;
        default:
            y0 = 0;
            y1 = 0;
            break;
    }
    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null, Shader.TileMode.CLAMP);
        }
    });
    paintDrawable.setAlpha(Math.round(0.4f * 255));
    return paintDrawable;
}
Also used : LinearGradient(android.graphics.LinearGradient) RectShape(android.graphics.drawable.shapes.RectShape) ShapeDrawable(android.graphics.drawable.ShapeDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) Shader(android.graphics.Shader)

Example 89 with LinearGradient

use of android.graphics.LinearGradient in project Shimmer-android by RomainPiel.

the class ShimmerViewHelper method resetLinearGradient.

private void resetLinearGradient() {
    // our gradient is a simple linear gradient from textColor to reflectionColor. its axis is at the center
    // when it's outside of the view, the outer color (textColor) will be repeated (Shader.TileMode.CLAMP)
    // initially, the linear gradient is positioned on the left side of the view
    linearGradient = new LinearGradient(-view.getWidth(), 0, 0, 0, new int[] { primaryColor, reflectionColor, primaryColor }, new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP);
    paint.setShader(linearGradient);
}
Also used : LinearGradient(android.graphics.LinearGradient)

Example 90 with LinearGradient

use of android.graphics.LinearGradient in project android_frameworks_base by DirtyUnicorns.

the class RectShadowPainter method paintShadow.

public static void paintShadow(Outline viewOutline, float elevation, Canvas canvas) {
    Rect outline = new Rect();
    if (!viewOutline.getRect(outline)) {
        throw new IllegalArgumentException("Outline is not a rect shadow");
    }
    float shadowSize = elevationToShadow(elevation);
    int saved = modifyCanvas(canvas, shadowSize);
    if (saved == -1) {
        return;
    }
    try {
        Paint cornerPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        cornerPaint.setStyle(Style.FILL);
        Paint edgePaint = new Paint(cornerPaint);
        edgePaint.setAntiAlias(false);
        float radius = viewOutline.getRadius();
        float outerArcRadius = radius + shadowSize;
        int[] colors = { START_COLOR, START_COLOR, END_COLOR };
        cornerPaint.setShader(new RadialGradient(0, 0, outerArcRadius, colors, new float[] { 0f, radius / outerArcRadius, 1f }, TileMode.CLAMP));
        edgePaint.setShader(new LinearGradient(0, 0, -shadowSize, 0, START_COLOR, END_COLOR, TileMode.CLAMP));
        Path path = new Path();
        path.setFillType(FillType.EVEN_ODD);
        // A rectangle bounding the complete shadow.
        RectF shadowRect = new RectF(outline);
        shadowRect.inset(-shadowSize, -shadowSize);
        // A rectangle with edges corresponding to the straight edges of the outline.
        RectF inset = new RectF(outline);
        inset.inset(radius, radius);
        // A rectangle used to represent the edge shadow.
        RectF edgeShadowRect = new RectF();
        // left and right sides.
        edgeShadowRect.set(-shadowSize, 0f, 0f, inset.height());
        // Left shadow
        sideShadow(canvas, edgePaint, edgeShadowRect, outline.left, inset.top, 0);
        // Right shadow
        sideShadow(canvas, edgePaint, edgeShadowRect, outline.right, inset.bottom, 2);
        // Top shadow
        edgeShadowRect.set(-shadowSize, 0, 0, inset.width());
        sideShadow(canvas, edgePaint, edgeShadowRect, inset.right, outline.top, 1);
        // bottom shadow. This needs an inset so that blank doesn't appear when the content is
        // moved up.
        edgeShadowRect.set(-shadowSize, 0, shadowSize / 2f, inset.width());
        edgePaint.setShader(new LinearGradient(edgeShadowRect.right, 0, edgeShadowRect.left, 0, colors, new float[] { 0f, 1 / 3f, 1f }, TileMode.CLAMP));
        sideShadow(canvas, edgePaint, edgeShadowRect, inset.left, outline.bottom, 3);
        // Draw corners.
        drawCorner(canvas, cornerPaint, path, inset.right, inset.bottom, outerArcRadius, 0);
        drawCorner(canvas, cornerPaint, path, inset.left, inset.bottom, outerArcRadius, 1);
        drawCorner(canvas, cornerPaint, path, inset.left, inset.top, outerArcRadius, 2);
        drawCorner(canvas, cornerPaint, path, inset.right, inset.top, outerArcRadius, 3);
    } finally {
        canvas.restoreToCount(saved);
    }
}
Also used : Path(android.graphics.Path) RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) Rect(android.graphics.Rect) RadialGradient(android.graphics.RadialGradient) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Aggregations

LinearGradient (android.graphics.LinearGradient)128 Paint (android.graphics.Paint)74 RectF (android.graphics.RectF)41 RadialGradient (android.graphics.RadialGradient)27 Canvas (android.graphics.Canvas)23 Rect (android.graphics.Rect)21 Shader (android.graphics.Shader)19 Path (android.graphics.Path)18 Bitmap (android.graphics.Bitmap)16 Matrix (android.graphics.Matrix)16 Point (android.graphics.Point)16 SweepGradient (android.graphics.SweepGradient)12 PorterDuffXfermode (android.graphics.PorterDuffXfermode)10 ShapeDrawable (android.graphics.drawable.ShapeDrawable)10 SuppressLint (android.annotation.SuppressLint)7 ComposeShader (android.graphics.ComposeShader)7 PaintDrawable (android.graphics.drawable.PaintDrawable)5 RectShape (android.graphics.drawable.shapes.RectShape)5 ColorDrawable (android.graphics.drawable.ColorDrawable)4 ShaderFactory (android.graphics.drawable.ShapeDrawable.ShaderFactory)4