Search in sources :

Example 26 with LinearGradient

use of android.graphics.LinearGradient in project ListenerMusicPlayer by hefuyicoder.

the class ScrimUtil method makeCubicGradientScrimDrawable.

/**
     * Creates an approximated cubic gradient using a multi-stop linear gradient. See
     * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
     * details.
     */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, final int gravity) {
    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;
    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }
    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 = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
        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;
        case Gravity.CENTER_HORIZONTAL:
        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;
        case Gravity.CENTER_VERTICAL:
        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, gravity == Gravity.CENTER_VERTICAL ? width : width * x1, gravity == Gravity.CENTER_HORIZONTAL ? height : height * y1, gravity == Gravity.CENTER_HORIZONTAL || gravity == Gravity.CENTER_VERTICAL ? makeDoubleStopColors(stopColors) : stopColors, null, Shader.TileMode.CLAMP);
        }
    });
    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}
Also used : LinearGradient(android.graphics.LinearGradient) RectShape(android.graphics.drawable.shapes.RectShape) PaintDrawable(android.graphics.drawable.PaintDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) Drawable(android.graphics.drawable.Drawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) Shader(android.graphics.Shader)

Example 27 with LinearGradient

use of android.graphics.LinearGradient in project PhotoNoter by yydcdut.

the class FloatingActionButton method createInnerStrokesDrawable.

/**
     * 创建内层drawable
     *
     * @param color
     * @param strokeWidth
     * @return
     */
private Drawable createInnerStrokesDrawable(final int color, float strokeWidth) {
    //不描边的话直接返回一个颜色为透明的drawable
    if (!mStrokeVisible) {
        return new ColorDrawable(Color.TRANSPARENT);
    }
    //圆 drawable
    ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
    //这个暗颜色
    final int bottomStrokeColor = darkenColor(color);
    //比bottomStrokeColor透明一半
    final int bottomStrokeColorHalfTransparent = halfTransparent(bottomStrokeColor);
    //这个亮颜色
    final int topStrokeColor = lightenColor(color);
    //比topStrokeColor透明一半
    final int topStrokeColorHalfTransparent = halfTransparent(topStrokeColor);
    final Paint paint = shapeDrawable.getPaint();
    //锯齿
    paint.setAntiAlias(true);
    //描边宽度
    paint.setStrokeWidth(strokeWidth);
    //描边
    paint.setStyle(Style.STROKE);
    //draws a linear gradient
    shapeDrawable.setShaderFactory(new ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width / 2, 0, width / 2, height, new int[] { topStrokeColor, topStrokeColorHalfTransparent, color, bottomStrokeColorHalfTransparent, bottomStrokeColor }, new float[] { 0f, 0.2f, 0.5f, 0.8f, 1f }, TileMode.CLAMP);
        }
    });
    return shapeDrawable;
}
Also used : LinearGradient(android.graphics.LinearGradient) ShaderFactory(android.graphics.drawable.ShapeDrawable.ShaderFactory) ColorDrawable(android.graphics.drawable.ColorDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) Paint(android.graphics.Paint) OvalShape(android.graphics.drawable.shapes.OvalShape) Shader(android.graphics.Shader) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint)

Example 28 with LinearGradient

use of android.graphics.LinearGradient in project XCL-Charts by xcltapestry.

the class Bar3D method renderVertical3DBar.

/**
	 * 竖向柱形的3D效果
	 * @param barLeft	左边X坐标
	 * @param barTop	顶部Y坐标
	 * @param barRight	右边X坐标
	 * @param barBottom	底部Y坐标
	 * @param color		柱形颜色
	 * @param canvas	画布
	 */
