关于Bitmap缩放图片的问题
我的这段代码在最后一行returnBitmap.createScaledBitmap(bitmap,w,h,true);运行错误提示该段空指针。我想做个ListView它...
我的这段代码在最后一行return Bitmap.createScaledBitmap(bitmap, w, h, true);运行错误提示该段空指针。我想做个ListView 它的Item里面有张图片 用自定义Adapter显示该图片 但是该图片分辨率很大容易内存溢出 于是我用Bitmap缩放图片 但是实验了半天就是没成功。。有高手帮我改改么?
public class MainActivity extends Activity {
private Bitmap bitmap = null;
private ListView listView = null;
private ArrayList<HashMap<String, Object>> mapList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv_View);
mapList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.wow);
mapList.add(map);
MyBaseAdapter MBA = new MyBaseAdapter(mapList, this);
listView.setAdapter(MBA);
}
class MyBaseAdapter extends BaseAdapter {
List<HashMap<String, Object>> data;
Context ctx;
MyBaseAdapter(List<HashMap<String, Object>> data, Context ctx) {
this.data = data;
this.ctx = ctx;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@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
if (convertView == null)
convertView = View.inflate(ctx, R.layout.lv_item, null);
ImageView img = (ImageView) convertView
.findViewById(R.id.img_view_lv_item);
HashMap<String, Object> map = data.get(position);
Object imgType = map.get("img");
Log.d("============>", imgType + "");
String s = String.valueOf(imgType);
Log.d("============>", s + "");
img.setImageBitmap(BM(s, 300, 300));
return convertView;
}
private Bitmap BM(String path, int w, int h) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Config.ARGB_8888;
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(path, options);
int height = options.outHeight;
int width = options.outWidth;
float scaleHeight = 0.f;
float scaleWidth = 0.f;
if (height > w || width > h) {
scaleHeight = ((float) height) / w;
scaleWidth = ((float) width) / h;
}
options.inJustDecodeBounds = false;
float scale = Math.max(scaleWidth, scaleHeight);
options.inSampleSize = (int) scale;
bitmap = BitmapFactory.decodeFile(path, options);
return Bitmap.createScaledBitmap(bitmap, w, h, true);
}
}
} 展开
public class MainActivity extends Activity {
private Bitmap bitmap = null;
private ListView listView = null;
private ArrayList<HashMap<String, Object>> mapList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv_View);
mapList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.wow);
mapList.add(map);
MyBaseAdapter MBA = new MyBaseAdapter(mapList, this);
listView.setAdapter(MBA);
}
class MyBaseAdapter extends BaseAdapter {
List<HashMap<String, Object>> data;
Context ctx;
MyBaseAdapter(List<HashMap<String, Object>> data, Context ctx) {
this.data = data;
this.ctx = ctx;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@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
if (convertView == null)
convertView = View.inflate(ctx, R.layout.lv_item, null);
ImageView img = (ImageView) convertView
.findViewById(R.id.img_view_lv_item);
HashMap<String, Object> map = data.get(position);
Object imgType = map.get("img");
Log.d("============>", imgType + "");
String s = String.valueOf(imgType);
Log.d("============>", s + "");
img.setImageBitmap(BM(s, 300, 300));
return convertView;
}
private Bitmap BM(String path, int w, int h) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Config.ARGB_8888;
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(path, options);
int height = options.outHeight;
int width = options.outWidth;
float scaleHeight = 0.f;
float scaleWidth = 0.f;
if (height > w || width > h) {
scaleHeight = ((float) height) / w;
scaleWidth = ((float) width) / h;
}
options.inJustDecodeBounds = false;
float scale = Math.max(scaleWidth, scaleHeight);
options.inSampleSize = (int) scale;
bitmap = BitmapFactory.decodeFile(path, options);
return Bitmap.createScaledBitmap(bitmap, w, h, true);
}
}
} 展开
展开全部
其实不用这么麻烦的,这样做还是会占用多余的内存空间,直接在你的R.layout.lv_item布局文件中,为其设置大小就可以了。还有,你代码中map加进去的不是图片路径(path),R.drawable.wow是R文件中,为wow图片自动生成的一个int类型值,你加入进去,再用String读出来,因为系统无法判断BitmapFactory.decodeFile(path, options);中path是不是路径,只是判断path是不是string类型个,所以编译不会报错,但实际解析不到的,这是不能显示图片的原因。另外可以通过computeSampleSize方法动态获取inSampleSize的值,代码如下:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile, opts);
opts.inSampleSize = computeSampleSize(opts, -1, 128*128); //-1k可以不用改,128*128换成你需要的分辨率就好了
opts.inJustDecodeBounds = false;
try {
Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
imageView.setImageBitmap(bmp);
} catch (OutOfMemoryError err) {
}
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile, opts);
opts.inSampleSize = computeSampleSize(opts, -1, 128*128); //-1k可以不用改,128*128换成你需要的分辨率就好了
opts.inJustDecodeBounds = false;
try {
Bitmap bmp = BitmapFactory.decodeFile(imageFile, opts);
imageView.setImageBitmap(bmp);
} catch (OutOfMemoryError err) {
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询