Drawabe是可绘制到屏幕上图形一种概述,屏幕上绘制的图形内容都会最终已Drawable形式提供,Drawable提供了多种展示形式,如下:
Bitmap: the simplest Drawable, a PNG or JPEG or,GIF image. -->BitmapDrawable
Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it. -->NinePatchDrawable
Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases. -->ShapeDrawable xml<shape>
Layers: a compound drawable, which draws multiple underlying drawables on top of each other. -->LayerDrawable xml<layer-list>
States: a compound drawable that selects one of a set of drawables based on its state. -->StateListDrawable xml
<
selector
>
Levels: a compound drawable that selects one of a set of drawables based on its level.
-->
LevelListDrawable xml<level-list>
Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level. -->ScaleDrawable xml<scale>
XML Bitmap:使用xml方式处理Bitmap,例如小图平铺背景
xml定义
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/icon" android:tileMode="repeat" />
titleMode为Bitmap重复填充方式,tileMode="clamp
"填充边缘色
XML Nine-Patch:
<?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"] />
LayerDrawable:管理一组drawable显示处理,例如:叠加图形的展示
xml定义:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/android_red" android:gravity="center" /> </item> <item android:top="10dp" android:left="10dp"> <bitmap android:src="@drawable/android_green" android:gravity="center" /> </item> <item android:top="20dp" android:left="20dp"> <bitmap android:src="@drawable/android_blue" android:gravity="center" /> </item> </layer-list>
layer-list阴影效果:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--底层阴影--> <!--top代表下边的阴影高度,left代表右边的阴影宽度 如果不做这个控制,底层和上层的左上会重合在一起--> <item android:left="2dp" android:top="2dp"> <shape android:shape="rectangle"> <!--使用渐变色处理阴影色度 angle 0 左边;90 下边,180,右边,270 上边--> <gradient android:angle="270" android:endColor="#0F000000" android:startColor="#0F000000"/> <corners android:radius="8dp"/> </shape> </item> <!-- 上层背景部分 上层的右边距离底层的右边3,距离底层底部3dp--> <item android:bottom="3dp" android:right="3dp"> <shape android:shape="rectangle"> <corners android:radius="8dp"/> </shape> </item> </layer-list>
可参考:https://blog.csdn.net/android_cmos/article/details/80033784
StateListDrawable:根据View的状态来展示不同状态下的Drawable
xml文件:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused --> <item android:state_hovered="true" android:drawable="@drawable/button_focused" /> <!-- hovered --> <item android:drawable="@drawable/button_normal" /> <!-- default --> </selector>
LevelListDrawable:展示几张图形,使用setLevel() or setImageLevel()
来展示不同Drawable
xml定义:
<?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" /> </level-list>
TransitionDrawable:限制两个Drawable,淡入淡出效果过度展示
xml定义:
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/on" /> <item android:drawable="@drawable/off" /> </transition>
使用:
ImageButton button = (ImageButton) findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable) button.getDrawable(); drawable.startTransition(500);
InsetDrawable:嵌入到另一个Drawable里,指定边距区域显示,例如:背景区域小于内容区可使用
xml文件:
<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:insetTop="dimension" android:insetRight="dimension" android:insetBottom="dimension" android:insetLeft="dimension" />
ScaleDrawable:对Drawable的尺寸进行拉伸处理
xml定义:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/logo" android:scaleGravity="center_vertical|center_horizontal" android:scaleHeight="80%" android:scaleWidth="80%" />
ClipDrawable:裁剪处理
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/android" android:clipOrientation="horizontal" android:gravity="left" />
/** * Drawable转化为Bitmap */ public static Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return bitmap; } /** * Bitmap to Drawable * @param bitmap * @param mcontext * @return */ public static Drawable bitmapToDrawble(Bitmap bitmap,Context mcontext){ Drawable drawable = new BitmapDrawable(mcontext.getResources(), bitmap); return drawable; }