Java 将图像视图添加到线性布局中
我从互联网上下载了一组图像,代码如下:Java 将图像视图添加到线性布局中,java,android,textview,imageview,Java,Android,Textview,Imageview,我从互联网上下载了一组图像,代码如下: 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.openConne
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();
}
}
如果我尝试使用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);
}
for(int i=0;i
加载有问题还是插入失败?如何修复?据我所知,您试图在垂直线性布局中添加图像列表 我建议您使用具有自定义布局的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类MainActivity扩展了ListActivity{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
StrictMode.ThreadPolicy策略=新StrictMode。
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
列表位图=新的ArrayList();
对于(int i=1;i<12;i++){
试一试{
URL imageURL=新URL(“http://developer.android.com/assets/images/dac_logo.png");
位图image=BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
位图。添加(图像);
}捕获(例外e){
系统输出打印ln(e);
e、 printStackTrace();
}
}
setListAdapter(新位图适配器(此,R.layout.listview_项,位图));
}
自定义位图适配器:
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;
}
}
公共类位图适配器扩展了ArrayAdapter{
语境;
国际布局资源;
列出位图;
公共位图适配器(上下文,int textViewResourceId,
列出对象){
超级(上下文、textViewResourceId、对象);
//TODO自动生成的构造函数存根
this.layoutResourceId=textViewResourceId;
this.bitmap=对象;
this.context=上下文;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图行视图=充气机。充气(this.layoutResourceId,父级,false);
ImageView iv=(ImageView)rowView.findViewById(R.id.imageView1);
iv.setImageBitmap(this.bitmaps.get(position));
返回行视图;
}
}
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" />
我编写了下面的代码,以编程方式将
图像视图添加到相对视图中。可以轻松地将其更改为使用线性布局
:
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;
}
尊敬。我在PHP端打印图片时遇到问题。我也应该检查一下。现在它工作了。添加一个null
检查位图。获取(I)
并检查。我猜您的位图是空的。请尝试为ImageView指定一个适当的大小,它可能会崩溃。@mmohsineem谢谢!我检查了,然后我想是什么问题:应该生成图像的PHP页面出现了错误。谢谢帮助!
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;
}