search

Home  >  Q&A  >  body text

Android 渐变

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

巴扎黑巴扎黑2888 days ago482

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:36:36

    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);
        }
    }

    reply
    0
  • Cancelreply