第六章 Android的Drawable

      Drawable的优点:使用简单,比自定义view的成本要低;非图片类型的Drawable占用空间小,有利于减小APK安装包的大小。

    6.1Drawable简介

    Drawable有很多种,他们都表示一种图像的概念,Drawable常被用来作为view的背景使用。Drawable是一个抽象类。Drawable的内部宽高这个参数比较重要,通过getIntrinsicWidth和getIntrinsicHeight这两个方法可以获取到他们。但并不是所有的Drawable都有内部宽高。

    6.2Drawable的分类

      6.2.1 BitmapDrawable

      BitmapDrawableb表示一张图片,下面是其常用的属性(xml格式的描述方式)     

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@mipmap/ic_launcher"
android:antialias="true|false"
android:dither="true|false"
android:filter="true|false"
android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"
android:mipMap="true|false"
android:tileMode="disabled|clamp|repeat|mirror"/>

      android:src  图片的资源ID,必有的属性。

      android:antialias  抗锯齿功能

      android:dither 抖动效果(优化图片显示效果的作用)

      android:filter 过滤效果(优化图片显示效果的作用)

      android:gravity 定位图片

      android:mipMap 纹理映射(不常用)

      android:tileMode 平铺模式(当开启这个模式时,gravity属性会被忽略)共有四种模式:disabled——关闭平铺模式;repeat——水平和垂直方向上的平铺效果;clamp——图片四周的像素会扩展到周围区域。mirror——水平和垂直方向上的镜面投影效果。

     NinePatchDrawable表示.9格式的图片,xml格式描述如下:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@mipmap/ic_launcher"
android:dither="true|false"/>

      6.2.2 ShapeDrawable

        ShapeDrawable代表通过颜色来构造的图形既可以是纯色也可以有渐变效果。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle|oval|line|ring">
<corners
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer"
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer" />
<gradient
android:angle="integer"
android:centerColor="integer"
android:centerX="integer"
android:centerY="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type="linear|radial|sweep"
android:useLevel="true|false" />
<padding
android:bottom="integer"
android:left="integer"
android:right="integer"
android:top="integer" />
<size
android:width="integer"
android:height="integer" />
<solid android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashGap="integer"
android:dashWidth="integer" /> </shape>

      android:shape  图形形状:rectangle(矩形)、oval(椭圆)、line(横线)、ring(圆环),默认矩形,line和ring必须有<stroke>标签

      <corners>  四个角的角度,只适用于矩形

      <gradient>  渐变效果,与<solid>标签互斥

      <solid>  纯色填充

      <stroke>  描边

      <padding>  包含它的View的空白

      <size>  shape的大小,表示shape的固有大小,并不代表最终显示大小。

      6.2.3 LayerDrawable

       LayerDrawable代表层次化的Drawable集合,用来实现叠加效果。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/ic_launcher"
android:id="@+id/drawable"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension">
</item>
</layer-list>

      一个<layer-list>可以包含多个item,每个item表示一个drawable,下面的item会覆盖上面的item,通过合理的分层,可以实现一些特殊的叠加效果。下面的例子实现了一种微信中的文本输入框效果

<?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" />
</shape>
</item>
<item android:bottom="6dip">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item>
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</item> </layer-list>

      6.2.4 StateListDrawable            

      主要用于设置可单击的View的背景,最常见的是Button,对应于<selector>标签。XML格式如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true|false"
