Android中的Drawable(一)

文章收藏的好句子:接受生活中的事与愿违,当你有这种坦然,你会看得更深,你会走得更远。

目录

1、Drawable 的分类

2、BitmapDrawable

3、ShapeDrawable

1、Drawable 的分类

表示一种图像的概念,但是它们又不全是图片,也是可以通过颜色来构造出各式各样的图像的效果;我们使用最多的是 Drawable 被用来作为 View 的背景使用,Drawable 作为 View 的背景使用就有2种方式了,一种是通过 XML 布局文件来设置,一种是使用逻辑代码(Java语言、kotlin语言)给 View 设置 Drawable;Drawable 是一个抽象类,它是所有 Drawable 子类的基类,比如 BitmapDrawable、ShapeDrawable、LayerDrawable 和 StateListDrawable 等;好,我们看看这 BitmapDrawable、ShapeDrawable、LayerDrawable 和 StateListDrawable  这几个类的声明;

2865c65d00d63378f13da7344466cb72.png

ac82bef17e303370ad3afdec54d07348.png

a038f697a6de489fe1c4dbf22a28e168.png

e1fd958209a2f5370014f90172740a25.png

StateListDrawable 继承的 DrawableContainer 最终是继承 Drawable。

2、BitmapDrawable

讲 BitmapDrawable 的属性之前,我们先写一个 BitmapDrawable 的 demo;

(1)在 drawable 文件夹下新建一个 bitmap.xml ;

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/img_back"
    android:dither="true"
    >
</bitmap>

(2)Activity 的布局文件 activity_main.xml ;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bitmap"/>
</RelativeLayout>

程序运行结果如下所示;

b0db8e3f9562b26a38998661748244c1.jpeg

我们上面写的 demo 是通过 bitmap.xml 文件来描述 BitmapDrawable 的,下面我们一一列举描述 BitmapDrawable 的所用属性。

android:src  :表示图片的资源 id;

android:antialias  : 表示是否开启抗锯齿功能,true 表示已经开启,如果开启了抗锯齿,那么图片就会变得平顺起来,什么意思呢?就比如说,一张图片的边是呈波浪线的,如果开启了抗锯齿功能,那么图片的边就趋向于直线的。

android:dither  : 是否开启抖动效果,当图片的像素配置和手机屏幕的像素配置不一致时,开启这个选项可以让高质量的图片在低质量的屏幕上还能保持较好的显示效果。

android:filter  : 是否开启过滤效果;当图片尺寸被拉伸或者压缩时,开启过滤效果可以保持较好的显示效果。

android:gravity  : 当图片小于容器的尺寸时,设置此选项可以对图片进行定位。

android:minMap  : 这是一种图像相关的处理技术,也叫纹理映射,默认值为 false。

android:tileMode  : 平铺模式,这个属性有如下几个值:disabled、clamp、repeat 和 mirror,disable 它是表示关闭平铺模式,这是默认值;repeat 表示的是简单的水平和竖直方向上的平铺效果, mirror 表示一种在水平和竖直方向上的镜面投影效果,clamp 表示的效果就是图片四周的像素会扩展到周围区域。

3、ShapeDrawable

它可理解为通过颜色来构造的图形,可以是纯色的图形,也可以是具有渐变效果的图形;我们先写一个 demo,然后再对它的属性一一说明;

(1)在 drawable 目录下新建一个 shape_drawable.xml 文件;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="38px"/>
    <solid android:color="#1756F1" />
    <!--<gradient android:centerColor="#00FF00"/>-->
    <padding android:bottom="10px"
        android:top="10px"
        android:left="10px"
        android:right="10px"/>
    <size android:width="100px"
        android:height="100px"/>
    <!--<stroke android:width="10px"-->
        <!--android:dashGap="3px"-->
        <!--android:dashWidth="3px"-->
        <!--android:color="#FF0000"/>-->
</shape>

