2012-03-26 95 views
0

我可以使用json通过url从服务器数据库中获取所有图像。我在列表视图如何可以给图像的高度和宽度如何设置图片高度和宽度从服务器?

显示所有图像这是main.java

JSONArray json = jArray.getJSONArray("names"); 
list=(ListView)findViewById(R.id.list);// calls main.xml 
    adapter=new ImageAdapter(this, json); 
    list.setAdapter(adapter); 
    Imageadapter.java 
    public class ImageAdapter extends BaseAdapter { 
Bitmap bmp; 
private static LayoutInflater inflater = null; 

private ImageView[] mImages; 
String[] itemimage; 
TextView[] tv; 
String itemname; 
HashMap<String, String> map = new HashMap<String, String>(); 

public ImageAdapter(Context context, JSONArray imageArrayJson) { 
    this.mImages = new ImageView[imageArrayJson.length()]; 
    String qrimage; 
    try { 

     for (int i = 0; i < imageArrayJson.length(); i++) { 
      JSONObject image = imageArrayJson.getJSONObject(i); 
      qrimage = image.getString("itemimage"); 
      itemname = image.getString("itemname"); 
      map.put("itemname", image.getString("itemname")); 
      System.out.println(itemname); 

      byte[] qrimageBytes = Base64.decode(qrimage.getBytes()); 

      bmp = BitmapFactory.decodeByteArray(qrimageBytes, 0, 
        qrimageBytes.length); 
      mImages[i] = new ImageView(context); 
      mImages[i].setImageBitmap(bmp); 

     } 

    } catch (Exception e) { 
     // TODO: handle exception 
    } 
} 

public int getCount() { 
    return mImages.length; 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

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


    return mImages[position]; 

} 
    } 
My xml view 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 


</LinearLayout> 

上述代码是显示从服务器数据库中的图像到Android到列表视图。 如何设置这些图像的高度和宽度?

+0

使用ImageView,只需设置宽度和高度属性?除非你在谈论缩放位图,在这种情况下,你可以使用BitmapFactor类来获得这个 – Mimminito 2012-03-26 09:00:29

+0

** IS是一个好主意,从服务器设置高度/宽度?​​* – 2012-03-26 10:10:44

回答

0

将字节转换为位图后。请问位图告诉他的身高,宽度

bmp = BitmapFactory.decodeByteArray(qrimageBytes, 0, 
         qrimageBytes.length); 

    // Ask Height and width from bitmap. 
    Log.e("Image Height" , " Height = "+bmp.getHeight() + " , Width = "+ bmp.getWidth()); 

     mImages[i] = new ImageView(context); 

     // Use LayououtParams to se your imageView hegiht , width 

     LayoutParams params = new LayoutParams(new LayoutParams(bmp.getWidth(), bmp.getHeight())); 
     mImages[i].setLayoutParams(params); 

     // And don't forget to set your imageView scale type. 
     img.setScaleType(ScaleType.FIT_CENTER); 
     // Then set your bitmap 
     mImages[i].setImageBitmap(bmp); 
+0

它获得我想要的图像的高度和宽度手动设置高度和宽度。根据图像和宽度它显示image.please告诉我如何做到这一点 – 2012-03-26 09:07:42

+0

请现在检查它 – Arslan 2012-03-26 09:19:38

0

您使用哪种布局来显示ListView行?您需要使用自定义ListView来显示图像和文本。给我一些你的代码的一部分。我会为你安排。

+0

雅我想显示自定义列表视图图像和文本请重播 – 2012-03-26 09:11:50

+0

您将不得不创建另一个XML布局来显示ListView行。您正在显示图像。所以你应该尝试延迟加载图像。 – 2012-03-26 09:50:45

+0

试试这些:(1)https://github.com/thest1/LazyList(2)http://negativeprobability.blogspot.in/2011/08/lazy-loading-of-images-in-listview.html – 2012-03-26 09:57:31

0

那么最简单的方法是让服务器返回其json中的widthheight作为属性。

使用位图有问题,因为如果它们太大,您将得到OutOfMemory错误。

+0

i得到它的答案 – 2012-03-26 09:37:11

相关问题