Drawable分类

Drawable分类

1.BitmapDrawable

一般开发中直接引用图片即可,但也可以通过XML的方式来描述它,可以设置更多的效果。

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:dither="true"
    android:gravity="center"
    android:mipMap="true"
    android:filter="true"
    android:src="@drawable/ic_launcher"
    android:tileMode="disabled" >

</bitmap>   

android:antialias
是否开启抗锯齿。开启使图片变得平滑。
android:dither
是否开启抖动效果。让高质量的图片在低质量的屏幕上还能保持较好的显示效果。
android:filter
是否开启过滤效果。图片尺寸被拉伸或压缩时,可以保持较好的显示效果。
android:mipMap(mipMap在API18以上)
纹理映射。图像相关的处理技术,不常用。
android:tileMode
平铺模式。[“disabled”|”clamp”|”repeat”|”mirror”],disabled:关闭平铺模式,开启平铺后android:gravity属性会被忽略。repeat:水平和竖直方向的平铺,mirror:水平和竖直方向镜面投影效果,clamp:图片四周的图像会扩展到周围区域。
这里写图片描述 repeat
这里写图片描述 mirror

NinePathDrawable

表示一张.9图。

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:src="@drawable/ic_launcher" >

</nine-patch>

ShapeDrawable

通过颜色来构造图形,即可是纯色,也可是渐变效果。

LayerDrawable

对应xml标签是,表示一种层次化的Drawable集合。

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 连框颜色值 -->
    <item
        android:bottom="0dp"
        android:left="0dp"
        android:right="1dp"
        android:top="0dp">
        <shape>
            <corners android:radius="3dp" />
            <gradient
                android:angle="270"
                android:endColor="#FFFFFF"
                android:startColor="#EEEEEE" />
        </shape>
    </item>
    <!-- 主体背景颜色值 -->
    <item
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape>
            <corners android:radius="3dp" />
            <stroke
                android:width="1dp"
                android:color="#E8E9E9" />
        </shape>
    </item>

</layer-list>

StateListDrawable

对应标签

LeverListDrawable

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/garland_gray"
        android:maxLevel="0" />
    <item
        android:drawable="@drawable/garland_yellow"
        android:maxLevel="1" />
    <item
        android:drawable="@drawable/garland_blue"
        android:maxLevel="2" />
</level-list>

通过下边代码设置选中对应等级的Drawable。

imageView.getDrawable().setLevel(1);

TransitionDrawable

对应标签。用于实现两个Drawable之间的淡入淡出效果

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/edge_green"/>
    <item android:drawable="@drawable/edge_purple"/>

</transition>

通过下边代码设置


        TransitionDrawable drawable = (TransitionDrawable) tv.getBackground();
        drawable.startTransition(5000);//开启效果
        drawable.reverseTransition(5000);//逆过程

InsetDrawable

对应标签

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="15dp"
    android:insetLeft="15dp"
    android:insetRight="15dp"
    android:insetTop="15dp" >

    <shape android:shape="rectangle" >
        <solid android:color="#ff0000" />
    </shape>

</inset>

ScaleDrawable

对应标签,可以根据自己的等级(level)将指定的Drawable缩放到一定比例。

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_launcher"
    android:scaleGravity="center"
    android:scaleHeight="25%"
    android:scaleWidth="25%" >

</scale>

但是直接使用上面的Drawable资源是不行的,必须设置ScaleDrawable的等级0-10000之间的值,如下:

ScaleDrawable scaleDrawable = (ScaleDrawable) view.getBackground();
        scaleDrawable.setLevel(1);

ClipDrawable

对应标签,根据自己当前等级来裁剪另一个Drawable。

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@drawable/ic_launcher"
    android:gravity="bottom" >

</clip>

裁剪方向通过android:clipOrientation和android:gravity共同控制。上边表示从上往下竖直裁剪。


ClipDrawable clipDrawable=(ClipDrawable) iv.getDrawable();
clipDrawable.setLevel(5000);

level从0-10000,0表示完全裁剪,10000表示不裁剪,5000表示裁剪一半。

自定义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) {
        Rect r = getBounds();
        float cX = r.exactCenterX();
        float cY = r.exactCenterY();
        canvas.drawCircle(cX, cY, Math.min(cX, cY), mPaint);

    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
        invalidateSelf();
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
        invalidateSelf();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值