Search in sources :

Example 46 with LinearGradient

use of android.graphics.LinearGradient in project dobby-android by InceptAi.

the class CircularGauge method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float padding = getStrokeWidth();
    float size = getWidth() < getHeight() ? getWidth() : getHeight();
    float width = size - (2 * padding);
    float height = size - (2 * padding);
    float radius = (width < height ? width / 2 : height / 2);
    float rectLeft = (getWidth() - (2 * padding)) / 2 - radius + padding;
    float rectTop = (getHeight() - (2 * padding)) / 2 - radius + padding;
    float rectRight = (getWidth() - (2 * padding)) / 2 - radius + padding + width;
    float rectBottom = (getHeight() - (2 * padding)) / 2 - radius + padding + height;
    mRect.set(rectLeft, rectTop, rectRight, rectBottom);
    mPaint.setColor(mStrokeColor);
    mPaint.setShader(null);
    mPaint.setStrokeWidth(mBaselineStrokeWidth);
    canvas.drawArc(mRect, mStartAngle, mSweepAngle, false, mPaint);
    mPaint.setColor(mPointStartColor);
    mPaint.setShader(new LinearGradient(getWidth(), getHeight(), 0, 0, mPointEndColor, mPointStartColor, Shader.TileMode.CLAMP));
    /* Switch to mStrokeWidth when showing the real data */
    mPaint.setStrokeWidth(mStrokeWidth);
    if (mPointSize > 0) {
        // if size of pointer is defined
        if (mCurrentValueInAngle > mStartAngle + mPointSize / 2) {
            canvas.drawArc(mRect, mCurrentValueInAngle - mPointSize / 2, mPointSize, false, mPaint);
        } else {
            // to avoid exceeding start/zero point
            canvas.drawArc(mRect, mCurrentValueInAngle, mPointSize, false, mPaint);
        }
    } else {
        // draw from start point to value point (long pointer)
        if (// use non-zero default value for start point (to avoid lack of pointer for start/zero value)
        mValue == mStartValue)
            canvas.drawArc(mRect, mStartAngle, DEFAULT_LONG_POINTER_SIZE, false, mPaint);
        else
            canvas.drawArc(mRect, mStartAngle, mCurrentValueInAngle - mStartAngle, false, mPaint);
    }
    if (mDividerSize > 0) {
        mPaint.setColor(mDividerColor);
        mPaint.setShader(null);
        int i = mDividerDrawFirst ? 0 : 1;
        int max = mDividerDrawLast ? mDividersCount + 1 : mDividersCount;
        for (; i < max; i++) {
            canvas.drawArc(mRect, mStartAngle + i * mDividerStepAngle, mDividerSize, false, mPaint);
        }
    }
}
Also used : LinearGradient(android.graphics.LinearGradient) Paint(android.graphics.Paint)

Example 47 with LinearGradient

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

the class FakeShadowDrawable method buildShadowCorners.

private void buildShadowCorners() {
    RectF innerBounds = new RectF(-mCornerRadius, -mCornerRadius, mCornerRadius, mCornerRadius);
    RectF outerBounds = new RectF(innerBounds);
    outerBounds.inset(-mShadowSize, -mShadowSize);
    if (mCornerShadowPath == null) {
        mCornerShadowPath = new Path();
    } else {
        mCornerShadowPath.reset();
    }
    mCornerShadowPath.setFillType(Path.FillType.EVEN_ODD);
    mCornerShadowPath.moveTo(-mCornerRadius, 0);
    mCornerShadowPath.rLineTo(-mShadowSize, 0);
    // outer arc
    mCornerShadowPath.arcTo(outerBounds, 180f, 90f, false);
    // inner arc
    mCornerShadowPath.arcTo(innerBounds, 270f, -90f, false);
    mCornerShadowPath.close();
    float startRatio = mCornerRadius / (mCornerRadius + mShadowSize);
    mCornerShadowPaint.setShader(new RadialGradient(0, 0, mCornerRadius + mShadowSize, new int[] { mShadowStartColor, mShadowStartColor, mShadowEndColor }, new float[] { 0f, startRatio, 1f }, Shader.TileMode.CLAMP));
    // we offset the content shadowSize/2 pixels up to make it more realistic.
    // this is why edge shadow shader has some extra space
    // When drawing bottom edge shadow, we use that extra space.
    mEdgeShadowPaint.setShader(new LinearGradient(0, -mCornerRadius + mShadowSize, 0, -mCornerRadius - mShadowSize, new int[] { mShadowStartColor, mShadowStartColor, mShadowEndColor }, new float[] { 0f, .5f, 1f }, Shader.TileMode.CLAMP));
}
Also used : RectF(android.graphics.RectF) Path(android.graphics.Path) LinearGradient(android.graphics.LinearGradient) RadialGradient(android.graphics.RadialGradient)

Example 48 with LinearGradient

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

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)

Example 49 with LinearGradient

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

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 50 with LinearGradient

use of android.graphics.LinearGradient in project weex-example by KalicyZhou.

the class WXResourceUtils method getShader.

/**
   * Assembly gradients
   * @param image gradient values contains direction、colors
   * @param width component width
   * @param height component height
   * @return gradient shader
   */
public static Shader getShader(String image, float width, float height) {
    List<String> valueList = parseGradientValues(image);
    if (valueList != null && valueList.size() == 3) {
        float[] points = parseGradientDirection(valueList.get(0), width, height);
        Shader shader = new LinearGradient(points[0], points[1], points[2], points[3], getColor(valueList.get(1), Color.WHITE), getColor(valueList.get(2), Color.WHITE), Shader.TileMode.REPEAT);
        return shader;
    }
    return null;
}
Also used : LinearGradient(android.graphics.LinearGradient) Shader(android.graphics.Shader)

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