Search in sources :

Example 91 with LinearGradient

use of android.graphics.LinearGradient in project XobotOS by xamarin.

the class GradientDrawable method ensureValidRect.

/**
     * This checks mRectIsDirty, 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 (mRectIsDirty) {
        mRectIsDirty = 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[] colors = st.mColors;
        if (colors != null) {
            RectF r = mRect;
            float x0, x1, y0, y1;
            if (st.mGradient == LINEAR_GRADIENT) {
                final float level = st.mUseLevel ? (float) 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, colors, 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;
                final float level = st.mUseLevel ? (float) getLevel() / 10000.0f : 1.0f;
                mFillPaint.setShader(new RadialGradient(x0, y0, level * st.mGradientRadius, colors, 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 = colors;
                float[] tempPositions = null;
                if (st.mUseLevel) {
                    tempColors = st.mTempColors;
                    final int length = colors.length;
                    if (tempColors == null || tempColors.length != length + 1) {
                        tempColors = st.mTempColors = new int[length + 1];
                    }
                    System.arraycopy(colors, 0, tempColors, 0, length);
                    tempColors[length] = colors[length - 1];
                    tempPositions = st.mTempPositions;
                    final float fraction = 1.0f / (float) (length - 1);
                    if (tempPositions == null || tempPositions.length != length + 1) {
                        tempPositions = st.mTempPositions = new float[length + 1];
                    }
                    final float level = (float) 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));
            }
        }
    }
    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 92 with LinearGradient

use of android.graphics.LinearGradient in project react-native-navigation by wix.

the class TranslucentDrawable method createShader.

private void createShader() {
    ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            double angleInRadians = Math.toRadians(90);
            int x1 = (int) (Math.cos(angleInRadians) * width);
            int y1 = (int) (Math.sin(angleInRadians) * height);
            int[] colors = new int[] { Color.argb(90, 0, 0, 0), Color.argb(15, 0, 0, 0), Color.TRANSPARENT };
            float[] positions = { 0, 0.78f, 1 };
            LinearGradient lg = new LinearGradient(0, 0, x1, y1, colors, positions, Shader.TileMode.REPEAT);
            return lg;
        }
    };
    setShaderFactory(sf);
}
Also used : LinearGradient(android.graphics.LinearGradient) ShapeDrawable(android.graphics.drawable.ShapeDrawable)

Example 93 with LinearGradient

use of android.graphics.LinearGradient in project WordPress-Android by wordpress-mobile.

the class StatsBarGraph method drawSeries.

@Override
public void drawSeries(Canvas canvas, GraphViewDataInterface[] values, float graphwidth, float graphheight, float border, double minX, double minY, double diffX, double diffY, float horstart, GraphViewSeriesStyle style) {
    float colwidth = graphwidth / values.length;
    int maxColumnSize = getGraphViewStyle().getMaxColumnWidth();
    if (maxColumnSize > 0 && colwidth > maxColumnSize) {
        colwidth = maxColumnSize;
    }
    paint.setStrokeWidth(style.thickness);
    paint.setColor(style.color);
    // Bar chart position of this series on the canvas
    List<BarChartRect> barChartRects = new LinkedList<>();
    // draw data
    for (int i = 0; i < values.length; i++) {
        float valY = (float) (values[i].getY() - minY);
        float ratY = (float) (valY / diffY);
        float y = graphheight * ratY;
        // hook for value dependent color
        if (style.getValueDependentColor() != null) {
            paint.setColor(style.getValueDependentColor().get(values[i]));
        }
        float pad = style.padding;
        float left = (i * colwidth) + horstart;
        float top = (border - y) + graphheight;
        float right = left + colwidth;
        float bottom = graphheight + border - 1;
        // Draw the orange selection behind the selected bar
        if (style.outerhighlightColor != 0x00ffffff && mBarPositionToHighlight == i) {
            paint.setColor(style.outerhighlightColor);
            canvas.drawRect(left, 10f, right, bottom, paint);
        }
        // Draw the grey background color on weekend days
        if (style.outerColor != 0x00ffffff && mBarPositionToHighlight != i && mWeekendDays != null && mWeekendDays[i]) {
            paint.setColor(style.outerColor);
            canvas.drawRect(left, 10f, right, bottom, paint);
        }
        if ((top - bottom) == 1) {
            // draw a placeholder
            if (mBarPositionToHighlight != i) {
                paint.setColor(style.color);
                paint.setAlpha(25);
                Shader shader = new LinearGradient(left + pad, bottom - 50, left + pad, bottom, Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
                paint.setShader(shader);
                canvas.drawRect(left + pad, bottom - 50, right - pad, bottom, paint);
                paint.setShader(null);
            }
        } else {
            // draw a real bar
            paint.setAlpha(255);
            if (mBarPositionToHighlight == i) {
                paint.setColor(style.highlightColor);
            } else {
                paint.setColor(style.color);
            }
            canvas.drawRect(left + pad, top, right - pad, bottom, paint);
        }
        barChartRects.add(new BarChartRect(left + pad, top, right - pad, bottom));
    }
    mSeriesRectsDrawedOnScreen.add(barChartRects);
}
Also used : LinearGradient(android.graphics.LinearGradient) Shader(android.graphics.Shader) LinkedList(java.util.LinkedList)

Example 94 with LinearGradient

use of android.graphics.LinearGradient in project ABPlayer by winkstu.

the class ColorPickerView method drawSatValPanel.

private void drawSatValPanel(Canvas canvas) {
    final RectF rect = mSatValRect;
    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(mDrawingRect.left, mDrawingRect.top, rect.right + BORDER_WIDTH_PX, rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }
    if (mValShader == null) {
        mValShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, 0xffffffff, 0xff000000, TileMode.CLAMP);
    }
    int rgb = Color.HSVToColor(new float[] { mHue, 1f, 1f });
    mSatShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, 0xffffffff, rgb, TileMode.CLAMP);
    ComposeShader mShader = new ComposeShader(mValShader, mSatShader, PorterDuff.Mode.MULTIPLY);
    mSatValPaint.setShader(mShader);
    canvas.drawRect(rect, mSatValPaint);
    Point p = satValToPoint(mSat, mVal);
    mSatValTrackerPaint.setColor(0xff000000);
    canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS - 1f * mDensity, mSatValTrackerPaint);
    mSatValTrackerPaint.setColor(0xffdddddd);
    canvas.drawCircle(p.x, p.y, PALETTE_CIRCLE_TRACKER_RADIUS, mSatValTrackerPaint);
}
Also used : RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) ComposeShader(android.graphics.ComposeShader) Point(android.graphics.Point) Point(android.graphics.Point) Paint(android.graphics.Paint)

Example 95 with LinearGradient

use of android.graphics.LinearGradient in project ABPlayer by winkstu.

the class ColorPickerView method drawAlphaPanel.

private void drawAlphaPanel(Canvas canvas) {
    if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null)
        return;
    final RectF rect = mAlphaRect;
    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX, rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }
    mAlphaPattern.draw(canvas);
    float[] hsv = new float[] { mHue, mSat, mVal };
    int color = Color.HSVToColor(hsv);
    int acolor = Color.HSVToColor(0, hsv);
    mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP);
    mAlphaPaint.setShader(mAlphaShader);
    canvas.drawRect(rect, mAlphaPaint);
    if (mAlphaSliderText != null && mAlphaSliderText != "") {
        canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
    }
    float rectWidth = 4 * mDensity / 2;
    Point p = alphaToPoint(mAlpha);
    RectF r = new RectF();
    r.left = p.x - rectWidth;
    r.right = p.x + rectWidth;
    r.top = rect.top - RECTANGLE_TRACKER_OFFSET;
    r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;
    canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);
}
Also used : RectF(android.graphics.RectF) LinearGradient(android.graphics.LinearGradient) Point(android.graphics.Point) Point(android.graphics.Point) 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