Android的Drawable

Drawable简介 

 1.Drawable的优点

  • 它使用简单,比自定义View的成本要低;
  • 其次,非图片类型的Drawable占用空间较小,这对减小apk的大小也很有帮助。

 2.Drawable是没有大小概念的,当用作View的背景时,Drawable会被拉伸至View的同等大小 

常用Drawable

1.BitmapDrawable
  最简单的Drawable了,它表示的就是一张图片。在实际开发中,我们可以直接引用原始的图片即可,但是也可以通过XML的方式来描述它,通过XML来描述的BitmapDrawable可以设置更多的效果

<?xml version="1.0" encoding="utf-8"?>
<bitmap
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:src="@[package:]drawable/drawable_resource"
		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"]
/>

属性说明:
  antialias
    是否开启图片抗锯齿功能。开启后会让图片变得平滑,同时也会在一定程度上降低图片的清晰度,但是这个降低的幅度较低以至于可以忽略,因此抗锯齿选项应该开启
  dither
    是否开启抖动效果,应该开启
  filter
    是否开启过滤效果。当图片尺寸被拉伸或者压缩时,开启过滤效果可以保持较好的显示效果,因此此选项也应该开启。
  gravity
    当图片小于容器的尺寸时,设置此选项可以对图片进行定位。这个属性的可选项比较多,不同的选项可以通过“|”来组合使用
  mipMap
    这是一种图像相关的处理技术,也叫纹理映射,比较抽象,这里也不对其深究了,默认值为false,
  tileMode
    平铺模式
     disable表示关闭平铺模式,这也是默认值,当开启平铺模式后,gravity属性会被忽略
     repeat、mirror和clamp的区别,这三者都表示平铺模式,但是它们的表现却有很大不同。
     repeat表示的是简单的水平和竖直方向上的平铺效果;
     mirror表示一种在水平和竖直方向上的镜面投影效果;
     clamp表示的效果就更加奇特,图片四周的像素会扩展到周围区域 

2.ShapeDrawabl

<?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:radius="integer"
    android:topLeftRadius="integer"
    android:topRightRadius="integer"
    android:bottomLeftRadius="integer"
    android:bottomRightRadius="integer" />
  <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"] />
  <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>

属性说明

  corners 

    表示shape的四个角的角度。它只适用于矩形shape,这里的角度是指圆角的程度,用px来表示shape的四个角的角度。
     android:radius——为四个角同时设定相同的角度,优先级较低,会被其他四个属性覆盖;
     android:topLeftRadius——设定最上角的角度;
     android:topRightRadius——设定右上角的角度;
     android:bottomLeftRadius——设定最下角的角度;
     android:bottomRightRadius——设定右下角的角度。
  gradient
    它与<solid>标签是互相排斥的,其中solid表示纯色填充,而gradient则表示渐变效果,gradient有如下几个属性:
     android:angle——渐变的角度,默认为0,其值必须为45的倍数,0表示从左到右,90表示从下到上,具体的效果需要自行体验,总之角度会影响渐变的方向;
     android:centerX——渐变的中心点的横坐标;
     android:centerY——渐变的中心点的纵坐标,渐变的中心点会影响渐变的具体效果;
     android:startColor——渐变的起始色;
     android:centerColor——渐变的中间色;
     android:endColo——渐变的结束色;
     android:gradientRadius——渐变半径,仅当android:type= "radial"时有效;
     android:useLevel——一般为false,当Drawable作为StateListDrawable使用时为true;
     android:type——渐变的类别,有linear(线性渐变)、radial(径向渐变)、
     sweep(扫描线渐变)三种,其中默认值为线性渐变,
  solid
    这个标签表示纯色填充,通过android:color即可指定shape中填充的颜色
  stroke
     android:width——描边的宽度,越大则shape的边缘线就会看起来越粗;
     android:color——描边的颜色;
     android:dashWidth——组成虚线的线段的宽度;
     android:dashGap——组成虚线的线段之间的间隔,间隔越大则虚线看起来空隙就越大。
  注意如果android:dashWidth和android:dashGap有任何一个为0,那么虚线效果将不能生效
  padding
    这个表示空白,但是它表示的不是shape的空白,而是包含它的View的空白,有四个属性:android:left、android:top、android:right和android:bottom。
  size
     <size>标签设置的宽/高就是ShapeDrawable的固有宽/高,但是作为View的背景时,shape还会被拉伸或者缩小为View的大小。


3.LayerDrawable

LayerDrawable对应的XML标签是<layer-list>,它表示一种层次化的Drawable集合,通过将不同的Drawable放置在不同的层上面从而达到一种叠加后的效果。它的语法如下所示。

<?xml version="1.0" encoding="utf-8"?>
<layer-list
	xmlns:android="http://schemas.android.com/apk/res/android">
	<item
		android:drawable="@[package:]drawable/drawable_resource"
		android:id="@[+][package:]id/resource_name"
		android:top="dimension"
		android:right="dimension"
		android:bottom="dimension"
		android:left="dimension" />
</layer-list>

   Layer-list有层次的概念,下面的item会覆盖上面的item,通过合理的分层,可以实现一些特殊的叠加效果,下面是一个例子:

<?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>

 4.StateListDrawable

StateListDrawable对应于<selector>标签,它也是表示Drawable集合,每个Drawable都对应着View的一种状态,这样系统就会根据View的状态来选择合适的Drawable。StateListDrawable主要用于设置可单击的View的背景,最常见的是Button。点击Button的背景变色。

<selector xmlns:android="http://schemas.android.com/apk/res/android"
	android:constantSize=["true" | "false"]
	android:dither=["true" | "false"]
	android:variablePadding=["true" | "false"] >
<item
	android:drawable="@[package:]drawable/drawable_resource"
	android:state_pressed=["true" | "false"]
	android:state_focused=["true" | "false"]
	android:state_hovered=["true" | "false"]
	android:state_selected=["true" | "false"]
	android:state_checkable=["true" | "false"]
	android:state_checked=["true" | "false"]
	android:state_enabled=["true" | "false"]
	android:state_activated=["true" | "false"]
	android:state_window_focused=["true" | "false"] />
</selector>

 属性说明:

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

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:state_pressed="true"
	    android:drawable="@drawable/button_pressed" /> <!--pressed -->
	<item android:state_focused="true"
	     android:drawable="@drawable/button_focused" /> <!--focused -->
	<item android:drawable="@drawable/button_normal" /> <!--default -->
</selector>

 
本文内容为《Android开发艺术探索》内容,电子版下载地址

 

转载于:https://www.cnblogs.com/ZeGod/p/10195861.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值