First of all, I already did a few things to display the image in gridview, making it smooth when scrolling
1. Download an image from the Internet in the background
AsyncTask acyncTask ;
HandlerThread handlerThread ;
URL imageUrl = new URL(link);<br>
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(is);
2. make a bitmap to select the size to save memory
final BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mFile.getPath(),
option);
option.inSampleSize = calculateInSampleSize(option , mSize , mSize);
option.inJustDecodeBounds = false;
option.inPurgeable = true ;
option.inDither = false ;
option.inScaled = false ;
option.inPreferredConfig = Bitmap.Config.ARGB_8888;
3. make a cache to save the decoded bitmap
LruCache lruCache = new LruCache<String , Bitmap>();
in baseadapter getView();
I will do lruCache.get(key)for the decoded bitmap image
4. lazy load when decoding a bitmap through a processor
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
imageView.setimageBitmap(bitmap);
}
});
, ,
google , - , , - , getView(), 2 ~ 6ms, async, , , . , ?
: , , , , , , ,
:
if (convertView == null) {
convertView = layoutInflater.inflate(
R.layout.row_display_image_grid, null);
viewHolder = new DisplayImageGridViewHolder();
viewHolder.background = (RelativeLayout) convertView
.findViewById(R.id.background);
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.text = (TextView) convertView.findViewById(R.id.text);
viewHolder.position = position;
viewHolder.text.setEllipsize(TruncateAt.END);
viewHolder.text.setTypeface(typeFace);
viewHolder.image.setOnClickListener(this);
viewHolder.image.setOnLongClickListener(this);
convertView.setTag(viewHolder);
} else {
viewHolder = (DisplayImageGridViewHolder) convertView.getTag();
}
viewHolder.position = position;
imageLoader.loadImage(imageLinkList.get(position), viewHolder.image);
return convertView;