android:dither="true|false"
android:variablePadding="true|false">
<item
android:drawable="@mipmap/ic_launcher"
android:state_pressed="true|false"
android:state_focused="true|false"
android:state_selected="true|false"
android:state_checked="true|false"
android:state_enabled="true|false"
/> </selector>

        item有很多状态,以上只列取了几种常见的状态。

      android:constantSize StateListDrawable的固有大小是否不随着其状态的改变而改变。

      android:dither 是否开启抖动效果

      android:variablePadding StateListDrawable的padding是否随着其状态的改变而改变。

      android:state_pressed 按下状态

      android:state_focused 获取焦点

      android:state_selected 选择

      android:state_checked 选中

      android:state_enabled 可用     

        默认的item一般放在selector的最后一条并且不附带任何状态。

      6.2.5 LevelListDrawable

      Drawable集合,每个Drawable都有一个等级,根据不同的等级,LevelListDrawable会切换对应的Drawable。xml格式的语法如下:

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/ic_launcher"
android:maxLevel="integer"
android:minLevel="integer" /> </level-list>

      等 级范围:0~10000,作为View的背景时,可以通过Drawable的setLevel方法来设置不同的等级从而切换具体的Drawable;作为 ImageView的前景Drawable,可以通过ImageView的setImageLevel方法来切换Drawable。

      6.2.6  TransitionDrawable

      实现两个Drawable之间的淡入淡出效果,对应于<transition>标签,XML格式语法如下:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/ic_launcher"
android:id="@+id/resource_name"
android:top="dimension"
android:bottom="dimension"
android:left="dimension"
android:right="dimension"
/>
</transition>

      实例:切换TextView的背景

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/drawable1" />
<item android:drawable="@mipmap/drawable2" />
</transition>
  <TextView
android:background="@drawable/test9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
        TextView textView=(TextView)findViewById(R.id.test);
TransitionDrawable drawable=(TransitionDrawable)textView.getBackground();
drawable.startTransition(1000);

      6.2.7  InsetDrawable

      可以将其他Drawable内嵌到自己当中,对应于<inset>标签,当一个View希望自己的背景比自己的实际区域小的时候可以采用InsetDrawable来实现。XML格式语法如下:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/ic_launcher"
android:insetTop="dimension"
android:insetBottom="dimension"
android:insetLeft="dimension"
android:insetRight="dimension">
</inset>

      6.2.8  ScaleDrawable

      根据自己的等级将指定的Drawable缩放到一定比例;对应于<scale>标签。XML语法如下:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/ic_launcher"
android:scaleGravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal"
android:scaleHeight="percentage"
android:scaleWidth="percentage"/>

      使用时还需要设置ScaleDrawable的等级大于0且小于10000的值,否则看不到效果

        ScaleDrawable scaleDrawable=(ScaleDrawable)textView.getBackground();
scaleDrawable.setLevel(1);

       6.2.9   ClipDrawable

      根据自己当前的等级来裁剪另一个Drawable,裁剪方向通过android:clipOrientation和android:gravity这两个属性来共同控制。XML属性语法如下:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal|vertical"
android:drawable="@mipmap/ic_launcher"
android:gravity="top|bottom|left|right|center_vertical|fill_vertical|center_horizontal|fill_horizontal|center|fill|clip_vertical|clip_horizontal" />

      等级范围也是0~10000,0表示完全裁剪,10000表示不裁剪。

   ClipDrawable clipDrawable=(ClipDrawable)textView.getBackground();
clipDrawable.setLevel(5000);

    6.3 自定义Drawable     

    只有某些特殊情况下才会用到,注意getIntrinsicWidth和getIntrinsicHeight这两个方法(p262)。

