Android 使用xml定义Drawable--第一篇:概述以和Shape Drawable示例
2016-03-29 11:24
525 查看
简介:
A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon. There are several different types of drawables: - 图片资源是可以画在屏幕上图形。你可以通过API getDrawable(int)方法获取图片对象或者通过属性android:drawable、android:icon来使用他们。他们有不同的类型:
- Bitmap File: A bitmap graphic file (.png, .jpg, or .gif). Creates a BitmapDrawable. Bitmap 文件: 一个图形文件(可以是png、jpg、gif文件、xml),使用BitmapDrawable来获取
- Nine-Patch File: A PNG file with stretchable regions to allow image resizing based on content (.9.png). Creates a NinePatchDrawable. .9文件: png文件:可以定义伸缩改变图片大小的部位(后缀是.9.png),使用NinePatchDrawable获取
- Layer List A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index is be drawn on top. Creates a LayerDrawable. 图层: 由多个图片组合成一个图片。他们按顺序被画出,所以最后一个图片被画在最顶层,使用LayerDrawable获取
- State List An XML file that references different bitmap graphics for different states (for example, to use a different image when a button is pressed). Creates a StateListDrawable. 状态资源: 一个Xml,用来在不同的状态下显示不同的Bitmap对象。
- Level List An XML file that defines a drawable that manages a number of alternate Drawables, each assigned a maximum numerical value. Creates a LevelListDrawable.
- Transition Drawable An XML file that defines a drawable that can cross-fade between two drawable resources. Creates a TransitionDrawable. 动画图像资源: XML定义两个图片之间过度的效果,使用TransitionDrawable获取
- Inset Drawable An XML file that defines a drawable that insets another drawable by a specified distance. This is useful when a View needs a background drawble that is smaller than the View's actual bounds. 嵌入图片资源: 当一个View需要的背景图小于View的实际大小使用Inset Drawable。 一个XML文件定义一个Drawable嵌入到另一个Drawable,并且定义特定的距离
- Clip Drawable An XML file that defines a drawable that clips another Drawable based on this Drawable's current level value. Creates a ClipDrawable. 裁剪图片资源: 一个XML文件定义一个Drawable,通过裁剪另外一个Drawable。 通过ClipDrawable获取
- Scale Drawable An XML file that defines a drawable that changes the size of another Drawable based on its current level value. Creates a ScaleDrawable 缩放图片资源: 一个Xml文件定义缩放的大小,通过ScaleDrawable获取
- Shape Drawable An XML file that defines a geometric shape, including colors and gradients. Creates a ShapeDrawable. Also see the Animation Resource document for how to create an AnimationDrawable. 图形图片资源: XML定义各种几何图形,包括颜色、坡度。通过ShapeDrawable获取。 也可以参考AnimationDrawable类
代码:
Shape Drawable示例:
- <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] android:innerRadius="integer" android:innerRadiusRatio="integer" android:thickness="integer" android:thicknessRatio="integer" android:useLevel="boolean" > <!--矩形、椭圆、直线、圆环 <Shape>节点后五个属性只有当shape="ring"时才有效 innerRadius:圆环内圆半径、innerRadiusRatio:圆环内圆半径比值、 thickness:圆环的宽窄(外圆半径减去内圆半径)、useLevel:为false,才显示圆环,true则变成levelListDrawable--> <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> <!-- corners:适用于矩形rectangle。用来设置4个角的半径 --> <gradient android:angle="integer" android:centerX="integer" android:centerY="integer" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:useLevel=["true" | "false"] /> <!--angle:当type="linear"时有效,只用于现行渐变,指定现行渐变的角度且必须为45的倍数、centerX(centerX):设置渐变的中心坐标(取值0-1)、 startColor(endColor):起始渐变色(结束渐变色)、type:linear(线性渐变),radial(径向渐变),sweep(扫描渐变)、useLevel:默认false,true是levelListDrawable--> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <!-- 设置与该Shape显示的内容和Shape边界的距离--> <size android:width="integer" android:height="integer" /> <!--矩形时,设置矩形的宽高; 椭圆时,设置椭圆外切矩形的宽高,当宽与高相等时,为正圆,否则为椭圆; 直线时,宽代表了直线的长度,高没有效果;圆环时,宽会影响内圆半径和圆环的宽窄,高没有效果 --> <solid android:color="color" /> <!-- 填充颜色 --> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> <!-- Shape 的边框。dashWidth:实线的长度、dashGap:虚线的长度--> </shape>
效果图如下:
代码如下:
布局文件<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv3"> <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:background="@drawable/shape_rectangle_2" android:gravity="center" android:text="矩阵示例" android:textSize="20sp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_gravity="center" android:background="@drawable/shape_oval_2" android:gravity="center" android:text="999" android:textColor="@android:color/white" android:textSize="50sp" /> </FrameLayout>
Drawable文件:
矩阵:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="5dp"/> <padding android:top="4dp" android:bottom="4dp" android:left="4dp" android:right="4dp"/> <stroke android:width="3dp" android:color="@android:color/white" android:dashGap="1dp" /> <solid android:color="#AAAAAA"/> <size android:width="250dp" android:height="150dp"/> </shape>
椭圆
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:innerRadius="40dp" android:thickness="10dp" android:useLevel="false"> <padding android:top="4dp" android:bottom="4dp" android:left="4dp" android:right="4dp"/> <stroke android:width="2dp" android:color="@android:color/white" android:dashGap="1dp" android:dashWidth="4dp"/> <size android:width="120dp" android:height="120dp"/> <solid android:color="@android:color/holo_red_light"/> </shape>
相关文章推荐
- android中id与+id
- mac配置android和maven环境变量
- Android开发IDE插件集合----selector
- android ViewHolder简洁写法 实用
- 关于android中的通信方式
- AndroidManifest.xml
- Android中dp,px,sp概念
- Android四大组件之BroadcastReceiver
- Android 多重CPU兼容的注意事项
- 欢迎使用CSDN-markdown编辑器
- Android 获取View的宽高
- Android Studio NDK开发
- [Android基础]Android总结篇
- Androidstudio点9图报错
- Android 状态栏工具类(实现沉浸式状态栏/变色状态栏)
- Android小记:eclipse下使用git上传(下载)代码至(从)github
- Android 绘图XML——Shape
- android自定义view-打造圆形ImageView(一)
- 自己实现一个PullToZoomListView放大回弹效果,PullToZoomView源码解析
- Android——屏幕尺寸