关于android:Android中的Drawable一

0次阅读

共计 4824 个字符,预计需要花费 13 分钟才能阅读完成。

PS:本文系转载文章,浏览原文可读性会更好,文章开端有原文链接

目录 1、Drawable 的分类 2、BitmapDrawable3、ShapeDrawable1、Drawable 的分类示意一种图像的概念,然而它们又不全是图片,也是能够通过色彩来结构出各式各样的图像的成果;咱们应用最多的是 Drawable 被用来作为 View 的背景应用,Drawable 作为 View 的背景应用就有 2 种形式了,一种是通过 XML 布局文件来设置,一种是应用逻辑代码(Java 语言、kotlin 语言)给 View 设置 Drawable;Drawable 是一个抽象类,它是所有 Drawable 子类的基类,比方 BitmapDrawable、ShapeDrawable、LayerDrawable 和 StateListDrawable 等;好,咱们看看这 BitmapDrawable、ShapeDrawable、LayerDrawable 和 StateListDrawable  这几个类的申明;

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> 程序运行后果如下所示;

咱们下面写的 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> 程序运行的后果如下所示;

咱们这里写的 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 的大小。

正文完
 0