(2)Activity 的布局文件 activity_main.xml ;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:layout_width="match_parent"
        android:text="开心"
        android:textSize="30px"
        android:textColor="#FF0000"
        android:background="@drawable/shape_drawable"
        android:layout_height="wrap_content" />
</RelativeLayout>

程序运行的结果如下所示;

0e07fd9c7d657810b12502760f0f1e89.jpeg

我们这里写的 ShapeDrawable 的效果是通过 shape_drawable.xml 文件的 shape 标签呈现出来的;好,我们现在讲解一下 shape 标签的属性以及 shape 的子标签的其他属性;

1) shape 标签的属性

android:shape  : 表示图形的形状,有 rectangle、oval、line 和 ring 这4个值,rectangle 是矩形,oval 是圆形,line 是横线,ring 是圆环,它的默认值是矩形,另外 line 和 ring 这两个值必须要通过 shape 的子标签 stroke 标签来指定线的宽度和颜色等信息,才达到预期的显示效果。

2)shape 标签的子标签属性

2、1)corners 标签

android:radius  : 适用于 shape 标签的 android:shape 属性值 rectangle,表示4个角的圆角角度,它比 android:topLeftRadius、android:bottomLeftRadius、android:bottomRightRadius 和 android:topRightRadius 这几个属性的优先级低。

android:topLeftRadius   :左上角的角度。

android:bottomLeftRadius  :左下角的角度。

android:bottomRightRadius  :右下角的角度。

android:topRightRadius  : 右上角的角度。

2、2)gradient 标签

它与 solid 标签是互相排斥的,其中 solid 表示纯色填充,而 gradient 则表示渐变效果。

android:angle  :  表示渐变的角度,默认为0,其值必须为 45 的倍数,0 表示从左到右,90表示从下到上。

android:centerX  :  渐变中心点的横坐标。

android:centerY  :  渐变中心点的纵坐标。

android:startColor  : 渐变的起始颜色。

android:centerColor  :渐变的中间色。

android:endColor  :  渐变的结束色。

android:gradientRadius  :  渐变的半径。

android:type  :  渐变的类别,其值有 linear、radial 和 sweep 这3种,linear 是线性渐变,radial 是径向渐变,sweep 是扫描性渐变,默认值是线性渐变。

2、3)solid 标签

android:color  : 将整个 shape 标签给填充颜色。

2、4)stroke 标签

android:width  : 描边的宽度。

android:color  :  描边的颜色。

android:dashWidth  :  组成虚线线段的宽度。

android:dashGap  :  组成虚线线段的间隔。

 注意:如果 android:dashWidth 和 android:dashGap 有任意一个为0,那么就没有虚线的效果。

2、5)padding 标签

android:left  :这个不是表示 shape 区域 的空白,而是表示包含 shape  的 view 的左边内边距空白。

android:right :  这个不是表示 shape区域的空白,而是表示包含 shape 的view的右边内边距空白。

android:top : 这个不是表示 shape 区域的空白,而是表示包含 shape 的 view 的顶部内边距空白。

android:buttom : 这个不是表示 shape 区域的空白,而是表示包含 shape 的 view 的底部内边距空白。

2、6) size 标签

shape 的大小,有两个 android:width 和 android:height 属性,分别表示 shape 的宽和高;这个表示的是 shape 的固有大小,但是一般来说它并不是 shape 最终显示的大小,对于 shape 来说它并没有宽和高的概念,作为 View 的背景它会自适应 View 的宽和高;Drawable 的两个方法 getIntrinsicWidth 和 getIntrinsicHeight 表示的是 Drawable的固有宽和高,对于有些 Drawable 比如图片来说,它的固有宽和高就是图片的尺寸;而对于 shape 一般来说,它是没有固有宽和高这个概念的,size 标签设置的宽和高就是 ShapeDrawable 的固有宽和高,作为 View 的背景时,shape 会适配为 View 的大小。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值