Android模拟器显示空白屏幕,图像仍然打开(假设有150,150张照片)。换句话说,我的150,150大拇指在空白屏幕后面
我的图片大于150 * 150,我按照下面的链接高效加载大位图,但它仍然无效。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
这是我在imageAdapter中的代码
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(reqHeight + reqWidth == 0){
return inSampleSize;
}
else if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
我还添加了完整的imageAdapter类以获取更多信息。
package com.example.first;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.util.Log;
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public Integer[] mThumbIds = {
R.drawable.h18, R.drawable.h17, R.drawable.h16, R.drawable.h15,
};
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(reqHeight + reqWidth == 0){
return inSampleSize;
}
else if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview;
if(convertView == null){
imageview = new ImageView(mContext);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
imageview.setLayoutParams(new GridView.LayoutParams(150,150));
imageview.setPadding(8, 8, 8, 8);
}else{
imageview = (ImageView) convertView;
}
imageview.setImageBitmap(decodeSampledBitmapFromResource( null, mThumbIds[position], 150, 150));
return imageview;
}
private Resources getResources() {
// TODO Auto-generated method stub
return null;
}
}
对此的任何帮助表示赞赏。
答案 0 :(得分:0)
您使用null
而不是Resources
对象调用您的位图加载器。
替换
decodeSampledBitmapFromResource( null, mThumbIds[position], 150, 150)
用
decodeSampledBitmapFromResource( mContext.getResources(), mThumbIds[position], 150, 150)
应该做的伎俩。