图像级别组件
<?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>
代码调用ImageView.setImageLevel(15);
实现图像的半透明效果
代码:
1.attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="abc">
<attr name="RID" format="integer" />
<attr name="ALPHA" format="integer" />
</declare-styleable>
</resources>
2.main.xml
<?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);
}
}
参考http://blog.youkuaiyun.com/longvslove/article/details/7084537