在 listview 中使用 Glide 加载图片,界面跳动?

2016-03-10 10:02:53 +08:00
 rjw1900

现在项目做的一个聊天软件,聊天的图片使用 glide 加载

图片的缩略图以 Base64 形式保存在数据库中

聊天界面会展示 gif 和 普通图片显示在不同的控件里,每次 getview 会释放 gif 资源

具体我的使用方式是:
//避免 gif 内存泄漏
if (null != gif_iv){
Drawable d = gif_iv.getDrawable();
if (d instanceof GifDrawable) {
((GifDrawable) d).recycle();
}
}

String miniMapStr_Base64 = "";
    if (message.getMessageType() == MessageType.IMAGE) {
        ImageFileInfo imageFileInfo = (ImageFileInfo) message.getFileInfo();
        miniMapStr_Base64 = imageFileInfo.getMiniMap();
    } else {
        VideoFileInfo imageFileInfo = (VideoFileInfo) message.getFileInfo();
        miniMapStr_Base64 = imageFileInfo.getMiniMap();
    }

    byte[] bytes = Base64.decode(miniMapStr_Base64, Base64.NO_WRAP);
    if (bytes == null) {
        return false;
    }

    if (message.getMessageType() == MessageType.IMAGE){
        String fileName = fileFolder + message.getMessageId();
        if (!TextUtils.isEmpty(message.getFileInfo().getFileExt())){
            fileName += "." + message.getFileInfo().getFileExt();
        }
        File file = new File(fileName);
        if (file.exists()){//文件已经下载、解密过
            if (message.getFileInfo().getFileExt().equalsIgnoreCase("gif")){
                    try {
                        GifDrawable gifDrawable = new GifDrawable(fileName);
                        gifDrawable.start();
                        gif_iv.setImageDrawable(gifDrawable);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }else {//普通图片依然展示缩略图
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeByteArray(bytes, 0, bytes.length,options);
                Glide.with(activity).load(bytes).override(options.outWidth,options.outHeight).into(iv);
            }
        }else {//图片不存在,就直接下载
            if (message.getFileInfo().getFileExt().equalsIgnoreCase("gif")){
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                gif_iv.setImageBitmap(bitmap);
            }else {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeByteArray(bytes, 0, bytes.length,options);
                Glide.with(activity).load(bytes).override(options.outWidth,options.outHeight).into(iv);
            }
                            /////下载、解密
        }
    }else {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(bytes, 0, bytes.length,options);
        Glide.with(activity).load(bytes).override(options.outWidth,options.outHeight).into(iv);
    }

    return true;



    <FrameLayout
        android:id="@+id/fl_pic_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="@dimen/padding_40"
        android:layout_marginRight="@dimen/padding_8"
        android:layout_toLeftOf="@id/iv_userhead"
        android:background="@drawable/chatto_bg"
        android:paddingLeft="@dimen/padding_4"
        android:paddingTop="@dimen/padding_4"
        android:paddingRight="@dimen/padding_8"
        android:paddingBottom="@dimen/padding_4"
        android:layout_marginLeft="@dimen/padding_8">

        <pl.droidsonroids.gif.GifImageView          显示普通图片
            android:id="@+id/iv_sendPicture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="@dimen/chat_img_max_width"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_gravity="center"/>

        <pl.droidsonroids.gif.GifImageView            显示 gif
            android:id="@+id/iv_sendPicture_gif"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="@dimen/chat_gif_max_width"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:layout_gravity="center"
            android:visibility="gone"/>

        <common.view.CircleProgressBar
            android:id="@+id/progressBar"
            android:layout_width="@dimen/padding_32"
            android:layout_height="@dimen/padding_32"
            android:layout_gravity="center"
            android:visibility="invisible" />
    </FrameLayout>

在聊天界面滑动时 图片会跳动,从第一个直接跳到第三个 等等

求教 这是什么原因?

9468 次点击
所在节点    Android
3 条回复
WayneWangWM
2016-03-10 11:18:25 +08:00
你是用的什么 ListView ,我遇到过加载不对的情况是因为,使用 RecyclerView 时复用 ViewHolder 导致, image 加载到了错误的地方
20015jjw
2016-03-11 00:17:29 +08:00
用 recyclerview
TomKein
2016-03-11 12:26:42 +08:00
试试看郭霖大神的博客,有一篇详细讲了 ListView 错位的问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/262407

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX