Search code examples
androidimageviewgifandroid-ion

how can I resize gif with custom height and width in android?


I am using Koush/Ion library to load gif from internet into imageview but I cannot resize the gifs aspectly, upon calling resize function, gif becomes still

so my question is, is there any way I can resize gif aspectly in android ?

here's my calculation to get aspect height and width of image if that helps

final int newWidth = deviceWidth;
final int newHeight = (int) ((int) newWidth * (imageHeight / imageWidth));

Solution

  • I think I finally found the solution, First scale image with FIT_XY and then set 'newHeight' as the height of image view

    imageView.setScaleType(ScaleType.FIT_XY);
    imageView.getLayoutParams().height = newHeight; 
    

    Code Snippet

    Ion.with(imageView)
        .animateGif(AnimateGifMode.ANIMATE)
        .load(imgUrl)
        .setCallback(new FutureCallback < ImageView > () {
    
        @SuppressLint("NewApi")
        @Override
        public void onCompleted(Exception arg0,
        ImageView arg1) {
    
            imageView.getViewTreeObserver().addOnPreDrawListener(
            new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
    
                    imageView.getViewTreeObserver()
                        .removeOnPreDrawListener(this);
    
                    imageView.setScaleType(ScaleType.FIT_XY);
                    imageView.getLayoutParams().height = newHeight;
    
                    return true;
                }
            });
        }
    });