我使用以下代码从互联网上下载了一堆图片:
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);
}
加载问题或插入是否失败?怎么解决?
答案 0 :(得分:0)
根据我的理解,您试图在垂直linearlayout中添加图像列表?
我建议您使用带有自定义布局的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));
}
自定义BitmapAdapter:
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;
}
}
activitymain.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" />
答案 1 :(得分:0)
我编写了下面的代码,以编程方式将ImageView
添加到RelativeLayout
。可以轻松更改它以使用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;
}
问候。
答案 2 :(得分:0)
我在PHP端打印图片时遇到问题。我也应该检查一下。现在它正在运作。