1.BitmapDrawable
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
<!--图片的资源 -->
android:src="@drawable/create"
<!--是否抗锯齿-->
android:antialias="true"
<!--是否开启抖动效果-->
android:dither="true"
<!--开启过滤效果-->
android:filter="true"
<!--图片大小和容器不符合时图片摆放位置,拉伸,裁剪-->
android:gravity="
<!--图片大小不发生变化-->
top|bottom|left|right
|center_vertical|center_horizontal|center
<!--图片大小发生变化-->
|fill_vertical|fill_horizontal|fill
|clip_vertical|clip_horizontal"
<!--纹理映射,图像处理技术,一般用不到-->
android:mipMap="false"
<!--平铺模式,开启此属性gravity会失效
disabled 关闭
clamp 图片四周像素会扩散到周围区域
repeat 简单的水平和竖直方向重复图片
mirror 图片倒立重复图片
-->
android:tileMode=" ["disabled"|"clamp"|"repeat"|"mirror"]"
>
</bitmap>
2.NinePatchDrawable
点9图可以自动根据所需的宽高进行相应的缩放并保证不失真
属性基本和BitmapDrawable相同
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/create"
>
</nine-patch>
3.ShapeDrawable
在代码中生成的是GradientDrawable对象
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
<!--图形 可以是rectangle(矩形)
oval(椭圆)
line(线)
ring(圆环)
-->
android:shape=""
<!--以下是圆环特有属性
圆环内径-->
android:innerRadius="@dimen/activity_horizontal_margin"
<!--内径所占整个Drawable的比例 默认为9,如果为n,那么内半径=宽度/n,以innerRadius为主-->
android:innerRadiusRatio="1"
<!--圆环厚度-->
android:thickness="@dimen/activity_horizontal_margin"
<!--厚度所占整个Drawable的比例 默认为3,如果为n,那么厚度=宽度/n,以thickness为主-->
android:thicknessRatio="1"
<!--除非用于LevelListDrawable一般为false-->
android:useLevel="false"
>
<!--Drawable的内在宽高-->
<size android:height="@dimen/activity_horizontal_margin"
android:width="@dimen/activity_horizontal_margin"/>
<!--包含这个Drawable的View的空白-->
<padding
android:top="@dimen/activity_horizontal_margin"
android:bottom="@dimen/activity_horizontal_margin"
android:left="@dimen/activity_horizontal_margin"
android:right="@dimen/activity_horizontal_margin"
/>
<!--适用于rectangle四个角的圆角半径-->
<corners
android:radius="@dimen/activity_horizontal_margin"
android:topLeftRadius="@dimen/activity_horizontal_margin"
android:topRightRadius="@dimen/activity_horizontal_margin"
android:bottomRightRadius="@dimen/activity_horizontal_margin"
android:bottomLeftRadius="@dimen/activity_horizontal_margin"
/>
<!--颜色填充使用纯色-->
<solid android:color="@color/colorAccent"/>
<!--颜色填充使用渐变色-->
<gradient
<!--渐变类型
linear 线性渐变(默认)
radial 径向渐变
sweep 扫描线渐变
-->
android:type=""
<!-- 渐变的角度默认为0,其值必须为45的倍数
0代表从左到右
90代表从下到上-->
android:angle="0"
<!--渐变中心点坐标-->
android:centerX="1"
android:centerY="1"
android:startColor="@color/colorAccent"
android:centerColor="@color/colorAccent"
android:endColor="@color/colorAccent "
<!--渐变半径 仅当type等于radial有效-->
android:gradientRadius="1"
<!--除非用于LevelListDrawable一般为false-->
android:useLevel="false"
/>
<!--描边-->
<stroke
<!--描边宽度-->
android:width="@dimen/activity_horizontal_margin"
<!--描边颜色-->
android:color="@color/colorAccent"
<!--设置为虚线描边宽度与dashGap同时使用才能生效-->
android:dashWidth="@dimen/activity_horizontal_margin"
<!--描边虚线间隔空隙-->
android:dashGap="@dimen/activity_horizontal_margin"
/>
</shape>
4.LayerDrawable
一种不同层次的Drawable的集合,放置不同层面而达到叠加后的效果,一个layer-list可包含多个item
默认情况下,layer-list中所有的View都会被缩放至View的大小(对于bitmap可通过android:gravity控制图片显示效果),下面的item会覆盖上面的item。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
<!--可引用一个drawable资源-->
android:drawable="@drawable/drawable_bitmap"
<!--drawable相对于View的偏移量-->
android:top="@dimen/activity_horizontal_margin"
android:bottom="@dimen/activity_horizontal_margin"
android:left="@dimen/activity_horizontal_margin"
android:right="@dimen/activity_horizontal_margin"
>
<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>
5.StateListDrawable
不同状态选择不同drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<!--StateListDrawable固有大小是否随着drawable的切换而发生变化为true不发生变化,为item中drawable最大的固有大小 默认为false-->
android:constantSize="true"
<!--是否开启防抖效果-->
android:dither="true"
<!--StateListDrawable的padding是否随着drawable的切换而发生变化,为false不变是所有item中drawable padding的最大值,默认为false-->
android:variablePadding="true"
>
<item android:drawable="@drawable/create" android:state_pressed="true"/>
<item android:drawable="@drawable/create" android:state_focused="true" />
<item android:drawable="@drawable/create" android:state_checked="true" />
<!--默认-->
<item android:drawable="@drawable/create" />
</selector>
6.LevelListDrawable
Drawable的集合,每个Drawable都可设置最小level和最大level,View的背景可通过getBackground().setLevel()设置不同了Level匹配不同的Drawable
ImageView的话可通过setIamgeLevel()匹配不同的Drawable
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/create"
android:minLevel="0"
android:maxLevel="0"
>
</item>
<item android:drawable="@drawable/b_now" android:maxLevel="1"/>
<item android:drawable="@drawable/tap_launch" android:maxLevel="2"/>
<item android:drawable="@drawable/uilib" android:maxLevel="3"/>
</level-list>
7.TransitionDrawable
只能实现两个Drawable的淡入淡出效果
默认显示item1
TransitionDrawable drawable= (TransitionDrawable) mIvLevel.getBackground();
if (!mBoolean)
{
//从item1淡出,item2淡入
drawable.startTransition(1000);
mBoolean=true;
}
else
{
//反转
drawable.reverseTransition(1000);
mBoolean=false;
}
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/b_now"
android:top="@dimen/activity_horizontal_margin"
android:bottom="@dimen/activity_horizontal_margin"
android:left="@dimen/activity_horizontal_margin"
android:right="@dimen/activity_horizontal_margin"
></item>
<item android:drawable="@drawable/create"></item>
</transition>
8.InsetDrawable
把Drawable内嵌到自己当中
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/create"
android:insetBottom="@dimen/activity_horizontal_margin"
android:insetLeft="@dimen/activity_horizontal_margin"
android:insetRight="@dimen/activity_horizontal_margin"
android:insetTop="@dimen/activity_horizontal_margin"
>
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent"></solid>
</shape>
</inset>
9.ScaleDrawable
适合缩小一个特定的drawable
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/create"
<!--缩放宽高比例越大 Drawable显示越小-->
android:scaleWidth="1%"
android:scaleHeight="50%"
android:scaleGravity="center"
<!--level取值0到10000 为0不显示,level越大Drawable显示越大-->
android:level="1"
>
</scale>
10.ClipDrawable
裁剪Drawable 由clipOrientation和gravity共同决定裁剪位置
ClipDrawable clipDrawable= (ClipDrawable)
mIvLevel.getDrawable();
//level取值0到10000,取值越大裁剪越少 0完全裁剪,10000表示不裁剪
clipDrawable.setLevel(5000);
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/create"
<!--裁剪的方向-->
android:clipOrientation="vertical"
android:gravity="center"
>
</clip>
自定义Drawable
public class CustomDrawable extends Drawable {
private Paint mPaint;
public CustomDrawable(int color) {
mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(color);
}
@Override
public void draw(Canvas canvas) {
final Rect rect=getBounds();
float cx=rect.exactCenterX();
float cy=rect.exactCenterY();
canvas.drawCircle(cx,cy,Math.min(cx,cy),mPaint);
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
invalidateSelf();
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
圆形Drawable
public class CircleImageDrawable extends Drawable {
private Paint mPaint;
private int mWidth;
private Bitmap mBitmap ;
public CircleImageDrawable(Bitmap bitmap)
{
mBitmap = bitmap ;
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(bitmapShader);
mWidth = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
}
@Override
public void draw(Canvas canvas)
{
canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);
}
@Override
public int getIntrinsicWidth()
{
return mWidth;
}
@Override
public int getIntrinsicHeight()
{
return mWidth;
}
@Override
public void setAlpha(int alpha)
{
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf)
{
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity()
{
return PixelFormat.TRANSLUCENT;
}
}
圆角矩形Drawable
public class RoundImageDrawable extends Drawable {
private Paint mPaint;
private Bitmap mBitmap;
private RectF rectF;
public RoundImageDrawable(Bitmap bitmap)
{
mBitmap = bitmap;
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(bitmapShader);
}
@Override
public void setBounds(int left, int top, int right, int bottom)
{
super.setBounds(left, top, right, bottom);
rectF = new RectF(left, top, right, bottom);
}
@Override
public void draw(Canvas canvas)
{
canvas.drawRoundRect(rectF, 30, 30, mPaint);
}
@Override
public int getIntrinsicWidth()
{
return mBitmap.getWidth();
}
@Override
public int getIntrinsicHeight()
{
return mBitmap.getHeight();
}
@Override
public void setAlpha(int alpha)
{
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf)
{
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity()
{
return PixelFormat.TRANSLUCENT;
}
}