Android-总结Drawable用法

今天为了适配启动页背景图,接触到了BitmapDrawable保证了启动页的背景图不变形。想一想以前真的没用过,因此仍是有必要详细了解一下Drawable,至少在遇到一些问题时能够及时的找到解决方案。android

Drawable的分类

Drawable通常都是用XML定义的,可是咱们也能够自定义Drawable,可是代码会比较复杂。bash

BitmapDrawablw

能够理解为它是一张带规则的图片,通常状况下若是能够在ImageView里面设置属性的就不必使用,可是若是一张图片要作到像放在控件里适配就有必要使用了,例如启动页背景图布局

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="clip_horizontal|clip_vertical"
    android:dither="true"
    android:src="@drawable/splash_bg"
    android:tileMode="disabled"/>
    <!--属性解释-->
    <!--android:src 图片资源id-->
    <!--android:antialias是否开启抗锯齿,应该开启-->
    <!--android:dither是否开启抖动效果,应该开启-->
    <!--android:filter是否开启过滤效果,应该开启-->
    <!--android:gravity对图片进行定位-->
    <!--android:mipMap纹理映射,通常为false-->
    <!--android:tileMode平铺模式四个值:disabled(默认值即关闭平铺模式)、clamp、repeat、mirror-->
复制代码

若是直接使用图片,因为不能控制图片的位置,有些手机会变形很严重。学习

NinePatchDrawable

与BitmapDrawable相似不过NinePatchDrawable的效果相似于.9图,实际使用时其实直接使用.9图就好动画

ShapeDrawable

开发时用的比较多,能够理解为只有纯颜色或渐变色的图片。只是有些地方须要注意一下。ui

以上代码是一个指定渐变百分比的渐变shape,其中centerY与centerColor必须同时设置才能生效,不然只是普通的渐变,另外,其实咱们的系统进度条也能够设置渐变spa

<ProgressBar
             style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:max="1000"
                android:progress="0"
                android:progressDrawable="@drawable/progress_green" />

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--progress_green-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="50dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/progress_shape"
            android:scaleWidth="100%" />
    </item>
</layer-list>
复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--progress_shape-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="0"
        android:endColor="@color/theme_color"
        android:startColor="@color/theme_color1"
        android:type="linear" />
</shape>
复制代码

进度条max的值因为没有小数,因此设置1000效果会好不少,加上动画效果后效果以下.net

-c

LayerDrawable

对应的标签是layer-list,表示一种层次化的Drawable。不少时候均可以使用布局嵌套完成,可是使用layor-list能够减小布局的层级结构,例如3d

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <stroke android:width="1dp" android:color="#45DAA8" />
        </shape>
    </item>

    <item>
        <bitmap android:src="@drawable/gou"
            android:gravity="bottom|right" />
    </item>
</layer-list>
复制代码

效果图code

StateListDrawble

这个开发中也常常用到,标签对应的是selector。好比咱们的点击效果、还有选择后与选择前的不一样效果,使用selector能够在代码中设置一个属性改变很方便。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:drawable="@drawable/bg_btn_circle_gradient_full">
    </item>

    <item android:state_enabled="false">

        <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="#aaffffff">
            <item>
                <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
                    <corners android:radius="360dp" />
                    <solid
                        android:color="@color/gray_eeeeee"
                        />
                </shape>
            </item>
        </ripple>

    </item>
</selector>
复制代码

这里结合了ripple标签,能够作出水波纹效果。效果以下

LevelListDrawable

也是一个Drawable的集合,对应的标签是level-list。有点相似于layer-list,不过level-list是根据等级(0~10000)来显示的,而layer-list则是显示在一块儿的。开发中比较少用到,若是一个按钮有多种状态能够考虑使用代替if-else判断显示

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/icon_heat" android:maxLevel="1"/>
    <item android:drawable="@drawable/icon_cool" android:maxLevel="2"/>
</level-list>
复制代码
imageview.getDrawable().setLevel(1);
复制代码

TransitionDrawable

能够实现两个Drawable之间的淡入淡出,对应的标签是transition。

ScaleDrawable

对应的标签是scale,能够根据level指定Drawable缩放到必定比例。

这两个效果我以为能够在启动页的背景图作一点动画效果或者作全局的动画,其余地方可使用动画代替。

InsetDrawable

对应的标签是inset,能够把Drawable内嵌进来,能够用于加大点击区域。

ClipDrawable

对应的标签是clip,能够根据level裁剪出另一个drawabl。挺有用的一个Drawable能够看看下面的博客 效果图:

-c

blog.csdn.net/briblue/art…

自定义Drawable

其实Drawable是一个抽象的概念,能够理解为Canvas以前的一个封装。就是把资源封装成Drawable再绘制到画布上。Drawable能够说是内存级的画布。

过程与自定义View时的绘画是类似的,具体也上面ClipDrawable的博客。

参考资料

《Android开发艺术探索》


最开始坚持的地方,记录学习与生活的点点滴滴
相关文章
相关标签/搜索