圖像級別組件 android
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/bottle_pick_bg_spotlight_day"
android:maxLevel="10"
android:minLevel="6"/>
<item
android:drawable="@drawable/bottle_pick_bg_spotlight_night"
android:maxLevel="20"
android:minLevel="12"/>
</level-list> canvas
代碼調用ImageView.setImageLevel(15); ide
實現圖像的半透明效果 spa
代碼: .net
1.attrs.xml code
<?xml version="1.0" encoding="utf-8"?>
<resources> orm
<declare-styleable name="abc">
<attr name="RID" format="integer" />
<attr name="ALPHA" format="integer" />
</declare-styleable> xml
</resources> blog
2.main.xml utf-8
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cyx="http://schemas.android.com/apk/res/com.b.a" //注意是程序包名
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.b.a.ImageViewExt
android:layout_width="wrap_content"
android:layout_height="wrap_content"
cyx:ALPHA="50"
cyx:RID="0x7f020000" >
</com.b.a.ImageViewExt>
</LinearLayout>
3.自定義類
public class ImageViewExt extends View {
Bitmap bitmap;
int alpha;
public ImageViewExt(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.abc);
int value = typedArray.getInt(R.styleable.abc_RID, 0);
alpha = typedArray.getInt(R.styleable.abc_ALPHA, 0);
typedArray.recycle();
bitmap = BitmapFactory.decodeResource(getResources(), value);
System.out.println();
}
public ImageViewExt(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.abc);
int value = typedArray.getInt(R.styleable.abc_RID, 0);
alpha = typedArray.getInt(R.styleable.abc_ALPHA, 0);
typedArray.recycle();
bitmap = BitmapFactory.decodeResource(getResources(), value);
System.out.println();
}
public ImageViewExt(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAlpha(alpha);// 0-255來自於自定義的屬性
// 繪製半透明的圖像
canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), paint);
}
}