Android之Drawable
2016-04-06 11:13
519 查看
Drawable
BitmapDrawable
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/ic_launcher" android:antialias="true" android:dither="true" android:filter="true" android:tileMode="mirror" > </bitmap>
Antialias属性表示是否开启抗锯齿功能一般为true,dither属性表示是否开启抖动,一般为true,filter属性表示是否开启过滤效果,tileMode属性表示平铺模式
ShapeDrawable
定义<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#00bcd4"/> <corners android:radius=""/> <padding android:left=""/> <size android:width=""/> <stroke /> <gradient /> </shape>
Shape属性有rectangle(矩形),oval(椭圆),line(横线),ring(圆环)
使用line或ring属性定义shapeDrawable时必须指定stroke标签线的宽度颜色。
Solid属性表示纯色填充。
Gradient属性表示渐变色填充,其具体属性有:
Angle渐变角度默认为0,必须为45的倍数,0表示从左到右,90表示从上到下。
centerX渐变中心的横坐标
centerY渐变中心的纵坐标
startColor渐变的起始颜色
centerColor渐变的中间颜色
endColor渐变的结束颜色
gradientRadius渐变半径 仅当type=radial时有效
type渐变类别其值有 linear(线性渐变),radial(径向渐变),sweep(扫描渐变)。
Stroke属性表示描边
<stroke android:width="2dp" android:dashWidth="2dp" // 表示虚线的宽度 android:dashGap="4dp" //表示虚线之间的间隔 android:color="#00bcd4"/>
size属性表示shapeDrawable的固有大小(非最终显示大小),一般shapedrawable通过getIntrinsicHeight跟getIntrinsicWidth时会返回-1,当设置size属性时会返回其设置的值。
LayerDrawable
Layerdrawable 对应xml标签的layer-list,表示层次化的drawable集合。通过将不同的Drawable放置在不同层上面从而达到一种叠加后的效果。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/ic_launcher" android:id="" android:top="" android:right="" android:bottom="" android:left="" > </item> </layer-list>
其中 top,right,bottom,left分别表示相对于view上下左右的偏移量。
例子:仿照微信输入框效果
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > //第一个item将背景填充成蓝色 <item > <shape android:shape="rectangle"> <solid android:color="#00bcd4"/> </shape> </item> //第二个item将距离底部6dp的位置填充为白色,底部6dp内为蓝色 <item android:bottom="6dp"> <shape android:shape="rectangle"> <solid android:color="#ffffff"/> </shape> </item> //第三个item将距离底部1dp,左右1dp的位置填充为白色,所以只留下左右高6dp宽1dp的蓝色线,底部1dp的蓝色线 <item android:bottom="1dp" android:left="1dp" android:right="1dp"> <shape android:shape="rectangle"> <solid android:color="#ffffff"/> </shape> </item> </layer-list>
StateListDrawable
对应selector标签,页表示drawable的集合。每个drawable对应view的一种状态。<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="" android:dither="" android:variablePadding="" > <item android:state_focused="" //表示view获得了焦点 android:state_pressed=""//表示button按下后没有松开状态 android:state_active="" android:state_checked="" android:state_window_focused="" <span style="white-space:pre"> </span>android:drawable="" . . > <shape > . . . </shape> </item> </selector>
constantSize表示StateListDrawable的固有大小是否随着状态改变而保持不变。
variablePadding表示StateListDrawable的padding是否会随着其状态改变而改变。
LevelListDrawable
等级范围由0~10000,0是默认等级,imageview可通过setImageLevel来切换drawable。<level-list> <item android:drawable=”” android:maxLevel=”” android:minLevel=”” /> <item android:drawable=”” android:maxLevel=”” android:minLevel=”” /> </level-list>
InsetDrawable
InsetDrawable对应于inset标签。可以将drawable内嵌到自己当中,并且留一定距离。<inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetLeft="5dp" android:insetRight="5dp" android:insetTop="5dp" android:insetBottom="5dp" android:drawable="" > <item> <shape > </shape> </item> </inset>
ScaleDrawable
Scaledrawable对应于scale标签,可根据自己的等级将drawable缩放到一定比例。<scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/ic_launcher" android:scaleGravity="center" android:scaleHeight="50%" android:scaleWidth="50%" > </scale>
等级对scaledrawable的影响很大,默认等级为0,当等级为0时scaledrawable不可见(从android源码处可见)
Public void draw(Canvas canvas) { if(mScaleState.mDrawable.getLevel()!=0) mScaleState.mDrawable.draw(canvas); }
所以在使用之前,必须设置scaledrawable的level大于0(等级范围0-10000)
ScaleDrawable scaledrawable=view.getBackground(); scaledrawable.setlevel(1);
ClipDrawable
Clipdrawable对应clip标签,可以根据自己的等级(范围0-10000,0表示不裁剪,5000表示裁剪50%)来裁剪drawable.裁剪方向由clipOriention跟gravity共同决定。其中gravity属性值之间可以通过’|’连接。用法:
1.
<clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="vertical" android:drawable="@drawable/circle" android:gravity="bottom" > </clip>
2.
<ImageView android:id="@+id/clip_img" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/clip" />
3.
Clip_img=(ImageView)findViewById(R.id.clip_img); ClipDrawable clipDrawable=(ClipDrawable) clip_img.getDrawable(); clipDrawable.setLevel(5000);
自定义Drawable
自定义drawable是通过重写drawable的draw方法实现。缺点:自定义的drawable无法在xml中使用。
public class CustomDrawable extends Drawable { Paint mPaint; public CustomDrawable() { mPaint=new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.RED); } @Override public void draw(Canvas canvas) { Rect rect = getBounds(); float cx=rect.exactCenterX(); float cy=rect.exactCenterY(); canvas.drawCircle(cx, cy, Math.min(cx, cy), mPaint); } @Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); invalidateSelf(); } @Override public void setColorFilter(ColorFilter colorFilter) { mPaint.setColorFilter(colorFilter); invalidateSelf(); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } }
相关文章推荐
- [Android] Android Studio 安裝模擬器的小問題
- 2016.4.6 Android技术集萃
- android的m、mm、mmm编译命令的使用
- Android实现EditText输入金额(去除首位“0”)
- Android 开发资料整理
- 使用ccache机制加快Android重新编译速度
- Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment
- android——inflater 用法(转)
- Android Studio插件实现语言国际化
- Android的消息机制
- 使用Gradle管理你的Android Studio工程
- Android手机通讯录不正常,时常崩溃
- Android Studio多渠道打包和代码混淆教程
- Android 百分比布局库(percent-support-lib) 解析与扩展
- Android群侠传第三章阅读笔记(一)
- 7.Android常用第三方支付
- android多线程学习---Android Looper和Handler分析
- 关于Android Studio加载.so文件问题,亲测有效
- 第一次使用Android Studio时你应该知道的一切配置(三):gradle项目构建
- 第一次使用Android Studio时你应该知道的一切配置(二):新建一个属于自己的工程并安装Genymotion模拟器