开发者

android how to create an arraylist of bitmap

开发者 https://www.devze.com 2023-03-13 14:30 出处:网络
I have an application where i used Gallery widget to show a bunch of images present within drawable in a srcoll manner.

I have an application where i used Gallery widget to show a bunch of images present within drawable in a srcoll manner.

likes this below,

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;
        }
    }
}

I got also o/p as i need. But my question is here the images are present in drawable so its easy. but i want to fetch images at runtime from server(json) in form of JSon array. At that time what i should write instead of that Integer array mImageIds. I converted that json array to string array(consists of a no of image urls) after that what i send to that ImageAdapter class so it work properly.

Thank you


Use ArrayList instead of array to load data dynamically, to set image from a path use imageView.setImageURI(Uri.parse("myimagepath"));

Modify the following lines in your code as given below

ArrayList<String> mImageIds = new ArrayList<String>();

Add image urls after parsing from json at run time

mImageIds.add("imageurl1");
mImageIds.add("imageurl2");
.....

Instead of imageView.setImageResource(mImageIds[arg2]); inside setOnItemClickListener use the following

imageView.setImageURI(Uri.parse(mImageIds.get(arg2)));

Thanks Deepak


you should view the following links this may helps you 1 2 3 Happy Coding!!
Pragna

0

精彩评论

暂无评论...
验证码 换一张
取 消