android如何创建位图的arraylist
我有一个应用程序,我使用Gallery小部件以srcoll方式显示drawable中的一组图像 像下面这个,android如何创建位图的arraylist,android,Android,我有一个应用程序,我使用Gallery小部件以srcoll方式显示drawable中的一组图像 像下面这个, public class GalleryActivity extends Activity { private ImageView imageView; private int[] mImageIds = { R.drawable.restro1, R.drawable.restro2, R.draw
public class GalleryActivity extends Activity {
private ImageView imageView;
private int[] mImageIds = {
R.drawable.restro1,
R.drawable.restro2,
R.drawable.restro3,
R.drawable.restro4,
R.drawable.restro5,
R.drawable.restro6,
R.drawable.restro7,
R.drawable.restro8,
R.drawable.restro9,
R.drawable.restro10,
R.drawable.restro11,
R.drawable.restro12,
R.drawable.restro13,
R.drawable.restro14,
R.drawable.restro15,
R.drawable.restro16,
R.drawable.restro17,
R.drawable.restro18,
R.drawable.restro19,
R.drawable.restro20
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
imageView = (ImageView)findViewById(R.id.ImageView01);
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(GalleryActivity.this, "" + (arg2+1), Toast.LENGTH_SHORT).show();
imageView.setImageResource(mImageIds[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery2);//obtainStyledAttributes(android.R.style.Theme);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery2_android_galleryItemBackground,1);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(350, 250));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
公共类GalleryActivity扩展活动{
私人影像视图;
私有int[]图像ID={
R.drawable.restro1,
R.drawable.restro2,
R.drawable.restro3,
R.drawable.restro4,
R.drawable.restro5,
R.drawable.restro6,
R.drawable.restro7,
R.drawable.restro8,
R.drawable.restro9,
R.drawable.restro10,
R.drawable.restro11,
R.drawable.restro12,
R.drawable.restro13,
R.drawable.restro14,
R.drawable.restro15,
R.drawable.restro16,
R.drawable.restro17,
R.drawable.restro18,
R.drawable.restro19,
R.可抽出式厕所20
};
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
画廊g=(画廊)findViewById(R.id.Gallery);
g、 setAdapter(新的ImageAdapter(this));
imageView=(imageView)findViewById(R.id.ImageView01);
g、 setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
Toast.makeText(GalleryActivity.this,“+(arg2+1),Toast.LENGTH_SHORT.show();
setImageResource(mimageId[arg2]);
}
});
}
公共类ImageAdapter扩展了BaseAdapter{
int mGalleryItemBackground;
私有上下文;
公共图像适配器(上下文c){
mContext=c;
TypedArray a=ActainStyledAttributes(R.styleable.Gallery2);//ActainStyledAttributes(android.R.style.Theme);
mGalleryItemBackground=a.getResourceId(R.styleable.Gallery2\u android\u galleryItemBackground,1);
a、 回收();
}
public int getCount(){
返回mImageIds.length;
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
ImageView i=新的ImageView(mContext);
i、 setImageResource(mimageId[位置]);
i、 setLayoutParams(新画廊.LayoutParams(350250));
i、 setScaleType(ImageView.ScaleType.FIT_XY);
i、 挫折背景资源(mGalleryItemBackground);
返回i;
}
}
}
我也得到了我需要的o/p。但我的问题是,这里的图像是可绘制的,所以很容易。但我希望在运行时以json数组的形式从服务器(json)获取图像。
在那个时候我应该写什么来代替那个整数数组的mImageIds。
我将json数组转换为字符串数组(由许多图像URL组成)
在这之后,我将发送到ImageAdapter类,使其正常工作
谢谢您应该查看以下链接,这可能会对您有所帮助 快乐编码
Pragna使用
ArrayList
而不是数组动态加载数据,从路径设置图像使用imageView.setImageURI(Uri.parse(“myimagepath”)代码>
修改代码中的以下行,如下所示
而不是imageView.setImageResource(mimageId[arg2])代码>内部setOnItemClickListener
使用以下命令
imageView.setImageURI(Uri.parse(mImageIds.get(arg2)));
谢谢
Deepak你还必须写一些描述你的答案的东西。这种类型的答案表示spamI问了不同的问题。如果应用程序本身中存在图像(表示可绘制文件夹)然后很容易创建一个包含资源ID的整数数组。但是我想问的是图像是否是url的形式,然后我将它们转换为整数数组?嗯,我认为你应该用ID和名称映射它们。我认为你应该使用DBA。你能从url获取图像并获取图像吗?
mImageIds.add("imageurl1");
mImageIds.add("imageurl2");
.....
imageView.setImageURI(Uri.parse(mImageIds.get(arg2)));