Android 渐变
巴扎黑
巴扎黑 2017-04-17 17:34:36
0
1
445

1.android 中的gradient最多只支持三种颜色的渐变,我想支持10颜色的渐变有没有其他的方法

巴扎黑
巴扎黑

reply all(1)
刘奇

I researched and customized one

/**
 * Created by xg on 2016/8/4.
 * 自定义渐变view
 */
public class DrawView extends View {
    private LinearGradient linearGradient = null;
    private Paint paint = null;

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        linearGradient = new LinearGradient(0, 0, getWidth() - getPaddingRight(), 0,
                new int[]{Color.YELLOW, Color.GREEN, Color.TRANSPARENT, Color.WHITE}, null,
                Shader.TileMode.REPEAT);
        paint = new Paint();
        //设置渲染器
        paint.setShader(linearGradient);
        //绘制圆环
        RectF rect = new RectF(0, 0, getWidth() - getPaddingRight(), getHeight() - getPaddingBottom());
        canvas.drawRect(rect, paint);
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template