6.关于Drawable

1.简单介绍

  1. android.graphics.drawable.Drawable是一个可以在canvas上绘制的抽象概念;
  2. 通常用XML来定义,也可以用代码来创建(更复杂);
  3. 特点:比View成本低,非图片类型的Drawable占用空间小
/**
 * A Drawable is a general abstraction for "something that can be drawn."  Most
 * often you will deal with Drawable as the type of resource retrieved for
 * drawing things to the screen; the Drawable class provides a generic API for
 * dealing with an underlying visual resource that may take a variety of forms.
 * Unlike a {@link android.view.View}, a Drawable does not have any facility to
 * receive events or otherwise interact with the user.
 * **/
public abstract class Drawable {
	...
}

2.常见子类

  1. ShapeDrawable
    特殊形状,圆角矩形等等常见背景,常用xml来实现
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@color/color_task_item_bg" />

    <stroke
        android:color="@color/color_task_item_bg" />

    <corners android:radius="40px" />

</shape>
  1. LayerDrawable
    进度条背景,xml应用较为广泛
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/bg_pb_background"></item>

    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/bg_pb_progress"
            android:scaleWidth="100%" />
    </item>
</layer-list>
  1. BitmapDrawable
    用xml的形式来定义Image的各自属性,最后可以在布局文件中引用

3.自定义Drawable

自定义Drawable无法在xml中使用,所以其使用场景较少。关键点也是重写draw(canvas)方法,可以参考BitmapDrawable等具体子类等实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值