基本思路:
方法1.遮罩可直接叠加一层带alpha的纯白、或纯黑View,可直接在ImageView外套一层FrameLayout,其foreground(一般同学可能只知道background,其实framelayout还有foreground)给定一个selector(按下时给0x33000000,即泛黑),到位。
方法2.重写imageView,按下状态改变时invalidate(触发onDraw),onDraw中根据按下状态绘制0x33000000。注意imageView.setClickable(true),或imageView.setOnClickListener时才可触发dispatchSetPressed。
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 取id // imgTest = (ImageView) findViewById(R.id.imgTest); loMain = (RelativeLayout) findViewById(R.id.loMain); findViewById(R.id.btn1).setOnClickListener(this); findViewById(R.id.btn2).setOnClickListener(this); MyImg myImg = new MyImg(this); myImg.setImageResource(R.drawable.meinv); myImg.setClickable(true); loMain.addView(myImg); } private class MyImg extends ImageView { public MyImg(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (isPressed()) canvas.drawColor(0x33000000); } @Override protected void dispatchSetPressed(boolean pressed) { // TODO Auto-generated method stub super.dispatchSetPressed(pressed); invalidate(); } }