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

目录

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  这几个类的声明;

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"?>
<RelativeLayoutxmlns: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"><ImageViewandroid: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"?>
<RelativeLayoutxmlns: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"><Buttonandroid: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 的大小。

Android中的Drawable(一)相关推荐

  1. Android中的Drawable(三)

    文章收藏的好句子:一个人也许无法改变外界,但可以改变自己. 目录 1.TransitionDrawable 2.InsetDrawable 3.ScaleDrawable 1.TransitionDr ...

  2. android drawable 比例,Android中的Drawable基础与自定义Drawable

    转载请注明链接:http://blog..net/feather_wch/article/details/79124608 本文要点: 1. 介绍Android中Drawable的相关知识点,并且介绍 ...

  3. Android中的Drawable基础与自定义Drawable

    转载请注明链接:http://blog.csdn.net/feather_wch/article/details/79124608 本文要点: 1. 介绍Android中Drawable的相关知识点, ...

  4. android 椭圆形按钮,Android中使用drawable实现左右椭圆形的按钮

    本文和上一篇文章类似,只是增大了圆角半径的值,所以左右两边看着像椭圆形状,我们先来看看完成后的效果图: 首先,在res/drawable目录下新建一个shape形状,文件名为round_rect_ov ...

  5. 深入理解Android中的Drawable类

    Drawable对于Android开发工程师来说非常熟悉,最常用的用法是在drawable目录里放入png或其他格式的图片,然后在代码里就可以用resources访问到如: // 访问test图片资源 ...

  6. Android中使用Drawable绘制横竖虚线

    横虚线 <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape=&quo ...

  7. android 设置drawable大小,在Android中调整Drawable大小

    我设置了一个进度对话框( pbarDialog )的drawable,但我的问题是我想每次调整drawable的大小,但不知道如何. 这里是一些代码:Handler progressHandler = ...

  8. Android中的13种Drawable小结

    本节引言: 从本节开始我们来学习Android中绘图与动画中的一些基础知识,为我们进阶部分的自定义打下基础!而第一节我们来扣下Android中的Drawable!Android中给我们提供了多达13种 ...

  9. Android中的IPC机制

    Android IPC简介 IPC是Inter-Process Communication的缩写,含义就是进程间通信或者跨进程通信,是指两个进程之间进行数据交换的过程.那么什么是进程,什么是线程,进程 ...

  10. android获取当前位置经纬度,Android中通过GPS或NetWork获取当前位置的经纬度

    今天在Android项目中要实现一个通过GPS或NetWork来获取当前移动终端设备的经纬度功能.要实现该功能要用到Android Framework 中的 LocationManager 类.下面我 ...

最新文章

  1. 彩图完美解释:麦克斯韦方程组
  2. android手机存储大小设置在哪里看,Android 最新获取手机内置存储大小,SD卡存储空间大小方法...
  3. Atitit。DD dragdrop拖拽功能c#.net java swing的对比与实现总结
  4. Angular getOrCreateInjectable的实现原理调试
  5. .NET开发人员犯的6大安全错误
  6. html meta页面自适应,【转载·收藏】 html5手机网站自适应需要加的meta标签
  7. 【Pytorch神经网络实战案例】26 MaskR-CNN内置模型实现目标检测
  8. 为什么自己编写的页面总是在那里抖动_为什么小红书的简介添加不上 个性签名的修改方法...
  9. The reference to entity characterEncoding must end with the ';' delimiter
  10. 用汇编的眼光看C++(之const属性)
  11. 使用 Request.Form 集合之后,不能调用 BinaryRead
  12. 利用Javacsv实现Java读写csv文件
  13. 如何写程序自动下载BBC Learning English的所有在线课程
  14. 基于Fisher准则线性分类器设计
  15. C#之Dispose
  16. 小巧的网页聊天工具提供-GOFLY在线客服系统
  17. 日历查询---在线阴阳历转换器
  18. android官网m魅族15,魅族15/Plus/Lite等机型现身Android官网:设计惊艳
  19. 日语发音:日语汉字音变规则
  20. 12306多线程抢票

热门文章

  1. 【黑苹果镜像】macOS Monterey 12.3正式版 (21E230) With OpenCore 0.7.9 and Clover 5144 and winPE原版镜像
  2. Android之Dex动态加载机制解析
  3. win7的计算机最大连接数,win7系统解除共享文件夹最大连接数限制的操作方法
  4. win7添加打印机失败
  5. 大数据时代,数据的应用场景
  6. 基于Java框架开发OA企业在线办公系统项目教程-附源码-毕业设计
  7. 百度翻译API错误码大全(建议收藏)
  8. 实战篇---Nios ii 基本操作
  9. hilife技术支持网站
  10. 常用概率分布及其数学期望和方差