public class MyImageView extends ImageView {
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
private StateListDrawable newSelector() {
StateListDrawable bg = new StateListDrawable();
Drawable normal = getDrawable();
Drawable pressed = changeBrightnessBitmap(((BitmapDrawable) getDrawable())
.getBitmap());
// View.PRESSED_ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_pressed,
android.R.attr.state_enabled }, pressed);
// View.ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled }, normal);
// View.EMPTY_STATE_SET
bg.addState(new int[] {}, normal);
return bg;
}
/**
* 传入改变亮度前的bitmap,返回改变亮度后的bitmap
*
* @param srcBitmap
* @return
*/
private Drawable changeBrightnessBitmap(Bitmap srcBitmap) {
Bitmap bmp = Bitmap.createBitmap(srcBitmap.getWidth(),
srcBitmap.getHeight(), Config.ARGB_8888);
int brightness = 60 - 127;
ColorMatrix cMatrix = new ColorMatrix();
cMatrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0,
brightness,// 改变亮度
0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 });
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
Canvas canvas = new Canvas(bmp);
// 在Canvas上绘制一个Bitmap
canvas.drawBitmap(srcBitmap, 0, 0, paint);
return new BitmapDrawable(bmp);
}
}
获取src资料为 getdrawable
ImageView自定义状态选择器 仿ios
最新推荐文章于 2023-07-27 11:33:28 发布