如何在点击时将图像设置为背景图像?

时间:2012-05-28 22:21:42

标签: android image background

我的活动显示了包含不同图片的网格视图。当点击其中一个图像时,我希望点击的图像成为另一个活动的背景图像。

我该怎么做?

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

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

2 个答案:

答案 0 :(得分:2)

因此,海报没有(最初)确切地指定是否要在点击时立即打开其他活动,或者是否需要记录点击的项目图像以供日后使用。第一个问题对我来说似乎更常见,例如GridView显示一组图像缩略图。用户点击它,会出现一个显示该项信息的新活动。缩略图可能会全屏显示。无论如何,也许这不是海报的想法,但这是我的假设(也许有人最终会在考虑这样一个用例的情况下找到答案)。

也没有说明图像的存储方式,因此我会假设

  1. ImageAdapter的图片是此应用的捆绑资源
  2. 我们可以对ImageAdapter进行一些更改,以存储一些数据来解决此问题
  3. 因此,我采用标准ImageAdapter,并在ImageView的tag属性中添加一行代码来记录每个ImageView的整数资源ID。有很多方法可以做到这一点,但这是我选择的方式。

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

    然后,当Hello活动点击了一个网格项时,我检索图像的资源ID并将其作为Intent额外的(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);
                }
             }
          });
       }
    

    最后,当新活动(AnotherActivity)打开时,我们检索该额外的意图,并将其解码为整数资源ID,并将其设置为全屏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();
          }
       }
    

    我还在上面显示了一些注释掉的代码,如果由于某种原因需要传递图像资源的字符串名称,而不是整数资源代码。注释掉的代码查找给定图像名称的资源ID。根据我的ImageAdapter中使用的资源名称,要传递的示例字符串名称可能是"pic1"。当然,如果你这样做,调用Activity(HelloGridViewActivity)需要将Intent extra编码为String,而不是整数。

答案 1 :(得分:1)

获取所需图像的资源ID作为BG并将其保存到首选项。

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        saveImageId(position);
    }

private void saveImageId(int position) {
    int id = getImageId(); // get the R.drawable.* id of the image. You should be able to figure this out.
    Editor ed = PreferenceManager.getDefaultSharedPreferences(this).edit();
    ed.putInt("bg_image_id", id);
    ed.commit();
}

现在,在您的其他活动中,您可以获取图片ID:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int id = prefs.getInt("bg_image_id", R.drawable.default_background); //Get image id, use default background if there isn't one.
LinearLayout mLayout = (LinearLayout) findViewById(R.id.background_layout);
mLayout.setBackgroundResource(id);
祝你好运