Detailed explanation of Android development GradientDrawable

foreword

GradientDrawable supports gradient Drawable, which is similar to shapeDrawable in terms of drawing, and supports more gradients. The GradientDrawable on the code is much more powerful than the gradient attribute under the shape in xml, because the gradient attribute under the shape only supports three-color gradients, and the GradientDrawable can have more color gradients.

GradientDrawable is the code implementation of the shape tag in Android, and various shapes can also be created by using GradientDrawable.
 GradientDrawable supports gradient Drawable, which is similar to shapeDrawable in terms of drawing, and supports more gradients. The GradientDrawable on the code is much more powerful than the gradient attribute under the shape in xml, because the gradient attribute under the shape only supports three-color gradients, and the GradientDrawable can have more color gradients.

How to use GradientDrawable
1. Obtain the shape of the control and modify it dynamically:
Since GradientDrawable is a dynamic implementation of shape, it can obtain an instance and modify it by dynamically obtaining the shape of the control,
such as my last article
Android dynamically generates shape And dynamically change the shape color

2. Create dynamically through code:

//什么都不指定默认为矩形
GradientDrawable background = new GradientDrawable();
background.setColor(Color.GREEN);
view.setBackgroundDrawable(background);

If you want to set the shape, you can use the setShape(int shape) method to set it. There are four shapes that can be set here:

GradientDrawable.RECTANGLE:矩形
GradientDrawable.OVAL:椭圆形
GradientDrawable.LINE:一条线
GradientDrawable.RING:环形(环形试了好久不知为何画不出来)

Here is an experiment with GradientDrawable.OVAL:

GradientDrawable background = new GradientDrawable();
background.setColor(Color.GREEN);
background.setShape(GradientDrawable.OVAL);
view.setBackgroundDrawable(background);

If you want to make the effect richer, add a stroke or color gradient:

GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.OVAL);
background.setStroke(10,Color.RED);//设置宽度为10px的红色描边
background.setGradientType(GradientDrawable.LINEAR\_GRADIENT);//设置线性渐变,除此之外还有:GradientDrawable.SWEEP\_GRADIENT(扫描式渐变),GradientDrawable.RADIAL\_GRADIENT(圆形渐变)
background.setColors(new int\[\]{Color.RED,Color.BLUE});//增加渐变效果需要使用setColors方法来设置颜色(中间可以增加多个颜色值)
view.setBackgroundDrawable(background);

draw a line

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

Renderings:

draw a dotted line

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

You can also turn off the hardware acceleration of the specified view in the layout

android:layerType="software"

Renderings:

draw a circle

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

Renderings:

draw a circle

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

Renderings:

Rounded Rectangle

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

Renderings:

dotted rectangle

        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.LINEAR\_GRADIENT);
        gradientDrawable.setStroke(1, Color.GREEN,30, 30);
        mTextView.setBackground(gradientDrawable);

Renderings:

color gradient

linear gradient

        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);
        mTextView.setBackground(gradientDrawable);

Renderings:

Change the direction of the linear gradient

        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);
        mTextView.setBackground(gradientDrawable);

Renderings:

radius gradient

        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);
        mTextView.setBackground(gradientDrawable);

Renderings:

scan gradient

        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);
        mTextView.setBackground(gradientDrawable);

anti shake

gradientDrawable.setDither(true);

It can make the color gradient lower and softer during the gradient

transparency

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

at last

The Android development of GradientDrawable has been explained in detail here. If you need more learning materials for Android development, you can scan the code to get it for free! "Necessary Skills for Architects", "Analysis of the Top 100 Frameworks of Android", "Analysis of Android Performance Optimization", "Advanced Kotlin Strengthening", "Advanced Decryption of Android Advanced UI Open Source Framework", "NDK Module Development" , "Advanced Flutter Technology", "WeChat Mini Program Development". A full set of video materials is attached, including interview collection, source code collection, and open source framework collection.

Scan the QR code to receive the complete document

Table of contents

img

1. Essential Skills for Architects

1. In-depth understanding of Java generics 2. Annotations in simple terms 3. Concurrent programming 4. Data transmission and serialization 5. Java virtual machine principles 6. Efficient IO...img

2. Source Code Analysis of Top 100 Android Frameworks

1.Retrofit 2.0 source code analysis2.Okhttp3 source code analysis3.ButterKnife source code analysis4.MPAndroidChart source code analysis5.Glide source code analysis6.Leakcanary source code analysis7.Universal-lmage-Loader source code analysis8.EventBus 3.0 source code analysis9.zxing source code Analysis 10. Picasso source code analysis 11. LottieAndroid use detailed explanation and source code analysis 12. Fresco source code analysis - picture loading processimg

3. Actual analysis of Android performance optimization

1. Tencent Bugly: A Little Understanding of String Matching Algorithms

2. iQiyi: Android APP crash capture solution - xCrash

3. Bytedance: In-depth understanding of one of the Gradle frameworks: Plugin, Extension, buildSrc

4. Baidu APP technology: Android H5 first screen optimization practice

5. Analysis of Alipay client architecture: Android client startup speed optimization "garbage collection"

6. Ctrip: From the Zhixing Android project to see the practice of component architecture

7. Netease news construction optimization: how to make your construction speed "like lightning"?

img

4. Advanced kotlin strengthens actual combat

Guess you like

Origin blog.csdn.net/Android23333/article/details/129928747
Recommended