如何在点击图像设置为背景图片? [英] How to set an image as background image on a click?

查看:121
本文介绍了如何在点击图像设置为背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,显示了不同的图像网格视图。当在这些图像中的一个点击欲被点击的图像是另一活动的背景图像。

我怎么能这样做?

这是我的活动,显示了网格视图:

 公共类HelloGridViewActivity延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);

    // ImageAdapter类的实例
    gridView.setAdapter(新ImageAdapter(本));

    / **
     *在单人的GridView项Click事件
     * * /
    gridView.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){

//我应该怎么把这里????
    }
}
}
 

解决方案

所以,海报没有(原)明确指定的其他活动是否应立即打开上点击,或者单击项目的形象只需要被记录下来以备后用。第一个问题似乎更常见的我,在的GridView 显示一组图像缩略图。用户点击这一点,只显示该项目的信息,新的活动出现。这缩略图可能变为全屏显示。不管怎样,也许这不是海报脑子里想的是什么,但是这是我的假设(可能有人会最终找到这个答案与这样一个用例记)。

这还不算指定如何存储图像,所以我会做出假设

  1. ImageAdapter 的图像都捆绑这个应用程序的资源
  2. 我们可以做一些改变 ImageAdapter ,存储一些数据来解决这个问题

于是,有了这一点,我拿一个标准的 ImageAdapter ,并添加一行code,以记录每个ImageView的整数资源ID,在的ImageView 标记属性。有很多方法可以做到这一点,但是这是我自己选择的方式。

 公共类ImageAdapter扩展了BaseAdapter {
   / *见code在移除
       http://developer.android.com/resources/tutorials/views/hello-gridview.html
    * /

   //创建由适配器引用的每个项目的新的ImageView
   公共查看getView(INT位置,查看convertView,ViewGroup中父){
      ImageView的ImageView的;
      如果(convertView == NULL){//如果它不回收,初始化一些属性
         ImageView的=新ImageView的(mContext);
         imageView.setLayoutParams(新GridView.LayoutParams(85,85));
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
         imageView.setPadding(8,8,8,8);
      } 其他 {
         ImageView的=(ImageView的)convertView;
      }

      imageView.setImageResource(mThumbIds [位置]);
      //这里我们后来拍摄的图像资源编号,以便于访问
      imageView.setTag(mThumbIds [位置]);
      返回ImageView的;
   }

   //引用到我们的形象
   私人整数[] mThumbIds = {
         R.drawable.pic1,
         R.drawable.pic2,
         R.drawable.pic3,
         R.drawable.pic4,
         R.drawable.pic5
   };
}
 

然后,当你好活动有一个网格项点击,我获取图像的资源ID,并把它作为一个意图额外的(HelloGridViewActivity.java):

 公共无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.main);

      GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);

      // ImageAdapter类的实例
      gridView.setAdapter(新ImageAdapter(本));

      最终的关联活动=这一点;

      gridView.setOnItemClickListener(新OnItemClickListener(){

         公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
            对象标记= v.getTag();
            如果(标记的instanceof整数){
               //我们存储的参考缩略图图像中的ImageView的的标签属性
               意图I =新的意图(活动,AnotherActivity.class);
               整数RESOURCEID =(整数)标签;
               i.putExtra(和backgroundImage,RESOURCEID);
               startActivity(ⅰ);
            }
         }
      });
   }
 

最后,当新的活动( AnotherActivity )被打开,我们检索意图额外的,和去$ C C是$为整数资源ID,并设置它具有全屏背景图像的ImageView (AnotherActivity.java):

 保护无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
      this.setContentView(R.layout.another);

      意图I = getIntent();
      //字符串bgImage = i.getExtras()的getString(和backgroundImage)。
      INT渣油= i.getExtras()调用getInt(和backgroundImage)。

      尝试 {
         ImageView的背景=(ImageView的)findViewById(R.id.bgImage);
         //另一种选择,如果意图额外的存储资源的名称,
         //而不是整数资源ID
         //类<> C = R.drawable.class;
         //background.setImageResource(c.getDeclaredField(bgImage).getInt(c));
         background.setImageResource(渣油);
      }赶上(例外五){
         e.printStackTrace();
      }
   }
 

我也显示上述一些注释掉code,如果由于某种原因,你需要传递一个字符串图像资源的名称,而不是一个整数资源code。注释掉code查找为给定的图像名称的资源ID。据我ImageAdapter使用的资源名称,样品字符串名称传递可能是PIC1。当然,如果你这样做,调用活动(HelloGridViewActivity)需要连接code中的额外的意向作为一个字符串,而不是一个整数。

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.

How can I do that?

This is my activity that shows the grid view:

public class HelloGridViewActivity extends Activity {

@Override
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));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

//WHAT SHALL I PUT HERE????
    }
}
}

解决方案

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.

这篇关于如何在点击图像设置为背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