2

Is there away to show some kind of effect when imageview pressed. I use imageview as a button, so need to show some effect on touch. Many existing threads propose selectors with multiple images. This is too much work, because I have a lot of imageviews. I am also open to extending the imageview class. Any ideas or workaround.

pats
  • 1,273
  • 2
  • 20
  • 43

1 Answers1

9

In a click function add

 ImageView img= (ImageView) view.findViewById(R.id.theID);
 Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
 img.startAnimation(animation);

Create an anim folder in Assets and then cleate an animation resource file named alpha

inside the file paste the following

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:duration="50"
    android:fromAlpha="0.5"
    android:toAlpha="1.0" />

This type of animation just sets the Alpha tranparency to 50% and back to 100% so the image will flash.

There are many animations so have a look around on the net to find which one you like and create the anim resource file and put the name here R.anim.alpha);

in your imageView xml file you can add android:onClick="myClickFunction"

and then in the activity add

public void myClickFunction(View v) {
    ImageView img = (ImageView) v.findViewById(R.id.theID);
     Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
     img.startAnimation(animation);   
}
Tasos
  • 5,321
  • 1
  • 15
  • 26