Android Imageview with AnimationDrawable source change dimensions

  • I made a set of three images that will represent the movement of the animal. Each image is a frame and consists only of an animal with a transparent background.

  • I made an AnimationDrawable in code and created an animation:

    AnimationDrawable staticAnimation = new AnimationDrawable ();
    staticAnimation.addFrame (ctx.getResources (). getDrawable ("frame1"), 150);
    staticAnimation.addFrame (ctx.getResources (). getDrawable ("frame2"), 150);
    staticAnimation.addFrame (ctx.getResources (). getDrawable ("frame3"), 150);
  • I set the animation as background for Imageview and run it

    imgAnimal.setBackgroundDrawable (staticAnimation);
    staticAnimation.start ()

Since frames have different widths (the drawing should recreate the movement steps), it seems that Imageview resizes images in the animation. Better to say, if frame1 is larger than frame2, frame 1 is displayed normally, but frame 2 is stretched so that it is equal to frame1. It looks very awful.

Any suggestions on how to stop this behavior? Thank.

+3
source share
1 answer

The solution was quite simple, because the images in the frames have the same height, but different widths, I just made a transparent rectangle and placed it on top of each frame. In this case, each frame will have the same width, and the animatin will work as intended.

+2
source

Source: https://habr.com/ru/post/1789314/


All Articles