现在项目做的一个聊天软件,聊天的图片使用 glide 加载
图片的缩略图以 Base64 形式保存在数据库中
聊天界面会展示 gif 和 普通图片显示在不同的控件里,每次 getview 会释放 gif 资源
具体我的使用方式是:
//避免 gif 内存泄漏
if (null != gif_iv){
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>
在聊天界面滑动时 图片会跳动,从第一个直接跳到第三个 等等
求教 这是什么原因?
![]() | 1 WayneWangWM 2016-03-10 11:18:25 +08:00 你是用的什么 ListView ,我遇到过加载不对的情况是因为,使用 RecyclerView 时复用 ViewHolder 导致, image 加载到了错误的地方 |
![]() | 2 20015jjw 2016-03-11 00:17:29 +08:00 via Android 用 recyclerview |
试试看郭霖大神的博客,有一篇详细讲了 ListView 错位的问题 |