How to set an image as background image on a click?

后端 未结 2 1566
囚心锁ツ
囚心锁ツ 2021-01-26 10:59

I have got an activity that shows a grid view with different images. When clicking on one of those images I want the clicked image to be the background image of another activity

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 11:57

    So, the poster didn't (originally) specify exactly whether the other activity was to be opened immediately on click, or if the clicked item's image just needed to be recorded for use later. The first problem seems more common to me, as in a GridView that shows a set of image thumbnails. The user clicks on that, and a new Activity showing just that item's information comes up. That thumbnail image maybe goes full screen. Anyway, maybe that's not what the poster had in mind, but that's my assumption (and probably somebody will eventually find this answer with such a use case in mind).

    It also isn't specified how the images are stored, so I'll make assumptions that

    1. The ImageAdapter's images are bundled resources of this app
    2. We can make some changes to the ImageAdapter, to store some data to solve this problem

    So, with that, I take a standard ImageAdapter, and add one line of code to record the integer resource ID for each ImageView, in the ImageView's tag property. There's many ways to do this, but this is the way I chose.

    public class ImageAdapter extends BaseAdapter {
       /* see code removed at 
           http://developer.android.com/resources/tutorials/views/hello-gridview.html 
        */
    
       // 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]);
          // here we record the resource id of the image, for easy access later
          imageView.setTag(mThumbIds[position]);
          return imageView;
       }
    
       // references to our images
       private Integer[] mThumbIds = {
             R.drawable.pic1, 
             R.drawable.pic2, 
             R.drawable.pic3, 
             R.drawable.pic4, 
             R.drawable.pic5 
       };
    }
    

    Then, when the Hello activity has a grid item clicked, I retrieve the image's resource id and pass it as an Intent extra (HelloGridViewActivity.java):

       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
    
          GridView gridView = (GridView) findViewById(R.id.gridview);
    
          // Instance of ImageAdapter Class
          gridView.setAdapter(new ImageAdapter(this));
    
          final Context activity = this;
    
          gridView.setOnItemClickListener(new OnItemClickListener() {
    
             public void onItemClick(AdapterView parent, View v, int position, long id) {
                Object tag = v.getTag();
                if (tag instanceof Integer) {
                   // we stored a reference to the thumbnail image in the ImageView's tag property
                   Intent i = new Intent(activity, AnotherActivity.class);
                   Integer resourceId = (Integer)tag;
                   i.putExtra("backgroundImage", resourceId);
                   startActivity(i);
                }
             }
          });
       }
    

    And finally, when the new activity (AnotherActivity) is opened, we retrieve that intent extra, and decode it as an integer resource id, and set it as the background image with a fullscreen ImageView (AnotherActivity.java):

       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          this.setContentView(R.layout.another);
    
          Intent i = getIntent();
          //String bgImage = i.getExtras().getString("backgroundImage");
          int resId = i.getExtras().getInt("backgroundImage");
    
          try {
             ImageView background = (ImageView) findViewById(R.id.bgImage);
             // Another alternative, if the intent extra stored the resource 'name',
             //   and not the integer resource id
             //Class c = R.drawable.class;
             //background.setImageResource(c.getDeclaredField(bgImage).getInt(c));
             background.setImageResource(resId);
          } catch (Exception e) {
             e.printStackTrace();
          }
       }
    

    I also show above some commented out code, if for some reason you need to pass a string name of an image resource, instead of an integer resource code. The commented out code looks up the resource id for the given image name. According to the resource names used in my ImageAdapter, a sample string name to pass might be "pic1". If you do this, of course, the calling Activity (HelloGridViewActivity) needs to encode the Intent extra as a String, not an integer.

提交回复
热议问题