接上篇《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的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. 《Android开发艺术探索》读书笔记 (9) 第9章 四大组件的工作过程

    第9章 四大组件的工作过程 9.1 四大组件的运行状态 (1)四大组件中只有BroadcastReceiver既可以在AndroidManifest文件中注册,也可以在代码中注册,其他三个组件都必须在 ...

随机推荐

  1. ECLIPS-S测井系统下的仪器挂接 [TCC模块]

    1. 环境 HPUX版本:11.23 Complete Image ECLIPS版本:Rel 5.1i 2. 效果图 3. 用途 为以后在此系统中挂接新仪器打下坚实的基础. 4. 参考资料 ECLIP ...

  2. Servlet--HttpServletRequest接口,HttpServletResponse接口

    HttpServletRequest接口 定义 public interface HttpServletRequest extends ServletRequest; 用来处理一个对 Servlet ...

  3. alibaba架包FastJson使用例子

    alibaba的架包FastJson可以对json字符串进行快捷的类型转换.下面是一些各种类型转换的使用例子. 一.下载FastJson的架包,并导入项目中,如下: Maven项目pom.xml配置如 ...

  4. 在 ios 中的日期格式

    var d="2017-1-1" ; new Date(d) //生成一个日期对象 这样写在 Android 中没有问题,但是在 ios 中,d  的格式不对,应该设为 2017- ...

  5. MySQL相关命令与备份

    不加任何参数直接备份 mysqldump -uroot zabbix >/opt/zabbix.bak.sql 恢复,这样恢复时需要自已创建表 mysql -uroot < zabbix. ...

  6. SQL FOR XML PATH 和 Stuff 用法

    sql stuff 用法 1.作用 删除指定长度的字符,并在指定的起点处插入另一组字符. 2.语法 STUFF ( character_expression , start , length ,cha ...

  7. Notepad++ 运行java(转)

    Notepad++ 运行java java, 2013/05/04, 9 replies, 6,007 views 文章目录 Notepad++ for java 安装必须的程序 配置NppExec ...

  8. SpringMVC空字符串转为null

    空字符串转为null 现在我遇到这样一个需求,那就是我想要吧前端传过来的值变为空,因为所谓前端的校验,其实都不是校验,如果前端传给后台一个表单,可是表单未填入值,我们后台进行判断的时候 既需要判断nu ...

  9. oracle用户与表空间操作

    oracle系统用户sys,system , sysman, scott 使用system用户登录[username/password][@server][as sysdba|sysoper]eg: ...

  10. 1.Tarball软件make与makefile详解(还需要补充)

    *通常自己安装的软件放在 /usr/local/软件名   中,而将源文件放在/usr/local/src *为安装到单独目录的软件之 man page 加入 man path 搜寻: 如果你安装的软 ...