【安卓特效】怎样给ImageView加上遮罩,点击时泛黑、或泛白、?

基本思路:

方法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();
}
}

【安卓特效】怎样给ImageView加上遮罩,点击时泛黑、或泛白、?

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FybGluMzIx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">【安卓特效】怎样给ImageView加上遮罩,点击时泛黑、或泛白、?

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FybGluMzIx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

上一篇:SQL知识整理二:锁、游标、索引


下一篇:[转载]jquery中attr和prop的区别