我是安卓新手。我在一个目录中有上千张图片。我在网格视图中显示图像拇指。使用以下代码它的作品。但问题是加载视图需要 45 秒。我需要它:使用加载器显示网格并一张一张加载图像。所以用户不能等待最后一张图片加载。
代码是:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListRowHolder listRowHolder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item,
parent, false);
listRowHolder = new ListRowHolder();
listRowHolder.imgSponsor = (ImageView) convertView
.findViewById(R.id.imggrid_item_image);
convertView.setTag(listRowHolder);
} else {
listRowHolder = (ListRowHolder) convertView.getTag();
}
try {
listRowHolder.imgSponsor
.setImageBitmap(decodeSampledBitmapFromResource(
ImageName.get(position)));
} catch (Exception e) {
Toast.makeText(ctx, e + "", Toast.LENGTH_SHORT).show();
}
return convertView;
}
public static Bitmap decodeSampledBitmapFromResource(String fileName) {
Bitmap picture = BitmapFactory.decodeFile(fileName);
int width = picture.getWidth();
int height = picture.getWidth();
float aspectRatio = (float) width / (float) height;
int newWidth = 98;
int newHeight = (int) (98 / aspectRatio);
return picture = Bitmap.createScaledBitmap(picture, newWidth,
newHeight, true);
}