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>

img_c0be513c97b646cf2173971a32bb5526.png
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>

img_1b6eebcb25b3e04cc2d0404f348518e3.png
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);
优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/658846

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。