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定义
titleMode为Bitmap重复填充方式,tileMode="clamp
"填充边缘色
XML Nine-Patch:
LayerDrawable:管理一组drawable显示处理,例如:叠加图形的展示
xml定义:
StateListDrawable:根据View的状态来展示不同状态下的Drawable
xml文件:
LevelListDrawable:展示几张图形,使用setLevel() or setImageLevel()
来展示不同Drawable
xml定义:
TransitionDrawable:限制两个Drawable,淡入淡出效果过度展示
xml定义:
使用:
ImageButton button = (ImageButton) findViewById(R.id.button);TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();drawable.startTransition(500);
InsetDrawable:嵌入到另一个Drawable里,指定边距区域显示,例如:背景区域小于内容区可使用
xml文件:
ScaleDrawable:对Drawable的尺寸进行拉伸处理
xml定义:
ClipDrawable:裁剪处理
/** * 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; }