本文主要是介绍ImageView点击图片加上遮罩,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基本思路:
方法1.遮罩可直接叠加一层带alpha的纯白、或纯黑View,可直接在ImageView外套一层FrameLayout,其foreground(一般同学可能仅仅知道background,事实上framelayout还有foreground)给定一个selector(按下时给0x33000000,即泛黑),到位。
方法2.重写imageView,按下状态改变时invalidate(触发onDraw),onDraw中依据按下状态绘制0x33000000。注意imageView.setClickable(true),或imageView.setOnClickListener时才可触发dispatchSetPressed。
@Overridepublic 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);}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);if (isPressed())canvas.drawColor(0x33000000);}@Overrideprotected void dispatchSetPressed(boolean pressed) {// TODO Auto-generated method stubsuper.dispatchSetPressed(pressed);invalidate();}}
这篇关于ImageView点击图片加上遮罩的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!