public void renderVertical3DBar(float barLeft, float barTop, float barRight, float barBottom, int color, Canvas canvas) {
    //浅色	
    int lightColor = DrawHelper.getInstance().getLightColor(color, mAlpha);
    getBarPaint().setColor(color);
    mPaint3D.setColor(lightColor);
    //水平偏移量
    double offsetX = getOffsetX();
    //垂直偏移量
    double offsetY = getOffsetY();
    //Shadow
    float barLeft2 = Math.round(barLeft - offsetX);
    float barTop2 = Math.round(barTop + offsetY);
    float barRight2 = Math.round(barRight - offsetX);
    float barBottom2 = Math.round(barBottom + offsetY);
    //顶		
    mPathRectangle2D.reset();
    mPathRectangle2D.moveTo(barLeft, barTop);
    mPathRectangle2D.lineTo(barLeft2, barTop2);
    mPathRectangle2D.lineTo(barRight2, barTop2);
    mPathRectangle2D.lineTo(barRight, barTop);
    mPathRectangle2D.close();
    canvas.drawPath(mPathRectangle2D, getBarPaint());
    //右侧边
    mPathRectangle2D.reset();
    mPathRectangle2D.moveTo(barRight, barTop);
    mPathRectangle2D.lineTo(barRight2, barTop2);
    mPathRectangle2D.lineTo(barRight2, barBottom2);
    mPathRectangle2D.lineTo(barRight, barBottom);
    mPathRectangle2D.close();
    canvas.drawPath(mPathRectangle2D, getBarPaint());
    mPathRectangle2D.reset();
    mPathRectangle2D.moveTo(barRight2, barTop2);
    mPathRectangle2D.lineTo(barRight2, barBottom2);
    mPathRectangle2D.lineTo(barLeft2, barBottom2);
    mPathRectangle2D.lineTo(barLeft2, barTop2);
    mPathRectangle2D.close();
    //正面 浅色	
    LinearGradient linearGradient = new LinearGradient(barLeft2, barBottom2, barRight2, barBottom2, new int[] { color, lightColor }, null, Shader.TileMode.REPEAT);
    mPaint3D.setShader(linearGradient);
    mPaint3D.setStyle(Style.FILL);
    canvas.drawPath(mPathRectangle2D, mPaint3D);
    //柱形顶上用白画一个RECT,强化3D效果		 
    mPaintLine.reset();
    mPaintLine.setStyle(Style.STROKE);
    mPaintLine.setColor(Color.WHITE);
    mPathRectangle2D.reset();
    mPathRectangle2D.moveTo(barLeft2, barTop2);
    mPathRectangle2D.lineTo(barRight2, barTop2);
    mPathRectangle2D.lineTo(barRight, barTop);
    canvas.drawPath(mPathRectangle2D, mPaintLine);
    //柱形正面画一根白色竖线,强化3D效果
    canvas.drawLine(barRight2, barTop2, barRight2, barBottom2, mPaintLine);
}
Also used : LinearGradient(android.graphics.LinearGradient) Paint(android.graphics.Paint)

Example 29 with LinearGradient

use of android.graphics.LinearGradient in project XCL-Charts by xcltapestry.

the class FlatBar method setBarGradient.

/**
	 * 绘制柱形渲染效果
	 * @param left	左边X坐标
	 * @param top	顶部Y坐标
	 * @param right	右边X坐标
	 * @param bottom	底部Y坐标
	 */
private void setBarGradient(float left, float top, float right, float bottom) {
    int barColor = getBarPaint().getColor();
    int lightColor = DrawHelper.getInstance().getLightColor(barColor, 150);
    float width = Math.abs(right - left);
    float height = Math.abs(bottom - top);
    Shader.TileMode tm = Shader.TileMode.MIRROR;
    if (//横向柱形
    width > height) {
        linearGradient = new LinearGradient(right, bottom, right, top, new int[] { lightColor, barColor }, null, tm);
    } else {
        linearGradient = new LinearGradient(left, bottom, right, bottom, new int[] { lightColor, barColor }, null, tm);
    }
    getBarPaint().setShader(linearGradient);
}
Also used : LinearGradient(android.graphics.LinearGradient) Shader(android.graphics.Shader) Paint(android.graphics.Paint)

Example 30 with LinearGradient

use of android.graphics.LinearGradient in project XCL-Charts by xcltapestry.

the class XChart method setBackgroundColor.

/**
	 * 设置图的渐变背景色
	 * @param direction	渐变方向
	 * @param beginColor 起始颜色
	 * @param endColor	结束颜色
	 */
public void setBackgroundColor(XEnum.Direction direction, int beginColor, int endColor) {
    if (beginColor == endColor) {
        getBackgroundPaint().setColor(beginColor);
    } else {
        LinearGradient linearGradient;
        if (direction == XEnum.Direction.VERTICAL) {
            linearGradient = new LinearGradient(0, 0, 0, getBottom() - getTop(), beginColor, endColor, Shader.TileMode.MIRROR);
        } else {
            linearGradient = new LinearGradient(getLeft(), getBottom(), getRight(), getTop(), beginColor, endColor, Shader.TileMode.CLAMP);
        }
        getBackgroundPaint().setShader(linearGradient);
    }
    if (null == mBorder)
        mBorder = new BorderRender();
    mBorder.getBackgroundPaint().setColor(endColor);
}
Also used : LinearGradient(android.graphics.LinearGradient) BorderRender(org.xclcharts.renderer.plot.BorderRender)

Aggregations

LinearGradient (android.graphics.LinearGradient)123 Paint (android.graphics.Paint)70 RectF (android.graphics.RectF)38 RadialGradient (android.graphics.RadialGradient)27 Canvas (android.graphics.Canvas)22 Rect (android.graphics.Rect)21 Path (android.graphics.Path)18 Shader (android.graphics.Shader)18 Bitmap (android.graphics.Bitmap)15 Matrix (android.graphics.Matrix)15 Point (android.graphics.Point)13 SweepGradient (android.graphics.SweepGradient)12 PorterDuffXfermode (android.graphics.PorterDuffXfermode)9 ShapeDrawable (android.graphics.drawable.ShapeDrawable)9 SuppressLint (android.annotation.SuppressLint)6 ComposeShader (android.graphics.ComposeShader)6 PaintDrawable (android.graphics.drawable.PaintDrawable)5 RectShape (android.graphics.drawable.shapes.RectShape)5 ColorMatrix (android.graphics.ColorMatrix)3 PointF (android.graphics.PointF)3