如何在android中调整自定义高度和宽度的gif?

时间:2022-11-19 13:57:31

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

我正在使用Koush / Ion库将gif从互联网加载到imageview中,但我无法调整gif的大小调整,在调用resize函数时,gif变得静止

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

所以我的问题是,有什么办法可以在android中调整gif的大小吗?

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));

2 个解决方案

#1


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

我想我终于找到了解决方案,第一个使用FIT_XY缩放图像,然后将'newHeight'设置为图像视图的高度

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;
            }
        });
    }
});

#2


You can resize a gif in android programatically. When you do so the gif does not animate as it becomes a normal Image. In order to resize the gif and animate it, the only suggestion that I can give you is to use online resizing tools like

您可以以编程方式在android中调整gif的大小。当你这样做时,gif没有动画,因为它变成了普通的Image。为了调整gif的大小并为其设置动画,我能给你的唯一建议是使用在线调整大小的工具,如

Later save the resized gif in your assets folder and then call that using the Ion library which you use like this

稍后将已调整大小的gif保存在您的assets文件夹中,然后使用您使用的Ion库调用它

Ion.with(yourImageView).load("yourAssetFolder/yourGifImage.gif")

#1


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

我想我终于找到了解决方案,第一个使用FIT_XY缩放图像,然后将'newHeight'设置为图像视图的高度

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;
            }
        });
    }
});

#2


You can resize a gif in android programatically. When you do so the gif does not animate as it becomes a normal Image. In order to resize the gif and animate it, the only suggestion that I can give you is to use online resizing tools like

您可以以编程方式在android中调整gif的大小。当你这样做时,gif没有动画,因为它变成了普通的Image。为了调整gif的大小并为其设置动画,我能给你的唯一建议是使用在线调整大小的工具,如

Later save the resized gif in your assets folder and then call that using the Ion library which you use like this

稍后将已调整大小的gif保存在您的assets文件夹中,然后使用您使用的Ion库调用它

Ion.with(yourImageView).load("yourAssetFolder/yourGifImage.gif")