Drawable以及资源分析

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

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);
目录
相关文章
|
Android开发
android 中对Resources资源的访问
android 中对Resources资源的访问
84 0
|
编解码 Java Android开发
Android加载drawable中图片后自动缩放的原理
Android加载drawable中图片后自动缩放的原理
|
XML 缓存 API
Android如何优雅地解决重复Drawable资源
Android如何优雅地解决重复Drawable资源
Android如何优雅地解决重复Drawable资源
|
XML 编解码 Android开发
Android中的资源复用小技巧
做了很多项目,发现每个项目里都有大量的相似图标,比如每个颜色一种、每个角度一种(左箭头、右箭头)等等,虽然这些图标很小占用不了太多资源。但是当我们需要change的时候就得一个个去替换,其实在android里有很多小技巧可以让这些资源进行复用,减少体积的同时也可以使项目结构更加清晰。 这里总结的一些比较常用的复用,包扩:自适应、变色、旋转、阴影、组合、透明度、剪裁、留白等
233 0
|
Java 数据格式 XML
TextView中DrawableXXX图片无法设置大小的解决方案
在开发过程中我们往往会遇到图片旁边带文字的布局,这种布局有些比较Low的开发会直接用一个ImageView和TextView,有经验的会给TextView设置DrawableLeft、DrawableRight等等属性,一个View搞定,但是这个属性设置图片是无法控制大小的,在xml里面,当然在Java代码里是可以设置的。
1951 0
|
XML Android开发 数据格式
|
存储 缓存 Java
android 加载图片oom若干方案小结
本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结。 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定)。一般我们可以通过获取当前线程的可运行内存来判断,比如系统分给当前运行内存只有16M,而你的图片就有16M,这肯定会oom的。 相关知识介绍 1.颜色模型 常见的颜色模型有RGB、YUV、
1481 0

热门文章

最新文章