Android之Drawable

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;
}


}


转载于:https://my.oschina.net/sunqiyao/blog/664418

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值