博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
32、详解Android shape的使用方法(转载)
阅读量:6949 次
发布时间:2019-06-27

本文共 7488 字,大约阅读时间需要 24 分钟。

 

0、java绘制shape

 

在官方API介绍中:

ShapeDrawable :This object can be defined in an XML file with the <shape> element(这个对象可以用<shape>元素在xml文件中定义)

GradientDrawable :This object can be defined in an XML file with the <shape> element(这个对象可以用<shape>元素在xml文件中定义)

 

 [父节点] shape   --   ShapeDrawable

 [子节点] gradient   --  

 [子节点] padding   --   

 [子节点] corners   --   setCornerRadius 、setCornerRadii 

 [子节点] solid       --    

 [子节点]  stroke   --   setStroke 

 [子节点]  size --   setSize

 

1、概述

 

gradient   -- 对应颜色渐变。 startcolor、endcolor就不多说了。 android:angle 是指从哪个角度开始变。

solid      --  填充。

stroke   --  描边。

corners  --  圆角。

padding   -- 定义内容离边界的距离。 与android:padding_left、android:padding_right这些是一个道理。

1 
2
//如果当做是LevelListDrawable使用时值为true,否则为false.11 12
//右下角的圆角半径18 19
//使用LevelListDrawable时就要设置为true。设为false时才有渐变效果29 30
35 36
39 40
42 43
//虚线的间隔49

 

2、圆角矩形,扫描式渐变

1 
2
6 7
12 13
19 20
21

 

2、 圆形,线性渐变

 

1 
2
6 7
14 15
16 17
19 20

 

 

3、虚线

1 
2
5 6
8 9
13

 

 

4、 环形,放射型渐变

1 
2
8 9
16 17
19 20

 

5、demo

XML/HTML代码
  1. <Button  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:text="TestShapeButton"  
  5.     android:background="@drawable/button_selector"  
  6. />  

       button_selector.xml:

XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  
  3.     ns:android="http://schemas.android.com//res/android">  
  4.     <item android:state_pressed="true" >  
  5.         <shape>  
  6.             <!-- 渐变 -->  
  7.             <gradient  
  8.                 android:startColor="#ff8c00"  
  9.                 android:endColor="#FFFFFF"  
  10.                 android:type="radial"  
  11.                 android:gradientRadius="50" />  
  12.             <!-- 描边 -->  
  13.             <stroke  
  14.                 android:width="2dp"  
  15.                 android:color="#dcdcdc"  
  16.                 android:dashWidth="5dp"    
  17.                 android:dashGap="3dp" />  
  18.             <!-- 圆角 -->  
  19.             <corners  
  20.                 android:radius="2dp" />  
  21.             <padding  
  22.                 android:left="10dp"  
  23.                 android:top="10dp"  
  24.                 android:right="10dp"  
  25.                 android:bottom="10dp" />  
  26.         </shape>  
  27.     </item>  
  28.   
  29.     <item android:state_focused="true" >  
  30.         <shape>  
  31.             <gradient  
  32.                 android:startColor="#ffc2b7"  
  33.                 android:endColor="#ffc2b7"  
  34.                 android:angle="270" />  
  35.             <stroke  
  36.                 android:width="2dp"  
  37.                 android:color="#dcdcdc" />  
  38.             <corners  
  39.                 android:radius="2dp" />  
  40.             <padding  
  41.                 android:left="10dp"  
  42.                 android:top="10dp"  
  43.                 android:right="10dp"  
  44.                 android:bottom="10dp" />  
  45.         </shape>  
  46.     </item>  
  47.   
  48.     <item>          
  49.         <shape>  
  50.             <solid android:color="#ff9d77"/>  
  51.             <stroke  
  52.                 android:width="2dp"  
  53.                 android:color="#fad3cf" />  
  54.             <corners    
  55.                 android:topRightRadius="5dp"  
  56.                 android:bottomLeftRadius="5dp"  
  57.                 android:topLeftRadius="0dp"  
  58.                 android:bottomRightRadius="0dp"  
  59.             />  
  60.             <padding  
  61.                 android:left="10dp"  
  62.                 android:top="10dp"  
  63.                 android:right="10dp"  
  64.                 android:bottom="10dp" />  
  65.         </shape>  
  66.     </item>  
  67. </selector>  

       运行效果如下图:

       一般状态:

详解Android shape的使用方法

       获得焦点状态:

详解Android shape的使用方法

       按下状态:

详解Android shape的使用方法

 

 

6、官方资料 

<shape>

Basic method for drawing shapes via XML.

Attributes

