【读书笔记】【Android 开发艺术探索】第 6 章 Android 的 Drawable

来源:互联网 发布:建筑工程网络计划图 编辑:程序博客网 时间:2024/06/06 13:18

Drawable 常被用来作为 View 的背景使用,一般使用 XML 文件定义。Drawable 的内部宽/高,可以通过 getIntriniscWidth 和 getIntrinscHeight 获取。


     分类

        常用的

BitmapDrawable

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"     android:src="@mipmap/Rmipmap.ic_laucher"   /> <!--图片的资源-->    android:antialias="true | false"   <!--抗锯齿功能,让图片变得平滑,但是会降低图片的清晰度-->    android:dither="true | flase"  <!--抖动效果,让高质量的图片在低质量的屏幕上保持更好的显示效果-->    android:filter="true"<!--过滤效果,当图片被拉伸或者压缩时,开启后可以保持较好的显示效果-->    android:gravity="top"    android:mipMap="true"  <!--纹理映射,不常用,可忽略-->    android:tileMode="disabled | clamp | repeat |  mirror">   <!--平铺模式,开启后,gravity 属性会被忽略--></bitmap>

ShapeDrawable

  shapeDrawable 通过颜色来构造的图像,它既可以是纯色的图形,也可以是具有渐进效果的图形        

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle | line | oval | ring"> <!--形状-->    <corners  <!-- 圆角的程度-->        android:radius="integer"        android:topLeftRadius=""        android:topRightRadius=""        android:bottomLeftRadius=""        android:bottomRightRadius=""        />    <gradient    <!--渐变效果-->        android:angle="integer"  <!--渐变的角度,默认值为0, 其实数值必须为 45 的倍数, 0表示从左到右,90 表示从上到下-->        android:centerY="integer"   <!--渐变中心点的 Y 坐标-->        android:centerX="integer"    <!--渐变中心点的 X 坐标-->        android:startColor="color"        android:centerColor="color"        android:endColor="color"        android:gradientRadius="integer"    <!--渐变半径, 仅当 android:type="radial" 时有效-->        android:type="linear | radial | sweep"  <!--渐变类别-->        android:useLevel="true | false"/>  <!--一般为 false, 当 drawable 作为 StateListDrawable 使用时为 true-->    <padding   <!--空白-->        android:left="integer"        android:top="integer"        android:right="integer"        android:bottom="integer"/>    <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>

LayerDrawable 

          对应的标签是<Layer-list>, 表示一种层次的 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>        <shape android:shape="rectangle">            <solid android:color="#00ffff"/>        </shape>    </item>    <item        android:bottom="1dp">        <shape  android:shape="rectangle">            <solid android:color="#ffffff"/>        </shape>    </item></layer-list>


         剩下的Drawable 在使用的时候可以去官网了解

 StateListDrawable <selector>,  主要用于设置可单击的 View 的背景

  LevelListDrawable <level-list>,    

TransitionDrawable<transition>, 用于实现两个 Drawable 之间的淡入淡出效果

InsertDrawable<insert>  , 它可以将其他 Drawable 内嵌到自己当中,并可以在四周流出一定的间距。当一个 View 希望自己的背景比自己的实际区域小的时候,可以采用 InsertDrawable.

<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android"    android:insetBottom="15dp"    android:insetLeft="15dp"    android:insetRight="15dp"    android:insetTop="15dp" >    <shape android:shape="rectangle" >        <solid android:color="#ff0000" />    </shape></inset>
ScaleDrawable <scale>

ClipeDrawable <clip>

0 0
原创粉丝点击