我有一个带有ImageViews的gridView片段。 在一个屏幕上放置了大约12个图像。我的项目中总共有大约1000张图片。图像存储在/ assets文件夹中。
我尝试按照谷歌文档使滚动顺利。它对我有所帮助,但根本没有。
我有一个问题,当我滚动时,我能够看到最后一行是如何重绘的。 此外,如果我快速滚动,那么我将看到屏幕上的所有图像将如何重绘(旧图像按新方式更改)。而且当我快速滚动时,屏幕上的所有当前图像都会被冻结。只有在滚动停止时才会重新绘制图像。
有没有办法改善它?
我的自定义适配器:
public class GridViewAdapter extends BaseAdapter{
private Context context;
private List<Meme> memes;
private String path;
private LruCache<String, Bitmap> memoryCache;
public GridViewAdapter(Context context, List<Meme> memes, String path){
this.context = context;
this.memes = memes;
this.path = path;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory());
final int cacheSize = maxMemory / 4;
this.memoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount();
}
};
}
@Override
public int getCount() {
return memes.size();
}
@Override
public Object getItem(int position) {
return memes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
static class ViewHolder{
ImageView memeImage;
int position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.memeImage = (ImageView) convertView.findViewById(R.id.meme_image);
viewHolder.position = position;
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
String fullPath = path + "/" + memes.get(position).getTitle();
v = params[0];
Bitmap bitmap = getBitmapFromMemoryCache(fullPath);
if(bitmap == null){
try {
InputStream is = context.getAssets().open(fullPath);
bitmap = BitmapFactory.decodeStream(is);
addBitmapToMemoryCache(fullPath, bitmap);
}
catch (IOException e){
e.printStackTrace();
}
}
return bitmap;
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemoryCache(key) == null) {
memoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemoryCache(String key) {
return memoryCache.get(key);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (v.position == position) {
// If this item hasn't been recycled already, hide the
// progress and set and show the image
v.memeImage.setImageBitmap(result);
}
}
}.execute(viewHolder);
viewHolder.position = position;
return convertView;
}
}
提前多多谢谢你。