2012-11-12 66 views
0

我加載的圖像和標題從webservices列表視圖文本是f9但圖像太大瞭如何調整它的大小,我想設置文本在右側和圖像在每個左側行列表視圖。我使用此方法位圖resized = Bitmap.createScaledBitmap(位圖,70,70,真);但沒有出現在模擬器這是我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 

} 
+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); 
+0

thnkx爲建議,但沒有expite圖像是可見的 –

+1

可能丟失此方法 imageView.postinvalidate(); –

0

問題不在我這個bitmapfactory類中。我已經解決了我的問題。 我只是改變我的懶適配器類中的代碼。

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"); 
} 

我調整了我的列表中的圖像之前view.while將在adpter類列表視圖的調整。