Drawable
概述
- Drawable是一个抽象类,是对可绘制物体的抽象.与View不同的是Drawable没有事件和交互方法
- Drawable 有好多子类操作具体类型资源,比如
- BitmapDrawable是用来操作位图
- ColorDrawable用来操作颜色
- ClipDrawable用来操作剪切板
原理
- draw(Canvas canvas)
- setBounds()
BitmapDrawable
是对bitmap的一种包装,可一设置它包装的bitmap在BitmapDrawable区域内的绘制方式 如平铺填充,拉伸填充或者保持图片原始大小
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_pause" //不支持XML图片
android:tileMode="mirror"
android:antialias="true"
android:dither="true">
</bitmap >
LayerDrawable
可以管理一组drawable对象在LayerDrawable的drawable资源按照列表的顺序绘制,列表的最后一个drawable绘制在上层
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="20dp"
android:top="20dp">
<bitmap android:src="@drawable/ic_pause" />
</item>
<item
android:gravity="center"
android:left="50dp">
<bitmap android:src="@drawable/ic_pause" />
</item>
</layer-list>
StatusListDrawable
就是selector
LevelListDrawable
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_pause"
android:maxLevel="10"
android:minLevel="6" />
<item
android:drawable="@drawable/ic_start"
android:maxLevel="20"
android:minLevel="11" />
</level-list>
iv_name.setImageLevel(8); //根据设置不同的level显示不同的图片
TransitionDrawable
它只负责管理两层drawable,并且提供了一个透明度变化的动画,可以控制从一层drawable过度到另一层drawable的动画效果。
适合lian两幅同样的图片但是颜色不一样 显示过度效果
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item //始终显示的图片
android:drawable="@drawable/ic_start" />
<item //渐变显示的图片
android:drawable="@drawable/ic_pause" />
</transition>
TransitionDrawable drawable = (TransitionDrawable) iv_name.getDrawable();
drawable.reverseTransition(1000);
InsertDrawable
效果和 pading差不多
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_launcher_background"
android:insetLeft="25dp" //效果和 pading差不多
android:insetBottom="25dp"
android:insetRight="25dp"
android:insetTop="25dp">
</inset>
ClipDrawable
- ClicpDrawable是对Drawable尽享剪切操作,控制剪切区域
- android中的进度条就是使用ClipDrawable实现效果的,它根据level的属性值,决定剪切区域的大小
- ClipDrawable通过setLevel() 方法来设置剪切区域,level从0到1000,当为0时完全不显示,为10000时完全显示
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_start"
android:clipOrientation="horizontal"
>
</clip>
ClipDrawable background = (ClipDrawable) iv_name.getDrawable();
background.setLevel(6000);
自定义Drawable
public class CircleDrawable extends Drawable {
private Paint mPaint;
private int mWidth;
private Bitmap mBitmap;
private final BitmapShader bitmapShader;
public CircleDrawable(Bitmap mBitmap) {
this.mBitmap = mBitmap;
//着色器
bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(bitmapShader);
mWidth = Math.min(mBitmap.getHeight(), mBitmap.getWidth());
}
@Override
public void draw(@NonNull Canvas canvas) {
canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2,mPaint);
}
@Override
public void setAlpha(int i) {
mPaint.setAlpha(i);//设置画笔的透明度
}
@Override //设置画笔的ColorFilter
public void setColorFilter(@Nullable ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public int getIntrinsicWidth() {
return mWidth;
}
@Override
public int getIntrinsicHeight() {
return mWidth;
}
}
ImageView iv_name = findViewById(R.id.iv_name);
CircleDrawable circleDrawable = new CircleDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.th));
iv_name.setImageDrawable(circleDrawable);
public class Rect1Drawable extends Drawable {
private Paint mPaint;
private Bitmap mBitmap;
private RectF rect;
private final BitmapShader bitmapShader;
public Rect1Drawable(Bitmap mBitmap) {
this.mBitmap = mBitmap;
//着色器
bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(bitmapShader);
}
@Override
public void draw(@NonNull Canvas canvas) {
canvas.drawRoundRect(rect,60,60,mPaint);
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
rect=new RectF(left, top, right, bottom);
}
@Override
public void setAlpha(int i) {
mPaint.setAlpha(i);//设置画笔的透明度
}
@Override //设置画笔的ColorFilter
public void setColorFilter(@Nullable ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public int getIntrinsicWidth() {
return mBitmap.getWidth();
}
@Override
public int getIntrinsicHeight() {
return mBitmap.getHeight();
}
}