1. InsetDrawable
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/shape_line"
android:insetLeft="24dp"
android:insetRight="24dp">
</inset>
- inset翻译为插进,这里左插进24,右插进24.可以做左缩进的line.
2. ScaleDrawable
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/shape"
android:scaleHeight="70%"
android:scaleWidth="70%">
</scale>
findViewById(R.id.scale).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ScaleDrawable drawable = (ScaleDrawable) findViewById(R.id.scale).getBackground();
drawable.setLevel(drawable.getLevel()+100);
}
});
- ScaleDrawable可以根据自己的等级(level)缩放一定比例
- level=0是无法显示的
@Override
public void draw(Canvas canvas) {
final Drawable d = getDrawable();
if (d != null && d.getLevel() != 0) {
d.draw(canvas);
}
}
- scaleWidth是缩放比例,具体换算如下
w -= (int) ((w - iw) * (MAX_LEVEL - level) * mState.mScaleWidth / MAX_LEVEL);
由于iw一般为0,上面的简化为
w -= (int)(w*(MAX_LEVEL-level)*mState.mScaleWidth/MAX_LEVEL);
level越大,w越大。
3. ClipDrawable
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="@drawable/logo"
android:gravity="top">
</clip>
ClipDrawable clipDrawable = (ClipDrawable) findViewById(R.id.clip).getBackground();
clipDrawable.setLevel(5000);
- ClipDrawable可以根据自己当前的等级来裁剪另一个drawable.
- clipOrientation: 裁剪方向
- gravity:
- 如果方向为水平,则left,right,center_horizontal有效。
- 如果方向为竖直,则top,bottom,center_vertical。
- center,在水平时,同center_horizontal.竖直时,同center_vertical.
- fill_vertical和fill_horizontal会填充view的大小,压缩或拉伸。
4. 一一对应
Drawable和drawable中的标签都是一一对应的。如:
- BitmapDrawable和bitmap标签
- ShapeDrawable和shape标签
- LayerDrawable和layer-list标签
- StateListDrawable和selector标签
- LevelListDrawable和level-list标签
- TransitionDrawable和transition标签
- InsetDrawable和inset标签
- ScaleDrawable和scale标签
- ClipDrawable和clip标签