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>

更多相关文章

  1. Android 设置图片倒影效果
  2. Android中按钮点击效果显示
  3. Android有进度条的下载图片并且显示图片
  4. android开源新闻小程序、3D翻转公告效果、小说检索、Kotlin开发T
  5. Android智能推荐、MVP架构电商应用、markdown编辑器、多种动画效
  6. Android 实现开关灯效果
  7. android 改变 listView gridView的默认点击效果
  8. Android Button的背景图片拉伸变形解决方法
  9. Android 拖拽效果实现代码分享 - ORIETech

随机推荐

  1. This text field does not specify an in
  2. 在Android(安卓)Studio 编写应用程序,在模
  3. Android布局属性大全
  4. 【Android】持续收集
  5. 「Android」基于轻量级Messenger的进程通
  6. Android中RelativeLayout各个属性的含义
  7. Android(安卓)Studio记录一个错误:java.la
  8. Android(安卓)中自定义View(一)
  9. Android(安卓)EditText输入光标居于开头
  10. Android编译过程详解(一)