Strange effect with ImageView startAnimation in ListView

after several months of coming here from time to time, finally it’s my turn to introduce my problem:

I have a ListView with a custom ArrayAdapter that downloads images from the Internet (one image per line). I created an ImageCache class that calls the onImageLoaded method in my ArrayAdapter array:

public void onImageLoaded(Bitmap image, ImageView view){ view.setImageBitmap(image); Utils.log("start animation : " + view.toString()); view.startAnimation(mAnim); } 

The problem is that every time startAnimation (presumably fadeIn) is called in the ONE ImageView, it seems that the animation starts again from the very beginning during the ALL ImageView animation, causing some (or all) of the images to scroll weirdly when scrolling.

Utils.log says that startAnimation is usually called (i.e. only once for each new ImageView appearing in the ListView).

The content of my XML animation is as follows:

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="400"/> </set> 

Has this happened to anyone? Do you see what I'm doing wrong?

Thanks!

+4
source share
1 answer

The problem is loading the animation and saving it instead of creating a new animation every time.

Example solution:

  public void onImageLoaded (Bitmap image, ImageView view) {

     view.setImageBitmap (image);
     Animation fadeInAnimation = AnimationUtils.loadAnimation (getContext (), R.anim.fade_in);
     view.startAnimation (fadeInAnimation);
 }

Note the dependence of this line of code on the method above:

  Animation fadeInAnimation = AnimationUtils.loadAnimation (getContext (), R.anim.fade_in); 
+2
source

All Articles