1

我从网上下载了一堆图片,代码如下:

for(int i = 1; i < 12; i++) {
    try {
        URL imageURL = new URL("http://domain/drawimage.php?type=" + i + "&d1=" + this.d1 + "&d2=" + this.d2);
        Bitmap image = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());

        bitmaps.add(image);
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
}

稍后当我尝试将它们插入到 LinearLayout ( ll) 中时,即使bitmaps.size() = 11.

for(int i = 0; i < bitmaps.size(); i++) {
    ImageView iv = new ImageView(c); // c = getApplicationContext()
    iv.setImageBitmap(bitmaps.get(i));
    iv.setVisibility(ImageView.VISIBLE);

    ll.addView(iv);
}

如果我尝试使用TextView,它可以工作。

for(int i = 0; i < bitmaps.size(); i++) {
    TextView tv = new TextView(c); // c = getApplicationContext()
    tv.setText("image ");
    tv.setGravity(Gravity.CENTER_HORIZONTAL);

    ll.addView(tv);
}

是加载问题还是插入失败?怎么修?

4

3 回答 3

0

I've wrote the code bellow to add an ImageView programatically to a RelativeLayout. It can be easly changed to work with LinearLayout:

private ImageView addImageView(RelativeLayout mainLayout, int x, int y, int width, int height, OnClickListener onClickListener){
    ImageView imageView = new ImageView(this);
    imageView.setAdjustViewBounds(false);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.height = height;
    params.width = width;
    imageView.setLayoutParams(params);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setImageDrawable(getResources().getDrawable(R.drawable.marker_red));
    //imageView.setBackgroundColor(Color.BLUE);
    params.leftMargin = x - width/2;
    params.topMargin = y - height/2;
    imageView.setOnClickListener(onClickListener);
    mainLayout.addView(imageView);
    return imageView;
}

Regards.

于 2012-10-28T17:53:43.767 回答
0

据我了解,您正在尝试在垂直线性布局中添加图像列表?

我建议您使用具有自定义布局的 ListView 和 ArrayAdapter。

这是我为帮助您而制作的示例代码:

public class MainActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitymain);


        StrictMode.ThreadPolicy policy = new StrictMode.
             ThreadPolicy.Builder().permitAll().build();
             StrictMode.setThreadPolicy(policy);


        List<Bitmap> bitmaps= new ArrayList<Bitmap>();

        for(int i = 1; i < 12; i++) {
            try {
                URL imageURL = new URL("http://developer.android.com/assets/images/dac_logo.png");
                Bitmap image = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());

                bitmaps.add(image);
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }

        setListAdapter(new BitmapAdapter(this,R.layout.listview_item,bitmaps));

    }

自定义位图适配器:

public class BitmapAdapter extends ArrayAdapter<Bitmap>{

     Context context; 
      int layoutResourceId;  
      List<Bitmap> bitmaps;

    public BitmapAdapter(Context context, int textViewResourceId,
            List<Bitmap> objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub

        this.layoutResourceId = textViewResourceId;
        this.bitmaps = objects;
        this.context = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(this.layoutResourceId, parent, false);
        ImageView iv = (ImageView) rowView.findViewById(R.id.imageView1);

        iv.setImageBitmap(this.bitmaps.get(position));

        return rowView;
    }

}

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
     android:id="@android:id/list">

</ListView>

listview_item.xml:

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

于 2012-10-28T17:23:03.820 回答
0

我在 PHP 端打印图片时遇到问题。我也应该检查一下。现在它正在工作。

于 2012-11-22T14:13:23.177 回答