Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Opacity in pressed state of image

I using an imageview for button. For normal state it is simple red color image with text on it , in pressed state i want to change the opacity of image.

enter image description here

Now in press state i want to decrease the opacity of this image . To do so the option i know is 1. create an other image with required opacity and use selector to get the effect 2. use color codes for both the states in selector ; But here i already have one image state as image , for the next state i want to decrease the opacity in this image only .

like image 462
Bora Avatar asked Jun 07 '13 14:06

Bora


People also ask

How do you change opacity in XML?

Create a drawable xml resource with bitmap as the root say bg. set android:src="@drawable/background" to the image which you want as the background. set android:alpha="0.4" to any value between 0 to 1 as per the required opacity.


1 Answers

button.setOnTouchListener(this);
@Override
public boolean onTouch(View v, MotionEvent event) {
  if (v == button) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      v.setAlpha(0.5f);
    } else {
      v.setAlpha(1f);
    }
  }
  return false;
}
like image 141
selva_pollachi Avatar answered Sep 30 '22 01:09

selva_pollachi