Name Type Default Description
visible boolean parent|true Determines if drawable is visible.
shape enum (rectangle, oval, line, ring) rectangle Determines the shape: rectangle (shape is a rectangle, possibly with rounded corners); oval (shape is an ellipse); line (shape is a line); ring (shape is a ring).
innerRadiusRatio float 3.0 Only valid if shape == 'ring'. Inner radius of the ring expressed as a ratio of the ring's width. For instance, if innerRadiusRatio=3, then the inner radius equals the ring's width divided by 3. This value is ignored if innerRadius is defined.
innerRadius float -1 Only valid if shape == 'ring'. Inner radius of the ring. When defined, innerRadiusRatio is ignored. When undefined, innerRadiusRatio's default is used.
thicknessRatio float 9.0 Only valid if shape == 'ring'. Thickness of the ring expressed as a ratio of the ring's width. For instance, if thicknessRatio=9, then the thickness equals the ring's width divided by 9. This value is ignored if thickness is defined. Default value is 9.
thickness float -1 Only valid if shape == 'ring'. Thickness of the ring. When defined, thicknessRatio is ignored. When undefined, thicknessRatio's default is used.
useLevel boolean true Only valid if shape == 'ring'. Allows one to draw only part of the ring (arc-wise), by modifying the drawable's level. This setting only makes sense in context of a <level-list> (LevelListDrawable).

Children

Element Description
Determines the size of the shape.
Adds a background gradient to the shape.
Adds a solid background color to the shape. Overides gradient element.
Adds a border to the shape.
Adds rounded corners to the shape.
The padding for the content within this drawable. (Does not pad graphics in any way.)

<size>

Determines the size of the shape.

Attributes

Name Type Default Description
width dimension -1 Width of the shape.
height dimension -1 Height of the shape.

<gradient>

Adds a background gradient to the shape.

Attributes

Name Type Default Description
startColor color 0 The color at the start of the gradient.
centerColor color 0 The color in the center of the gradient. Optional; if not included, there is no center color.
endColor color 0 The color at the end of the gradient.
type enum (linear, radial, sweep) linear Determines the type of gradient.
centerX float|fraction .5 Determines the location of the centerColor. Ranges from 0 to 1. Ignored if centerColor is undefined.
centerY float|fraction .5 Determines the location of the centerColor. Ranges from 0 to 1. Ignored if centerColor is undefined.
angle float 0 Only valid if type == 'linear'. Determines the angle of a linear gradient. Must be a multiple of 45 degrees.
gradientRadius float|fraction N/A Only valid if type == 'radial' or 'sweep'. Required if type == 'radial'. Determines the radius of the gradient.
useLevel boolean false Determines the amount of the gradient to be drawn, based on the level of the shape. Affects all three gradient types.

<solid>

Adds a solid background color to the shape. Overides gradient element.

Attributes

Name Type Default Description
color color 0 The color of the background.

<stroke>

Adds a border to the shape.

Attributes

Name Type Default Description
width dimension 0 The width of the stroke.
color color 0 The color of the stroke.
dashWidth dimension 0 The width of each dash. Ignored unless dashGap is also defined.
dashGap dimension 0 The width of gaps between eahc dash. Ignored unless dashWidth is also defined.

<corners>

Adds rounded corners to the shape.

Attributes

Name Type Default Description
radius dimension 0 The radius of every corner.
topLeftRadius dimension radius Determines the radius of the top left corner. Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes.
topRightRadius dimension radius Determines the radius of the top right corner. Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes.
bottomLeftRadius dimension radius Determines the radius of the bottom left corner (buggy; is actually bottom right corner). Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes.
bottomRightRadius dimension radius Determines the radius of the bottom right corner (buggy; is actually bottom left corner). Ignored unless radius for all corners is defined, either through 'radius' or the other corners' attributes.

<padding>

The padding for the content within this drawable. (Does not pad graphics in any way.)

Attributes

Name Type Default Description
left dimension 0 Left padding.
top dimension 0 Top padding.
right dimension 0 Right padding.
bottom dimension 0 Bottom padding.

转载于:https://www.cnblogs.com/kunyashaw/p/4364699.html

你可能感兴趣的文章
install ubuntu tweak on ubuntu lts 10.04,this software is created by zhouding
查看>>
Objective-C中一种消息处理方法performSelector: withObject:
查看>>
JSP实现分页显示
查看>>
关注HTML5安全
查看>>
ios中Pldatabase的用法(4)
查看>>
Leetcode: Search in Rotated Sorted Array
查看>>
windows上配置git
查看>>
新型智能芯片nxp----嗯质朴
查看>>
使用事件捕获实时捕获img是否加载完毕, 实现iframe内容高度自动适应
查看>>
Mysql 分组排序
查看>>
论文撰写及排版流程总结
查看>>
Underscore.js (1.7.0)-函数预览
查看>>
Sublime 插件补充
查看>>
word2013 如何套用模版
查看>>
Objective-C 与JAVA的SHA1/HmacSHA1加密算法实现
查看>>
Android Gradle 自定义Task 详解
查看>>
【nodejs】FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
查看>>
基于.net2 的CAD 绘图控件virtualGraph
查看>>
heroku logs乱码
查看>>
史上最全最强SpringMVC详细示例实战教程
查看>>