Android开发艺术探索学习笔记(六)的更多相关文章

  1. Android开发艺术探索学习笔记(三)

    第三章  View的事件体系 3.1 View基础知识 3.1.1 什么是view View 是Android中所有控件的基类,是一种界面层的控件的一种抽象,它代表了一个控件. 3.1.2 View的 ...

  2. Android开发艺术探索学习笔记(十一)

    第十一章  Android的线程和线程池 从用途上来说,线程分为子线程和主线程,主线程主要处理和界面相关的事情,而子线程往往用于执行耗时的操作.AsyncTask,IntentService,Hand ...

  3. Android开发艺术探索学习笔记(十)

    第十章  Android的消息机制 面试中经常会被问到的一个问题:handler是如何在子线程和主线程中进行消息的传递的,这个问题通过了解Android的消息机制可以得到一个准确的答案. Androi ...

  4. Android开发艺术探索学习笔记(四)

    第四章 View的工作原理 4.1初识ViewRoot和DecorView ViewRoot是连接WindowManager和DecorView的纽带,View的三大流程均是通过ViewRoot来完成 ...

  5. Android开发艺术探索学习笔记(一)

    第一章 Activity的生命周期和启动模式 1.1Activity的生命周期全面解析 1.1.1典型情况下的生命周期分析 (1)在两个Activity进行切换时,当前的Activity的onPaus ...

  6. Android开发艺术探索读书笔记——进程间通信

    1. 多进程使用场景 1) 应用某些模块由于特殊需求须要执行在单独进程中. 如消息推送,使消息推送进程与应用进程能单独存活,消息推送进程不会由于应用程序进程crash而受影响. 2) 为加大一个应用可 ...

  7. Android开发艺术探索读书笔记——01 Activity的生命周期

    http://www.cnblogs.com/csonezp/p/5121142.html 新买了一本书,<Android开发艺术探索>.这本书算是一本进阶书籍,适合有一定安卓开发基础,做 ...

  8. android开发艺术探索学习 之 结合Activity的生命周期了解Activity的LaunchMode

    转载请标明出处: http://blog.csdn.net/lxk_1993/article/details/50749728 本文出自:[lxk_1993的博客]: 首先还是先介绍下Activity ...

  9. android开发艺术探索读书笔记之-------view的事件分发机制

    View的点击事件的分发,其实就是对MotionEvent事件的分发过程,即当一个MotionEvent产生后,系统需要把这个事件传递给一个具体的View,而这个过程就是分发过程. 分发过程主要由以下 ...

随机推荐

  1. linux内核链表使用

    原文链接:http://blog.csdn.net/xnwyd/article/details/7359373 Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p ...

  2. hdu 5073 有坑+方差贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=5073 就是给你 n 个数,代表n个星球的位置,每一个星球的重量都为 1 开始的时候每一个星球都绕着质心转动,那么 ...

  3. 我的ecshop二次开发经验分享

    https://jingyan.baidu.com/article/358570f65dbad2ce4724fcc7.html

  4. [leet code 100] same tree

    1 题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees ar ...

  5. Asp .Net core 2 学习笔记(1) —— Starup

    这个系列的初衷是便于自己总结与回顾,把笔记本上面的东西转移到这里,态度不由得谨慎许多,下面是我参考的资源: ASP.NET Core 中文文档目录 官方文档 记在这里的东西我会不断的完善丰满,对于文章 ...

  6. C#Datetimepicker出现问题及解决方法

    前几天公司用的物料管理系统出现了一个很奇怪的问题,具体的现象是:10月31号的那天,物流部的人因为之前的问题,需要将之前已经结转的9月份取消结转. ,当操作人员将10改变为9的时候,出现问题了.程序直 ...

  7. Grid++repor报表连接事件

    //定义报表模板 private GridppReport Report = new GridppReport(); //载入报表模板数据 Report.LoadFromFile(GridppRepo ...

  8. PowerDesigner执行脚本 name/comment/stereotype互转

    执行方法:工具栏->Tools -> Execute Commands -> Edit/Run Script (Ctrl+Shift+X) 如下图所示: 1.Name转到Commen ...

  9. DOM LEVEL 1 中的那些事儿[总结篇-下]

    本文承接:DOM LEVEL 1 中的那些事儿[上]   2.3 Element类型 Element类型应该是Document类型之外使用的最多的节点类型了,Element代表XML或HTML文档中的 ...

  10. Markdown的学习笔记一

    之前学习看些书籍.学些技术都喜欢用xmind做思维导图的笔记,慢慢的发现想把一些笔记做的详细一些就会变得很复杂,个人觉得误了思维导图本意,而且用手机查看的时候也各种不方便.所以开始学习使用markdo ...