美文网首页
android的drawable

android的drawable

作者: 著名的 | 来源:发表于2017-07-31 22:47 被阅读9次

drawable在android中主要的作用是作为背景或者直接用于imageView。使用好drawable对开发项目很有帮助。
下面介绍下android中的drawable及其用法:

1、BitmapDrawable

它表示一张图片。在实际开发中,我们可以直接引用原始的图片资源即可,但是也可以通过XML的方法来描述它,通过XML来描述的BitmapDrawable可以设置更多的效果,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@mipmap/ic_launcher" 
    android:antialias="true|false" 
    android:dither="true|false" 
    android:filter="true|false"         
    android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal" 
    android:mipMap="true|false" 
    android:tileMode="disabled|clamp|repeat|mirror"/>

它各个属性的含义如下:

android:src
图片的资源id

android:antialias
是否开启图片的抗锯齿功能。会一定程序的降低图片的清晰度,但是这个降低的幅度较低以至于可以忽略。

android:dither
是否开启抖动效果。当图片的像素配置和手机屏幕的像素配置不一致时,开启这个选项可以让高质量的图片在低质量的屏幕上还能保持较好的显示效果,比如图片的色彩模式为ARG8888,但是设备屏幕所支持的色彩模式为RGB555,这个时候开启抖动选项可以让图片显示不会过于失真。在Android中创建的Bitmap一般会选用ARG8888这个模式,即ARGB四个通道各占8位,在这种色彩模式下,一个像素所占的大小为4个字节,一个像素的位数总和越高,图像也就越逼真。

android:filter
是否开启过滤效果。当图片尺寸被拉伸或者压缩时,开启过滤效果可以保持较好的显示效果。

android:gravity
没什么好说的

android:mipMap
这是一种图象相关的处理技术,也叫纹理映射。知道有这么个东西就好了。。。

android:tileMode
平铺模式。这个选项有几个值:disabled、clamp、repeat、mirror。
这里要注意开启平铺模式gravity属性会被忽略。其中disabled是关闭,clamp是扩展,mirror是镜像,repeat是重复。

2、ShapeDrawable

ShapeDrawable是一种很常见的Drawable,可以理解为通过颜色来构造的图形,它既可以是纯色的图形,也可以是渐变效果的图形。ShapeDrawable的语法稍显复杂,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle|oval|line|ring">
    <corners
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer"
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerColor="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type="linear|radial|sweep"
        android:useLevel="true|false" />
    <padding
        android:bottom="integer"
        android:left="integer"
        android:right="integer"
        android:top="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashGap="integer"
        android:dashWidth="integer" />
</shape>

android:shape
表示图形形状:rectangle(矩形)、oval(椭圆)、line(横线)、ring(圆环),默认矩形,
line和ring必须有< stroke>标签来指定宽度和颜色,否则达不到预期效果。
< gradient>
渐变效果,与< solid>标签互斥
< solid>
纯色填充 通过android:color即可指定shape的颜色
< padding>
这个表示空白,但是它表示的不是shape的空白,而是包含它的view的空白,有四个属性:left、top、right、bottom。

3、LayerDrawable

表示的是一种层次化的Drawable集合,通过将不同的Drawable放置在不同的层上面从而达到一种叠加后的效果。语法如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#0ac39e" />
        </shape>
    </item>
    <item android:bottom="6dp">
        <shape android:shape="rectangle">
             <solid android:color="#ffffff" />
         </shape>
     </item>
     <item
         android:bottom="1dp"
         android:left="1dp"
         android:right="1dp">
         <shape android:shape="rectangle">
             <solid android:color="#ffffff" />
         </shape>
     </item>
</layer-list>

一个layer-list可以包含多个item,每个item代表一个drawable,item的结构也比较简单,常用属性有left、right、to、bottom分别表示drawable相对于View的上下左右的偏移量,单位为像素。默认情况下,layer-list中的所有的drawable都会被缩放到View的大小,对于bitmap来说,需要使用gravity属性才能控制图片的显示效果。layer-list有层次的概念,下面的item会覆盖上面的item,通过合理的分层,可以实现特殊的叠加效果。

4、StateListDrawable

StateListDrawable对应于<selector>标签,它也表示drawble集合,每个drawable对应着View的一种状态,这样系统就会根据View的状态来选择合适的drawble。

android:constantSize
StateListDrawable的固有大小是否不随着其状态的改变而改变的,因为状态的改变会导致StateListDrawable切换到具体的Drawable,而不同的Drawable具有不同的固有大小。True表示StateListDrawable的固有大小保持不变,这时它的固有大小是内部所有Drawable的固大小的最大值,false则会随着状态的改变而改变。此选项默认值为false。

android:dither
是否开启抖动效果,这个在BitmapDrawable中也有提到,开启此选项可以让图片在低质量的屏幕上仍然获得较好的显示效果。此选项默认值为true。

android:variablePadding
StateListDrawable的padding表示是否随着其状态的改变而改变,true表示会随着状态的改变而改变,false表示StateListDrawable的padding是内部所有Drawable的padding的最大值。此选项默认值为false,并且不建议开启此选项。

5、LevelListDrawable

LevelListDrawable对应于<level-list>标签,它同样表示一个drawable集合,集合中的每个drawable都有一个等级(level)的概念。根据不同的等级,LevelListDrawable会切换为对应的drawable,它的语法如下所示。

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shapedrawable"
        android:maxLevel="0"/>
    <item android:drawable="@drawable/bitmapdrawable"
        android:maxLevel="1"/>
</level-list>

6、TransitionDrawable

对应于<transition>标签,它用于实现两个drawable之间的淡入淡出效果。

相关文章

网友评论

      本文标题:android的drawable

      本文链接:https://www.haomeiwen.com/subject/osrdlxtx.html