Android:shape、XML绘图

简介:

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,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
XML API PHP
Android使用XML-RPC实现blog客户端
Android使用XML-RPC实现blog客户端
19 2
|
8天前
|
Android开发 开发者
Android UI设计中,Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等,定义在`styles.xml`。
【6月更文挑战第26天】Android UI设计中,Theme定义了Activity的视觉风格,包括颜色、字体、窗口样式等,定义在`styles.xml`。要更改主题,首先在该文件中创建新主题,如`MyAppTheme`,覆盖所需属性。然后,在`AndroidManifest.xml`中应用主题至应用或特定Activity。运行时切换主题可通过重新设置并重启Activity实现,或使用`setTheme`和`recreate()`方法。这允许开发者定制界面并与品牌指南匹配,或提供多主题选项。
19 6
|
1月前
|
XML 存储 JavaScript
50. 【Android教程】xml 数据解析
50. 【Android教程】xml 数据解析
20 1
|
2月前
|
Android开发
android string.xml文件中的整型和string型代替
android string.xml文件中的整型和string型代替
22 0
|
2月前
|
XML Android开发 数据格式
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class
31 0
|
2月前
|
XML 数据格式
android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual
android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual
18 0
|
11月前
|
XML Android开发 数据格式
Android中利用shape属性自定义设置Button按钮
Android中利用shape属性自定义设置Button按钮
176 0
|
2月前
|
Android开发
[Android]Shape Drawable
[Android]Shape Drawable
57 0
|
11月前
|
XML Java Android开发
#4,Android Studio Android程序结构 工程目录介绍 文件作用 运行配置文件AndroidManifest.xml
#4,Android Studio Android程序结构 工程目录介绍 文件作用 运行配置文件AndroidManifest.xml
|
11月前
|
XML Android开发 数据格式
Android Binary XML file line #50: Error inflating class androidx.cardview.widget.CardView 错误
Android Binary XML file line #50: Error inflating class androidx.cardview.widget.CardView 错误
64 0