1.BitmapDrawable
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
<!--圖片的資源 -->
android:src="@drawable/create"
<!--是否抗鋸齒-->
android:antialias="true"
<!--是否開(kāi)啟抖動(dòng)效果-->
android:dither="true"
<!--開(kāi)啟過(guò)濾效果-->
android:filter="true"
<!--圖片大小和容器不符合時(shí)圖片擺放位置嚼松,拉伸哲银,裁剪-->
android:gravity="
<!--圖片大小不發(fā)生變化-->
top|bottom|left|right
|center_vertical|center_horizontal|center
<!--圖片大小發(fā)生變化-->
|fill_vertical|fill_horizontal|fill
|clip_vertical|clip_horizontal"
<!--紋理映射,圖像處理技術(shù),一般用不到-->
android:mipMap="false"
<!--平鋪模式,開(kāi)啟此屬性gravity會(huì)失效
disabled 關(guān)閉
clamp 圖片四周像素會(huì)擴(kuò)散到周?chē)鷧^(qū)域
repeat 簡(jiǎn)單的水平和豎直方向重復(fù)圖片
mirror 圖片倒立重復(fù)圖片
-->
android:tileMode=" ["disabled"|"clamp"|"repeat"|"mirror"]"
>
</bitmap>
2.NinePatchDrawable
點(diǎn)9圖可以自動(dòng)根據(jù)所需的寬高進(jìn)行相應(yīng)的縮放并保證不失真
屬性基本和BitmapDrawable相同
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/create"
>
</nine-patch>
3.ShapeDrawable
在代碼中生成的是GradientDrawable對(duì)象
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
<!--圖形 可以是rectangle(矩形)
oval(橢圓)
line(線)
ring(圓環(huán))
-->
android:shape=""
<!--以下是圓環(huán)特有屬性
圓環(huán)內(nèi)徑-->
android:innerRadius="@dimen/activity_horizontal_margin"
<!--內(nèi)徑所占整個(gè)Drawable的比例 默認(rèn)為9,如果為n,那么內(nèi)半徑=寬度/n,以innerRadius為主-->
android:innerRadiusRatio="1"
<!--圓環(huán)厚度-->
android:thickness="@dimen/activity_horizontal_margin"
<!--厚度所占整個(gè)Drawable的比例 默認(rèn)為3槽奕,如果為n,那么厚度=寬度/n,以thickness為主-->
android:thicknessRatio="1"
<!--除非用于LevelListDrawable一般為false-->
android:useLevel="false"
>
<!--Drawable的內(nèi)在寬高-->
<size android:height="@dimen/activity_horizontal_margin"
android:width="@dimen/activity_horizontal_margin"/>
<!--包含這個(gè)Drawable的View的空白-->
<padding
android:top="@dimen/activity_horizontal_margin"
android:bottom="@dimen/activity_horizontal_margin"
android:left="@dimen/activity_horizontal_margin"
android:right="@dimen/activity_horizontal_margin"
/>
<!--適用于rectangle四個(gè)角的圓角半徑-->
<corners
android:radius="@dimen/activity_horizontal_margin"
android:topLeftRadius="@dimen/activity_horizontal_margin"
android:topRightRadius="@dimen/activity_horizontal_margin"
android:bottomRightRadius="@dimen/activity_horizontal_margin"
android:bottomLeftRadius="@dimen/activity_horizontal_margin"
/>
<!--顏色填充使用純色-->
<solid android:color="@color/colorAccent"/>
<!--顏色填充使用漸變色-->
<gradient
<!--漸變類(lèi)型
linear 線性漸變(默認(rèn))
radial 徑向漸變
sweep 掃描線漸變
-->
android:type=""
<!-- 漸變的角度默認(rèn)為0房轿,其值必須為45的倍數(shù)
0代表從左到右
90代表從下到上-->
android:angle="0"
<!--漸變中心點(diǎn)坐標(biāo)-->
android:centerX="1"
android:centerY="1"
android:startColor="@color/colorAccent"
android:centerColor="@color/colorAccent"
android:endColor="@color/colorAccent "
<!--漸變半徑 僅當(dāng)type等于radial有效-->
android:gradientRadius="1"
<!--除非用于LevelListDrawable一般為false-->
android:useLevel="false"
/>
<!--描邊-->
<stroke
<!--描邊寬度-->
android:width="@dimen/activity_horizontal_margin"
<!--描邊顏色-->
android:color="@color/colorAccent"
<!--設(shè)置為虛線描邊寬度與dashGap同時(shí)使用才能生效-->
android:dashWidth="@dimen/activity_horizontal_margin"
<!--描邊虛線間隔空隙-->
android:dashGap="@dimen/activity_horizontal_margin"
/>
</shape>
4.LayerDrawable
一種不同層次的Drawable的集合粤攒,放置不同層面而達(dá)到疊加后的效果,一個(gè)layer-list可包含多個(gè)item
默認(rèn)情況下囱持,layer-list中所有的View都會(huì)被縮放至View的大泻唤印(對(duì)于bitmap可通過(guò)android:gravity控制圖片顯示效果),下面的item會(huì)覆蓋上面的item。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
<!--可引用一個(gè)drawable資源-->
android:drawable="@drawable/drawable_bitmap"
<!--drawable相對(duì)于View的偏移量-->
android:top="@dimen/activity_horizontal_margin"
android:bottom="@dimen/activity_horizontal_margin"
android:left="@dimen/activity_horizontal_margin"
android:right="@dimen/activity_horizontal_margin"
>
<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>
5.StateListDrawable
不同狀態(tài)選擇不同drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<!--StateListDrawable固有大小是否隨著drawable的切換而發(fā)生變化為true不發(fā)生變化纷妆,為item中drawable最大的固有大小 默認(rèn)為false-->
android:constantSize="true"
<!--是否開(kāi)啟防抖效果-->
android:dither="true"
<!--StateListDrawable的padding是否隨著drawable的切換而發(fā)生變化盔几,為false不變是所有item中drawable padding的最大值,默認(rèn)為false-->
android:variablePadding="true"
>
<item android:drawable="@drawable/create" android:state_pressed="true"/>
<item android:drawable="@drawable/create" android:state_focused="true" />
<item android:drawable="@drawable/create" android:state_checked="true" />
<!--默認(rèn)-->
<item android:drawable="@drawable/create" />
</selector>
6.LevelListDrawable
Drawable的集合掩幢,每個(gè)Drawable都可設(shè)置最小level和最大level逊拍,View的背景可通過(guò)getBackground().setLevel()設(shè)置不同了Level匹配不同的Drawable
ImageView的話可通過(guò)setIamgeLevel()匹配不同的Drawable
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/create"
android:minLevel="0"
android:maxLevel="0"
>
</item>
<item android:drawable="@drawable/b_now" android:maxLevel="1"/>
<item android:drawable="@drawable/tap_launch" android:maxLevel="2"/>
<item android:drawable="@drawable/uilib" android:maxLevel="3"/>
</level-list>
7.TransitionDrawable
只能實(shí)現(xiàn)兩個(gè)Drawable的淡入淡出效果
默認(rèn)顯示item1
TransitionDrawable drawable= (TransitionDrawable) mIvLevel.getBackground();
if (!mBoolean)
{
//從item1淡出,item2淡入
drawable.startTransition(1000);
mBoolean=true;
}
else
{
//反轉(zhuǎn)
drawable.reverseTransition(1000);
mBoolean=false;
}
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/b_now"
android:top="@dimen/activity_horizontal_margin"
android:bottom="@dimen/activity_horizontal_margin"
android:left="@dimen/activity_horizontal_margin"
android:right="@dimen/activity_horizontal_margin"
></item>
<item android:drawable="@drawable/create"></item>
</transition>
8.InsetDrawable
把Drawable內(nèi)嵌到自己當(dāng)中
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/create"
android:insetBottom="@dimen/activity_horizontal_margin"
android:insetLeft="@dimen/activity_horizontal_margin"
android:insetRight="@dimen/activity_horizontal_margin"
android:insetTop="@dimen/activity_horizontal_margin"
>
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent"></solid>
</shape>
</inset>
9.ScaleDrawable
適合縮小一個(gè)特定的drawable
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/create"
<!--縮放寬高比例越大 Drawable顯示越小-->
android:scaleWidth="1%"
android:scaleHeight="50%"
android:scaleGravity="center"
<!--level取值0到10000 為0不顯示际邻,level越大Drawable顯示越大-->
android:level="1"
>
</scale>
10.ClipDrawable
裁剪Drawable 由clipOrientation和gravity共同決定裁剪位置
ClipDrawable clipDrawable= (ClipDrawable)
mIvLevel.getDrawable();
//level取值0到10000,取值越大裁剪越少 0完全裁剪芯丧,10000表示不裁剪
clipDrawable.setLevel(5000);
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/create"
<!--裁剪的方向-->
android:clipOrientation="vertical"
android:gravity="center"
>
</clip>
自定義Drawable
public class CustomDrawable extends Drawable {
private Paint mPaint;
public CustomDrawable(int color) {
mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(color);
}
@Override
public void draw(Canvas canvas) {
final 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);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
圓形Drawable
public class CircleImageDrawable extends Drawable {
private Paint mPaint;
private int mWidth;
private Bitmap mBitmap ;
public CircleImageDrawable(Bitmap bitmap)
{
mBitmap = bitmap ;
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(bitmapShader);
mWidth = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
}
@Override
public void draw(Canvas canvas)
{
canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);
}
@Override
public int getIntrinsicWidth()
{
return mWidth;
}
@Override
public int getIntrinsicHeight()
{
return mWidth;
}
@Override
public void setAlpha(int alpha)
{
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf)
{
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity()
{
return PixelFormat.TRANSLUCENT;
}
}
圓角矩形Drawable
public class RoundImageDrawable extends Drawable {
private Paint mPaint;
private Bitmap mBitmap;
private RectF rectF;
public RoundImageDrawable(Bitmap bitmap)
{
mBitmap = bitmap;
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(bitmapShader);
}
@Override
public void setBounds(int left, int top, int right, int bottom)
{
super.setBounds(left, top, right, bottom);
rectF = new RectF(left, top, right, bottom);
}
@Override
public void draw(Canvas canvas)
{
canvas.drawRoundRect(rectF, 30, 30, mPaint);
}
@Override
public int getIntrinsicWidth()
{
return mBitmap.getWidth();
}
@Override
public int getIntrinsicHeight()
{
return mBitmap.getHeight();
}
@Override
public void setAlpha(int alpha)
{
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf)
{
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity()
{
return PixelFormat.TRANSLUCENT;
}
}