0
我使用內容解析器在網格視圖中顯示圖片縮略圖。如何提高圖片縮略圖的加載速度
問題:它可以在我的設備上以50張圖像正常運行,但在我的其他設備上掛起。我如何減少加載時間並提高應用速度。
我的動態,其中我顯示利用基本適配器是如下大拇指
public class CustomGallaryViewActivity extends Activity {
Context mContext;
ArrayList<ImageItem> imageList;
private int listCount;
private ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_gallary_view);
mContext=this;
imageList=CreateImageThumbnails.createThumbnails(CustomGallaryViewActivity.this);
listCount=imageList.size();
GridView imageGrid=(GridView)findViewById(R.id.grid_view);
imageAdapter=new ImageAdapter();
imageGrid.setAdapter(imageAdapter);
}
公共類ImageAdapter延伸BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
LayoutInflater infalter=(LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
convertView=infalter.inflate(R.layout.galleryitem,null);
holder.imageview=(ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
}
else{
holder=(ViewHolder) convertView.getTag();
}
holder.imageview.setId(position);
holder.imageview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Log.d("IDIDID",""+id);
ImageItem item = imageList.get(id);
Intent intent=new Intent(mContext,FullScreenImageActivtiy.class);
//intent.putExtra("ID",item.id);
intent.putExtra("ImageItemVal",new int[]{item.id,item.imgNumber,listCount,id});
startActivity(intent);
}
});
ImageItem item=new ImageItem();
item=imageList.get(position);
holder.imageview.setImageBitmap(item.img);
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.custom_gallary_view, menu);
return true;
}
class ViewHolder {
ImageView imageview;
}
}
我使用下面的類
public class CreateImageThumbnails{
public static ArrayList<ImageItem> createThumbnails(Activity refActivtiy){
ArrayList<ImageItem> thumbNails=new ArrayList<ImageItem>();
final String[] columns = { MediaStore.Images.Thumbnails._ID };
final String orderBy = MediaStore.Images.Media._ID;
//Cursor imageCursor=refActivtiy.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,columns,null,null,orderBy);
Cursor imageCursor = refActivtiy.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,null, null,orderBy);
if(imageCursor!=null){
int count=imageCursor.getCount();
Log.d("Count Vlaue",""+count);
int image_column_index=imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
for(int i=0;i < count;i++){
imageCursor.moveToPosition(i);
int id=imageCursor.getInt(image_column_index);
Log.d("Image ID : ",""+id);
ImageItem imageItem=new ImageItem();
imageItem.id=id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
refActivtiy.getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
thumbNails.add(imageItem);
}
}
return thumbNails;
}
}