美文网首页Android
GradientDrawable详解

GradientDrawable详解

作者: 狂奔的_土豆 | 来源:发表于2020-12-08 18:52 被阅读0次

    GradientDrawable 支持渐变色的Drawable,与shapeDrawable在画型上是类似的,多了支持渐变色。代码上的GradientDrawable比在xml里的shape下gradient属性强大的多,因为shape下gradient属性只支持三色阶渐变,而GradientDrawable可以有更多的色阶渐变。

    画线

            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.LINE);
            //线的宽度 与 线的颜色
            gradientDrawable.setStroke(5, Color.YELLOW);
            textView.setBackground(gradientDrawable);
    

    画虚线

            //要显示虚线一定要关闭硬件加速       
            textView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.LINE);
            //第一个参数为线的宽度  第二个参数是线的颜色 第三个参数是虚线段的长度 
            //第四个参数是虚线段之间的间距长度
            gradientDrawable.setStroke(1, Color.BLACK, 10, 10);
            textView.setBackground(gradientDrawable);
    

    也可以在布局里关闭指定view的硬件加速

            android:layerType="software"
    

    画圆

            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.OVAL);
            gradientDrawable.setColor(Color.BLUE);
            gradientDrawable.setSize(50,50);
            textView.setBackground(gradientDrawable);
    

    画圆环

            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.OVAL);
            gradientDrawable.setColor(Color.BLUE);
            gradientDrawable.setStroke(10,Color.YELLOW);
            gradientDrawable.setSize(50,50);
            textView.setBackground(gradientDrawable);
    

    圆角矩形

            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);
            gradientDrawable.setColor(Color.RED);
            gradientDrawable.setStroke(10,Color.BLUE);
            gradientDrawable.setCornerRadius(10);
            gradientDrawable.setSize(50,50);
            mTextView.setBackground(gradientDrawable);
    

    虚线矩形

            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.LINEAR_GRADIENT);
            gradientDrawable.setStroke(1, Color.GREEN,30, 30);
            textView.setBackground(gradientDrawable);
    

    线性渐变

            int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);
            //添加颜色组
            gradientDrawable.setColors(colors);
            //设置线性渐变
            gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
            gradientDrawable.setSize(50,50);
            textView.setBackground(gradientDrawable);
    

    改变线性渐变方向

            int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);
            gradientDrawable.setColors(colors);
            //设置线性渐变
            gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
            //设置渐变方向
            gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT);
            gradientDrawable.setSize(50,50);
            textView.setBackground(gradientDrawable);
    

    半径渐变

            int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);
            //添加颜色组
            gradientDrawable.setColors(colors); 
            //设置半径渐变
            gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
            //渐变的半径值
            gradientDrawable.setGradientRadius(50);
            gradientDrawable.setSize(50,50);
            textView.setBackground(gradientDrawable);
    

    扫描渐变

            int[] colors = {Color.YELLOW, Color.GREEN, Color.BLUE};
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);
            gradientDrawable.setColors(colors); 
            //设置扫描渐变
            gradientDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);
            //渐变中心点
            gradientDrawable.setGradientCenter(0.5f,0.5f);
            gradientDrawable.setSize(50,50);
            textView.setBackground(gradientDrawable);
    

    防抖,可以让渐变的时候颜色阶梯降低,变得更柔和

            gradientDrawable.setDither(true);
    

    透明度

            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setShape(GradientDrawable.RECTANGLE);
            gradientDrawable.setColor(Color.YELLOW); 
            //设置透明度
            gradientDrawable.setAlpha(70);
            textView.setBackground(gradientDrawable);
    

    相关文章

      网友评论

        本文标题:GradientDrawable详解

        本文链接:https://www.haomeiwen.com/subject/bisqgktx.html