1

I have an ImageView that I previously used to display images with

imageview.setImageResource(R.drawable.my_image);

I defined the scaleType to centerCrop in the XML-layout to keep the image's aspect ratio

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image_view"
        android:src="@drawable/my_image"
        android:scaleType="centerCrop" />

Now I need to use a drawable animation in the ImageView and that is set to the imageView's background.

Layout file:

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image_view"
        android:src="@android:color/transparent"
        android:scaleType="centerCrop" />

Java:

AnimationDrawable animDraw;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView imageview = (ImageView) findViewById(R.id.image_view);
  imageview.setBackgroundResource(R.drawable.my_animation);
  animDraw = (AnimationDrawable) imageview.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    animDraw .start();
    return true;
  }
  return super.onTouchEvent(event);
}

my_animation drawable

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/my_image1" android:duration="150" />
    <item android:drawable="@drawable/my_image2" android:duration="150" />
    <item android:drawable="@drawable/my_image3" android:duration="150" />
</animation-list>

The animation works fine but the drawable is now set to the background and I cannot change its scaleType to keep the aspect ratio.

Is there some way to get the animation to keep the aspect ratio?

QitVision
  • 81
  • 1
  • 8

1 Answers1

0
imageview.setImageResource((R.drawable.my_animation);

You have to set imageRecource instead of imageBackground.
Scale type doesn't work with setBackground.

For more: How do I set an ImageViews source programmatically in Android?

Pang
  • 9,564
  • 146
  • 81
  • 122
Sai Jayant
  • 366
  • 2
  • 14
  • Thanks for the response. I tried to set the animation with `imageview.setImageResource((R.drawable.my_animation);` The problem with this is that I cannot start the animation. I tried to get the reference to the animation with `animDraw = (AnimationDrawable) imageview.getBackground();` and `animDraw = (AnimationDrawable) imageview.getDrawable();` but it does not work with `animDraw.start();` – QitVision Aug 10 '17 at 08:36
  • You can try this or your animation file http://www.mavengang.com/2016/05/02/gif-animation-android/ – Sai Jayant Aug 10 '17 at 09:13
  • I have a drawable file "my_animation.xml" that is shown in the original post. It simply specifies the frame images and their duration. This animation works fine if it is set on the background but it doesn't run if it is set with setImageResource. I could try your gif approach but I thought I could maybe set the animation to the background and then reprogram ImageView's layout parameters to match the animations aspect ratio. Then stretching wouldn't be an issue with different screen sizes. – QitVision Aug 10 '17 at 12:12