《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文件中注册,也可以在代码中注册,其他三个组件都必须在 ...
随机推荐
- 三、Html常用标签
1,基本标签 <html>:html文档的根元素,可以指定一个xmlns属性,值只能是http://www/w3.org/1999/xhtml. <body>:页面主体部分 & ...
- (纯代码)快速创建wcf rest 服务
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBind ...
- CURL模拟post请求上传文件
贴一段醍醐灌顶的话: 逻辑很简单,设置POST变量$post_data,其中upload指向需要发送的文件.这里要注意,我们之前使用POST都是发送一个字符串,然后在服务器端使用file_get_co ...
- 【PHP】 PHPqrCode二维码类库使用方法
1.首先去官网下载PHPqrCode库文件,只需要里面的phpqrcode.php文件,下载地址:http://phpqrcode.sourceforge.net 2.二维码生成实例代码: <? ...
- Zabbix3.4部署
Zabbix简介 zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活 ...
- 浅谈最大流的Dinic算法
PART 1 什么是网络流 网络流(network-flows)是一种类比水流的解决问题方法,与线性规划密切相关.网络流的理论和应用在不断发展,出现了具有增益的流.多终端流.多商品流以及网络流的分解与 ...
- encodeURI()和encodeURIComponent()
encodeURI() 返回值 URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换. 说明 该方法会替换所有的字符,但不包括以下字符,即使它们具有适当的UTF-8转义序列: 保留 ...
- 浅谈python模块的导入操作
1.什么是模块 在Python中有一个概念叫做模块(module). 所谓模块,就是将代码量较大的程序分割成多个有组织的,彼此独立但双能互相交互的代码片段, 这些自我包含的有组织的代码段就是模块. 2 ...
- eclipse open call hierarchy无效
问题: Eclipse中选中一个方法,右击选中open call hierarchy,不显示哪些地方调用了这个方法,却显示了这个方法里面调用了那些方法.前阵子还是好的,现在不知道为什么了. 解决: s ...
- Linux下查看CPU、内存和硬盘信息命令
一.查看cpu信息 cat /proc/cpuinfo 相同physical id 的记录是属于同一个CPU的,对应于多核的信息. 二.查看内存的信息 cat /proc/meminfo 三.查看硬盘 ...