根据先前活动中的 Gridview 选择显示图像

发布于 2024-11-24 09:44:57 字数 3540 浏览 0 评论 0原文

我目前有一个正在工作的网格视图,显示一些缩略图。目前,当您选择图像时,它会将较大分辨率的图像保存为壁纸。

我想要做的是打开一个新意图,然后根据单击的 GridView 位置显示全分辨率图像。我不确定如何找出从新活动/意图中单击的位置。

这是我在主要活动中的内容

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

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

}

和空白 FullView 模板

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?


}

}

我还没有在 FullView 活动中真正编写任何内容,因为不知道如何传递从这个新活动中的 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.

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

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

}

And blank FullView template

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?


}

}

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2024-12-01 09:44:57

在事件处理程序(OnItemClickListener)中,您可以获取图像的资源 ID 作为 mThumbs[position]。将其作为额外内容添加到用于启动 FullView Activity 的 Intent 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文