且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:shape、XML绘图

更新时间:2022-09-04 17:54:48

1.线line:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke android:width="8dp"
        android:color="#ff00ff00"
        android:dashWidth="6dp"
        android:dashGap="2dp"/>
</shape>


2.椭圆oval:

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ff345678"/>
    <stroke android:width="20dp" android:color="#ff876543"/>
</shape>


3.长方形rectangle:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="200dp"
        android:height="300dp"/>
    <solid android:color="#ff665544"/>
    <stroke android:width="20dp"
        android:color="#445566"
        android:dashWidth="40dp"
        android:dashGap="5dp"/>
</shape>


4.填充颜色solid、渐变gradient、边角corners:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="200dp"
        android:height="300dp"/>
    <solid android:color="#ff665544"/>
    <stroke android:width="20dp"
        android:color="#445566"
        android:dashWidth="40dp"
        android:dashGap="5dp"/>
        <gradient android:startColor="#ff5533aa"
            android:centerColor="#ff996633"
            android:endColor="#ff114477"
            android:angle="135"
        />
        <corners
            android:topLeftRadius="45dp"
            android:topRightRadius="45dp"
            android:bottomLeftRadius="135dp"
            android:bottomRightRadius="135dp"/>
                               
            <!-- android:radius="45dp" -->
            <!--  渐变角度 只能被45整除  -->
</shape>




5.代码编写

1
2
3
4
5
6
7
8
9
//白色背景shape
int strokeWidth = 5// 3dp
int roundRadius = 15// 8dp
int strokeColor = Color.parseColor("#2E3135");
int fillColor = Color.parseColor("#DFDFE0");
GradientDrawable gd = new GradientDrawable();
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);






本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1226254,如需转载请自行联系原作者