《android开发艺术探索》读书笔记(六)--Drawable
接上篇《android开发艺术探索》读书笔记(五)--RemoteViews
【BitmapDrawable】
简单的图片
<!xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
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"] --纹理映射,默认false,不常用
android:tileMode=["disabled"|"clamp"|"repeat"|"mirror"]/> --平铺模式,repeat普通平铺,mirror镜面投影,clamp周围扩散
【NinePatchDrawable】
.9图
<!xml version="1.0" encoding="utf-8"?>
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
android:dither=["true"|"false"]/>
【ShapeDrawable】
通过颜色来构造的图形,即<shape>标签,百度一搜一堆,这里就不写了
【LayerDrawable】
对应XML标签是<layer-list>,表示一种层次化的Drawable集合,通过将不同的Drawable放置在不同的层上面从而达到一种叠加后的效果
一个layer-list中可以包含多个item,每个item表示一个Drawable
<!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="6dp">
<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>
【StateListDrawable】
对应<selector>标签,百度一搜一堆,这里就不讲了
【LevelListDrawable】
对应于<level-list>标签,同样表示一个Drawable集合,集合中的每个Drawable都有一个等级level的概念,根据不同等级,会切换为对应的Drawable
每个item表示一个Drawable,并有等级范围,由minLevel和maxLevel来指定,在最小值和最大值之间的等级会对应此item中的Drawable,通过Drawable的setLevel方法来设置背景图片,setImageLevel方法来设置前景图片,等级范围0~10000
<!xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/status_off"
android:maxLevel="0"/>
<item
android:drawable="@drawable/status_on"
android:maxLevel="1"/>
</levle-list>
【TransitionDrawable】
对应于<transition>标签,用于实现两个Drawable之间的淡入淡出效果
<!xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schema.android.com/apk/res/android">
<item android:drawable="@drawable/drawable1"/>
<item android:drawable="@drawable/drawable2"/>
</transition>
<TextView
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/transition_drawable"/>
通过startTransition和reverseTransition方法来实现淡入淡出效果
TextView textView = (TextView)findViewById(R.id.test_transition);
TransitionDrawable drawable = (TransitionDrawable)textView.getBackground();
drawable.startTransiton(1000);
【InsetDrawable】
对应于<inset>标签,可以将其他Drawable内嵌到自己当中,并可以在四周留出一定的间距(类似于padding或margin效果)
<!xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetBottom="15dp"
android:insetLeft="15dp"
android:insetRight="15dp"
android:insetTop="15dp">
<shape android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
</inset>
【ScaleDrawable】
对应于<scale>标签,根据自己的等级(level)将指定的Drawable缩放到一定比例
例:缩小为原大小的30%
<!xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/image1"
android:scaleHeight="70%"
android:scaleWidth="70%"
android:scaleGravity="center"/>
View testScale = findViewById(R.id.test_scale);
ScaleDrawable testScaleDrawable = (ScaleDrawable)testScale.getBackground();
testScaleDrawable.setLevel(1);
等级范围0~10000,必须设置为大于0
【ClipDrawable】
对应于<clip>标签,可以根据当前的等级level来剪裁另一个Drawable,剪裁方向可以通过clipOrientation(裁剪方向)和gravity两个属性共同控制
例:从上往下裁剪20%
<!xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="@drawable/image1"
android:gravity="bottom"/>
<ImageView
android:id="@+id/test_clip"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/clip_drawable"
android:gravity="center"/>
ImageView testClip = (ImageView)findViewById(R.id.test_clip);
ClipDrawable testClipDrawable = (ClipDrawable)testClip.getDrawable();
testClipDrawable.setLevle(8000);
等级level的范围0~10000,0表示完全裁剪,10000表示不裁剪
【自定义Drawable】
重写draw方法
当自定义Drawable有固定大小时最好重写getIntrinsicWidth和getIntrinsicHeight方法
《android开发艺术探索》读书笔记(六)--Drawable的更多相关文章
- Android开发艺术探索读书笔记——01 Activity的生命周期
http://www.cnblogs.com/csonezp/p/5121142.html 新买了一本书,<Android开发艺术探索>.这本书算是一本进阶书籍,适合有一定安卓开发基础,做 ...
- Android开发艺术探索读书笔记——进程间通信
1. 多进程使用场景 1) 应用某些模块由于特殊需求须要执行在单独进程中. 如消息推送,使消息推送进程与应用进程能单独存活,消息推送进程不会由于应用程序进程crash而受影响. 2) 为加大一个应用可 ...
- android开发艺术探索读书笔记之-------view的事件分发机制
View的点击事件的分发,其实就是对MotionEvent事件的分发过程,即当一个MotionEvent产生后,系统需要把这个事件传递给一个具体的View,而这个过程就是分发过程. 分发过程主要由以下 ...
- Android开发艺术探索学习笔记(六)
第六章 Android的Drawable Drawable的优点:使用简单,比自定义view的成本要低:非图片类型的Drawable占用空间小,有利于减小APK安装包的大小. 6.1Drawable ...
- Android开发艺术探索学习笔记(三)
第三章 View的事件体系 3.1 View基础知识 3.1.1 什么是view View 是Android中所有控件的基类,是一种界面层的控件的一种抽象,它代表了一个控件. 3.1.2 View的 ...
- Android开发艺术探索学习笔记(十一)
第十一章 Android的线程和线程池 从用途上来说,线程分为子线程和主线程,主线程主要处理和界面相关的事情,而子线程往往用于执行耗时的操作.AsyncTask,IntentService,Hand ...
- Android开发艺术探索学习笔记(十)
第十章 Android的消息机制 面试中经常会被问到的一个问题:handler是如何在子线程和主线程中进行消息的传递的,这个问题通过了解Android的消息机制可以得到一个准确的答案. Androi ...
- Android开发艺术探索学习笔记(四)
第四章 View的工作原理 4.1初识ViewRoot和DecorView ViewRoot是连接WindowManager和DecorView的纽带,View的三大流程均是通过ViewRoot来完成 ...
- Android开发艺术探索学习笔记(一)
第一章 Activity的生命周期和启动模式 1.1Activity的生命周期全面解析 1.1.1典型情况下的生命周期分析 (1)在两个Activity进行切换时,当前的Activity的onPaus ...
- 《Android开发艺术探索》读书笔记 (9) 第9章 四大组件的工作过程
第9章 四大组件的工作过程 9.1 四大组件的运行状态 (1)四大组件中只有BroadcastReceiver既可以在AndroidManifest文件中注册,也可以在代码中注册,其他三个组件都必须在 ...
随机推荐
- js 原型 对象篇
一切皆对象 js中 值类型就不是对象 剩下的都是对象(也就是引用类型) typeof()运算符 判断四种值类型 typeof 10; --> Number || typeof &quo ...
- JDK 中的设计模式应用实例
在 JDK(Java Development Kit)类库中,开发人员使用了大量设计模式,正因为如此,我们可以在不修改 JDK 源码的前提下开发出自己的应用软件.研究 JDK 类库中的模式实例也不 ...
- error_reporting
有关error_reporting()函数: error_reporting() 设置 PHP 的报错级别并返回当前级别. ; 错误报告是按位的,或者将数字加起来得到想要的错误报告等级. ; E_AL ...
- docker之NGINX镜像构建
Nginx是一个高性能的Web和反向代理服务器,它具有很多非常优越的特性:1.作为Web服务器.2.作为负载均衡服务器.3.作为邮件代理服务器.4.安装及配置简单.接下来我们介绍在docker构建ng ...
- Codeforces D. Sorting the Coins
D. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...
- java中线程的状态详解
一.线程的五种状态 线程的生命周期可以大致分为5种,但这种说法是比较旧的一种说法,有点过时了,或者更确切的来说,这是操作系统的说法,而不是java的说法.但对下面所说的六种状态的理解有所帮助,所以 ...
- Trusted Execution Technology (TXT) --- 基本原理篇
版权声明:本文为博主原创文章,未经博主允许不得转载. http://www.cnblogs.com/tsec/p/8409600.html 1. Intel TXT 介绍 TXT是Trusted Ex ...
- 前端 js技术
1.JavaScript代码存在形式 <!-- 方式一 --> <script type"text/javascript" src="JS文件" ...
- BZOJ 3309: DZY Loves Math [莫比乌斯反演 线性筛]
题意:\(f(n)\)为n的质因子分解中的最大幂指数,求\(\sum_{i=1}^n \sum_{j=1}^m f(gcd(i,j))\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d| ...
- BZOJ 1086: [SCOI2005]王室联邦 [树上分块]
portal 题意: 树分成若干块大小在$[s,3s]$之间,每块有一个根(可以不在块内),所有点到根路径上的点都必须在块内 据说这是一个保证了块大小直径个数的科学分块方法,貌似只有本题有用 我错了 ...