基于从previous活动的GridView选择显示图片

基于从previous活动的GridView选择显示图片

本文介绍了基于从previous活动的GridView选择显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个工作的GridView显示一些缩略图。目前,当您选择一个图像,将图像的较大分辨率保存为墙纸。

I currently have a working gridview displaying some thumbnails. Currently, when you select an image it will save a larger resolution of the image as a wallpaper.

我想要做的就是打开一个新的意图,然后显示基于GridView控件位置的全分辨率图像点击。我不知道我是如何找出什么位置是通过新的活动/意图点击。

What I want to do is open a new intent and then display the full resolution image based on the GridView position clicked. I'm not sure how I find out what position was clicked from the new activity/intent.

下面有什么,我有我的主要活动

Heres what I have in my main activity

public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(getApplicationContext()));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(test.this, "" + position + "" + id, Toast.LENGTH_SHORT).show();

            //Make a Bitmap from the Resource
            ImageAdapter i = (ImageAdapter)parent.getAdapter();
            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));

            //Get the WallpaperManager
            WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

            try {
                //Set the clicked bitmap
                myWallpaperManager.setBitmap(mBitmap);
                Toast.makeText(test.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(test.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
            }

        }
    });

}

}

ImageAdapter

ImageAdapter

public class ImageAdapter extends BaseAdapter {

private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return mFullSizeIds[position];
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_1, R.drawable.sample_2,
        R.drawable.sample_3, R.drawable.sample_4,
        R.drawable.sample_5, R.drawable.sample_6,
        R.drawable.sample_7
};


private Integer[] mFullSizeIds = {
        R.drawable.wallpaper1,
        R.drawable.wallpaper2,
        R.drawable.wallpaper3,
        R.drawable.wallpaper4,
        R.drawable.wallpaper5,
        R.drawable.wallpaper6,
        R.drawable.wallpaper7
        };

}

和富景花园空白模板

public class FullView extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullview);

ImageView imageview;
    imageview.setImageResource(mFullSizeIds[**]) <--- How to set ** to what GridView position was selected?


}

}

我还没有真正由于被不知道如何通过从GridView控件被点击了这个新的活动,该位置写在活动富景花园任何内容。

I haven't really written anything in the FullView activity yet due to being unaware of how to pass the position that was clicked from the GridView in this new activity.

(我没有写意图code在我的主要活动要么所以你可以看到它目前的工作方式)

(I haven't written the intent code in my main activity either so you can see how it currently works)

我如何通过所选择进入新的活动GridView的位置?

How do I pass the gridview position that was selected into the new activity?

推荐答案

在你的事件处理程序(该OnItemClickListener),你可以得到的图像作为mThumbs [位置]资源ID。添加为一个额外的用于启动富景花园的活动的意图。

In your event handler (the OnItemClickListener), you can get the resource ID of the image as mThumbs[position]. Add this as an extra to the Intent you use to start your FullView Activity.

这篇关于基于从previous活动的GridView选择显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 02:28