V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
rjw1900
V2EX  ›  Android

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

  •  
  •   rjw1900 · 2016-03-10 10:02:53 +08:00 · 9198 次点击
    这是一个创建于 2977 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在项目做的一个聊天软件,聊天的图片使用 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>
    

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

    求教 这是什么原因?

    3 条回复    2016-03-11 12:26:42 +08:00
    WayneWangWM
        1
    WayneWangWM  
       2016-03-10 11:18:25 +08:00
    你是用的什么 ListView ,我遇到过加载不对的情况是因为,使用 RecyclerView 时复用 ViewHolder 导致, image 加载到了错误的地方
    20015jjw
        2
    20015jjw  
       2016-03-11 00:17:29 +08:00 via Android
    用 recyclerview
    TomKein
        3
    TomKein  
       2016-03-11 12:26:42 +08:00
    试试看郭霖大神的博客,有一篇详细讲了 ListView 错位的问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2366 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 07:18 · PVG 15:18 · LAX 00:18 · JFK 03:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.