Drawable以及资源分析

drawable是一个抽象类,他把资源文件夹下的Drawable用其子类进行实例化,然后绘制.so,我们只是在drawble资源中进行了配置,其绘制过程在对应的实现类中.
本文举例几种不常见的drawable......

1.BitmapDrawble

Bitmap-->位图

Bitmap是存储图片的一个类,构造成的是一个位图.构造过程在native方法中.

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.
 *drawable是一个抽象类,可以对资源进行绘制的工具类.

说一下Bitmap和Drawable的关系
Drawable的子类BitmapDrawble是对bitmap的包装类,在他的BitmapState中有一个bitmap对象引用.也就是说可以通过转型得到bitmap

BitmapDrawable bd = (BitmapDrawable) drawable;  
Bitmap bm= bd.getBitmap();  

下是他注释的翻译.

A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a
 BitmapDrawable from a file path, an input stream, through XML inflation, or from a {@link android.graphics.Bitmap} object.
 BitmapDrawble是一个bitmap的包装类,可以对bitmap进行平铺,拉伸和对齐等.可以通过IO,文件,xml或者bitmap构建对象

2.ShapeDrawable

ShapeDrawable是最常见的,他通过xml的drawable构建shape得到.

它对应的类不是ShapeDrawable,而是android.graphics.drawable.GradientDrawable.....害我找了好久shape中的ring等方法,好坑.

我们可以通过这个绘制我们想要的效果
比如,我们绘制一层蒙层效果,效果不错.

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

    <gradient
        android:angle="180"
        android:endColor="@android:color/transparent"
        android:startColor="@android:color/white" />

</shape>

image.png

3.LayerDrawable

LayerDrawable对应的是XML的<layer-list>,他是一种层次化的Drawable,类似于层叠效果.

这种我们可以做这种需求,比如需要对一个自定义控件进行底部画线,或者用线半包含底边,我们可以用.9图,或者用layer_list实现,作为background.比如

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

<item >
    <shape android:shape="rectangle">
        <solid android:color="#0ac39e"></solid>
    </shape>
</item>

    <item android:bottom="10dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"></solid>
        </shape>
    </item>
    <item android:right="3dp" android:bottom="3dp" android:left="3dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"></solid>
        </shape>
    </item>

</layer-list>

image.png

4.StateListDrawable

对应着<Selector> 根据状态进行不同的操作.不做赘述.

LevelListDrawable

对应着<level-list>标签,它可以根据level不同,改变相应的drawable.额....感觉和直接setImageVIew差不多....

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

    <item android:minLevel="0" android:maxLevel="3" android:drawable="@color/colorAccent"></item>
    <item android:minLevel="4" android:maxLevel="6" android:drawable="@color/colorPrimary"></item>
    <item android:minLevel="7" android:maxLevel="9" android:drawable="@color/colorPrimaryDark"></item>

</level-list>
//两种调用方式
  drawable.setLevel(0);
  imageView.setImageLevel(0);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • //通过获得资源文件进行设置。根据不同的情况R.color.red也可以是R.string.red或者R.draw...
    gogoingmonkey阅读 6,006评论 0 2
  • 1、Drawable 简介 Drawable——可简单理解为可绘制物,表示一些可以绘制在 Canvas 上的对象。...
    牧秦丶阅读 15,294评论 0 15
  • 更多Android总结知识点 Android中的13种Drawable小结 Android的八种对话框的实现 An...
    侯蛋蛋_阅读 9,524评论 0 5
  • 一、 Drawable简介 1 Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念,可以是纯颜色...
    黄海佳阅读 4,991评论 0 10
  • 天灰蒙蒙的,空气中充满了辛辣的味道,阳光透过树荫照进来,不痛不痒。抖抖衣领,跺一跺脚,灰尘便四下跳开了,不受拘束。...
    一只闪电猫阅读 1,571评论 0 0

友情链接更多精彩内容