【问题标题】:how can I resize gif with custom height and width in android?如何在Android中使用自定义高度和宽度调整GIF的大小?
【发布时间】:2015-07-16 02:04:54
【问题描述】:

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

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

如果有帮助,这是我获取图像纵横比高度和宽度的计算方法

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

【问题讨论】:

    标签: android imageview gif android-ion


    【解决方案1】:

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

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

    代码片段

    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】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2019-11-18
        • 2017-12-08
        • 1970-01-01
        • 1970-01-01
        • 2012-10-22
        • 2012-05-24
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多