【问题标题】:How to load animated gif from drawable in ImageView using binding adapter?如何使用绑定适配器从 ImageView 中的 drawable 加载动画 gif?
【发布时间】:2021-04-26 02:07:40
【问题描述】:

我想在 ImageView 中显示一个动画文件。但是我的 gif 文件动画不起作用。

【问题讨论】:

    标签: android gif android-binding-adapter


    【解决方案1】:

    我们可以使用最新的 Glide 库来做到这一点。将库依赖项添加到您的项目中。

    定义绑定适配器方法如下

     @BindingAdapter(value={"imageUrl", "placeholder"}, requireAll=false)
        public static void setImageUrl(ImageView imageView, String url,
                Drawable placeHolder) {
            if (url == null) {
                imageView.setImageDrawable(placeholder);
            } else {
                MyImageLoader.loadInto(imageView, url, placeholder);
            }
        }
    

    在 ImageView 中添加标签,如下所示

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:imageUrl="@{product.imageUrl}"
            app:placeholder="@{@drawable/shadowAvatar}"/>
    

    现在重建项目并运行。如果需要任何帮助,请随时发表评论。

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 2016-07-13
      • 2014-02-19
      相关资源
      最近更新 更多