开发者

Extending HelloGallery tutorial. Click on Image and pass the image to another activity/page

I am very new to Android (and Java) originally a C# developer.. and am struggling with the concepts and terminology. I have been through almost all the tutorials and have decided to start on my first test app.

The first part of my app is going to involve using the gallery widget (based on the hello gallery tutorial) to display a load of images. (these images will eventually come from the phones camera, but I will cross that bridge later!) My images are currently stored in the drawable folder in my project.

As a first step I want to just have the user click on an image and it pops up full screen, just so I know how to get reference to it, but I am struggling. Evetually I want the 开发者_如何学Gouser to click an image, then a window/activity(?) will open asking them for a bit of text. This will then be store in sqllite against the image.

So far I have two classes: Class1 & OpenImage. Class1 one contains the ImageAdapter class similar to the HelloGallery tutorial.. I am adding an onclick listener in the "getView" as follows:

public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        i.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {                    
                    Intent i = new Intent(Class1.this, OpenImage.class);
                    startActivity(i);
                    finish();                    
            }
          });     

        return i;
    }

The part I am struggling with is opening my OpenImage and getting it to show the image.

Firstly I'm not actually sure what I need to pass and how I do it, then how I get it on the OpenImage page/activity.

I am not even sure of the terminology of what I'm trying to do so I am also finding it hard to research. Am I opening a new activity when I am opening the fullscreen image? or should it be some sort of pop up?

This seems like the simplest thing to do but I'm falling at the first hurdle. Can someone give me some guidance?

Thanks Bex


You are almost done. Just pass the ID of the image to the intent:

                Intent i = new Intent(Class1.this, OpenImage.class);
                i.putExtra("the_image_id", mImageIds[position]);
                startActivity(i);
                finish();

Then, on your OpenImage class, you get the ID and set it to a ImageView in fullscreen:

public void onCreate(Bundle b){
    super.onCreate(b);
    // blah blah blah
    int theID = getIntent().getExtras().getInt("the_image_id");
    imageView.setImageResource(theID);
}

I'm assuming that you have already build a layout for the OpenImage activity, and that you have already initialized the imageView object and so on. The important thing here is that you learn how to pass data between activities.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