如何调整列表视图的图像大小

时间:2012-11-12 18:00:12

标签: android

我从webservices文本加载列表视图的图像和标题是f9但是图像太大了。我要调整它的大小,我想在列表视图的每一行中设置右侧的文本和左侧的图像。我是使用此方法Bitmap resized = Bitmap.createScaledBitmap(bitmap,70,70,true);但是没有出现在模拟器上这是我正在加载图像的costant课程

public static Bitmap loadPhotoBitmap(URL url) {
Bitmap bitmap = null ;
InputStream in = null;
BufferedOutputStream out = null;
BufferedOutputStream bfs = null;

try {
FileOutputStream fos = new FileOutputStream("/sdcard/photo-tmp.jpg");
bfs = new BufferedOutputStream(fos);

in = new BufferedInputStream(url.openStream(),8192);

final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, 8192);
copy(in, out);                    
out.flush();
final byte[] data = dataStream.toByteArray();

bfs.write(data, 0, data.length);
bfs.flush();

BitmapFactory.Options opt = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);

//System.out.println(resized);
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 70,70, true);
    return  resized;
} catch (IOException e) {
// android.util.Log.e("message", "Could not load photo: " + this, e);
System.out.println("Exception while loading image");
} finally {
closeStream(in);
closeStream(out);
closeStream(bfs);
}
System.out.println("returning");
return  bitmap;
}

LazyAdapter.class

public View getView(int position, View convertView, ViewGroup parent) {

System.out.println("Exception before..");
String strUrl = Constants.vctrImagePath.elementAt(counter).toString();// getting imgurl  
System.out.println("Urls...." + strUrl);
URL url =null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
e.printStackTrace();
}

Bitmap bitmap = Constants.loadPhotoBitmap(url);
//Bitmap resized = Bitmap.createScaledBitmap(bitmap, 70, 70, true);


RelativeLayout layout = new RelativeLayout(mContext);
RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);        

TextView text1 = new TextView(mContext);
text1.setText(Constants.vctrCategory.elementAt(counter).toString());
LayoutParams params1 = new       LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
text1.setLayoutParams(params1);
text1.setTextSize(20);
text1.setGravity(Gravity.RIGHT);
layout.addView(text1);
ImageView img = new ImageView(mContext);
img.setImageBitmap(bitmap);

layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
layout.addView(img);
counter++;
return layout;
}

private void setContentView(ImageView image) {
// TODO Auto-generated method stub

}

2 个答案:

答案 0 :(得分:2)

而不是

BitmapFactory.Options opt = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);

将其替换为

BitmapFactory.Options opts = new BitmapFactory.Options();               
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);

答案 1 :(得分:0)

  

问题不在我的这个bitmapfactory class.i hav解决了我的问题。   我只是在懒惰的适配器类中更改代码。

Bitmap bitmap = Constants.DownloadImage(strUrl);
    Bitmap resized=null;
    if(bitmap!=null){
    resized = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
}else{
    System.out.println(strUrl+ "no image here");
}
  

在我在列表视图中调整图像大小之前。它将在列表视图的adpter类中调整大小。