Android中的Drawable使用

来源:互联网 发布:nginx 点播 编辑:程序博客网 时间:2024/05/16 12:27

Drawable是一种图像的概念 但她又不全是图片 可以通过颜色来构造出一些图案,通常是用XML文件定义的 种类很多 可以通过getIntrinsicHeight和getIntrinsicWidth获取 通常来说Drawable是没有大小概念的 BitmapDrawable是有大小的 大小就是图片的宽高 下面我就来总结下 Drawable的各种类型的使用。

Drawable的分类

  1. 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"  //抗锯齿  一般设置true 开启后可以使图片平滑    android:dither="true"  //是否开启抖动效果  一般开启  可以使图片更适配不同设备    android:filter="true" //是否开启滤镜效果  当图片被拉伸 或者压缩 开启滤镜可以保持较好的显示 所以建议开启     android:gravity="bottom" //当图片小鱼容器尺寸时  图片的位置      android:mipMap="true"  //一种图像相关的处理技术 也叫纹理映射  默认为false 通常不用    android:tileMode="repeat"  //平铺的模式  有repeat  mirror  clamp  disabled几种  分别代表 简单的水平竖直平铺  在水平和竖直方向上的镜面投影效果  图片四周的像素会或占到周围区域   最后一个无平铺      android:alpha="0.5"  //透明度  0为完全透明  1为完全不透明     android:tint="#f0f"  //色彩      android:tintMode="add"  //色彩的模式      ></bitmap>

其中还可以使用 .9图片

<nine-patchandroid:src="@mipmap/ic_launcher"android:dither="true"    ></nine-patch>
  1. ShapeDrawable
    这个我平时使用较多 虽然在android5.0之后 提供了圆角图片的控件 只需要包裹一下即可 但是也不影响我们使用这个
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle"  //图片的形状 有矩形 椭圆 横线 圆环    >    <corners android:radius="15dp"></corners> //表示图片圆角的角度  可分别设置 4个方向    `<solid android:color="#f00"></solid>  //图片的填充颜色    <padding android:bottom="15dp"></padding>  //距离View的padding    <stroke android:color="#f0f" android:width="5dp" android:dashGap="5dp" android:dashWidth="5dp"></stroke> //边线  上述几个属性 分别是:  边线颜色  边线宽度  虚线线段间的间隔  虚线线段的宽度     <gradient android:angle="0" android:startColor="#f00" android:centerColor="#ff0" android:endColor="#fff"        android:centerX="0.5"        android:centerY="0.5"        android:gradientRadius="15dp"        android:type="linear"        android:useLevel="false"        ></gradient>  //填充渐变颜色 与solid是相互排斥的  设置了这个solid就会无效  上述属性分别是:angle渐变角度(默认是0从左向右渐变 必须是45倍数 如果是90度 那就是上下渐变 180度就是从右向左渐变)  开始渐变的颜色  中间的颜色  结束的颜色  渐变的中心X和Y坐标   渐变的半径(当type=radial时有效) 渐变的类别(线性 径向 扫描线渐变) useLevel通常为false,当Drawable作为selector时为true       </shape>
  1. LayerDrawable
    对应xml标签 表示一种层次化的drawable集合 将不同的drawable放置在不同的位置上达到一种叠加的效果 比如Progress的背景 就是用这个实现的
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:width="200dp"  //宽度        android:height="50dp"  //高度        android:drawable="@drawable/shape_draw"  //图片        ></item>    <item android:width="180dp"        android:height="40dp"        android:drawable="@drawable/shape_draw"        android:top="5dp"  //距离图片上面的高度        android:bottom="5dp"        android:left="10dp"        android:right="10dp"        ></item></layer-list>
  1. StateListDrawable
<selector xmlns:android="http://schemas.android.com/apk/res/android"    android:constantSize="true" //固有大小是否随着其状态的改变而改变 因为状态的改变会使其切换到不同的drawable 而每个drawable有不同的大小 true表示即使切换后固有大小也不变  默认为false    android:dither="true"  //是否开启抖动效果  可以在低质量的屏幕上也有较好的显示效果    android:variablePadding="false"  //padding是否会随状态改变  默认为false      >    <item android:drawable="@drawable/shape_draw" android:state_pressed="true"></item>    <item android:drawable="@drawable/shape_draw" android:state_pressed="false"></item></selector>
  1. LevelListDrawable
    对应于标签 同样也代表一个图片集合 集合中的每个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/shape_draw" android:maxLevel="10" android:minLevel="0"></item></level-list>

切换的level范围在min和max范围内 当当作背景时 可以通过Drawable的setLevel的方法来设置等级切换不同的Drawable 如果当作ImageView的前景色 可以通过ImageView的setImageLevel方法来设置 范围是0-10000

  1. TransitionDrawable
    对应于标签 用于实现2个Drawable之间的淡入淡出
<transition xmlns:android="http://schemas.android.com/apk/res/android">    <item>        <bitmap android:src="@mipmap/bg11" android:alpha="0"></bitmap>    </item>  //此处实现效果是想bg11从无到有的淡入进来  第一张图片为透明度为0的图片    <item android:drawable="@mipmap/bg11"></item> //第二章图片为最终要显示的 </transition>

必须要有2张图片 否则会报空指针
Activity中这样调用即可

RelativeLayout aaa = (RelativeLayout) findViewById(R.id.aaa);        TransitionDrawable background = (TransitionDrawable) aaa.getBackground();        background.startTransition(2000);
  1. InsetDrawable
    对应于标签 当一个View希望自己的背景比自身的大小小的时候可以使用这个属性 不过貌似好多Drawable都可以实现这一效果。。。。
<inset xmlns:android="http://schemas.android.com/apk/res/android"    android:inset="20dp"    android:drawable="@mipmap/bg11"    ></inset>
  1. ScaleDrawable
    对应于标签
<scale xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/bg11"  //需要缩放的图片    android:scaleHeight="70%"  //缩小的大小  表示最终为原图片的30%    android:scaleWidth="70%"    android:scaleGravity="center"  //图片缩放后在控件中的位置    ></scale>

需要注意的是 ScaleDrawable的使用需要设置等级 否则不显示 等级需要在1-10000 通常设置为1即可

RelativeLayout aaa = (RelativeLayout) findViewById(R.id.aaa);        ScaleDrawable background = (ScaleDrawable) aaa.getBackground();        background.setLevel(1);  //这个不设置 图片不显示
  1. ClipDrawable
    对应于标签 可以根据自己当前的等级来裁剪另一个drawable 裁剪方向通过 clipOrientation 和 gravity决定
<clip xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/bg11"    android:clipOrientation="vertical"    android:gravity="bottom"      > //表示竖直方向裁剪  从顶部开始裁剪  bottom表示的是  保留下面的部分 最后显示的靠下</clip>

这个需要注意的是 需要设置setLevel 0-10000 0表示完全裁剪 10000表示不裁剪 7000表示裁剪十分之三 裁剪之后的部分依然存在

//        RelativeLayout aaa = (RelativeLayout) findViewById(R.id.aaa);//        ClipDrawable background = (ClipDrawable) aaa.getBackground();//        background.setLevel(8000);        image = (ImageView) findViewById(R.id.image);        ClipDrawable drawable = (ClipDrawable) image.getDrawable();        drawable.setLevel(7000);
0 0