2012-05-28 173 views
0

我有一個活動,顯示不同圖像的網格視圖。當點擊其中一幅圖像時,我希望點擊的圖像成爲另一個活動的背景圖像。如何在點擊時將圖像設置爲背景圖像?

我該怎麼做?

這是我的活動,顯示網格視圖:

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

如果您以某種方式在GridView中加載圖像,則只需使用Intent將相同的信息(URL或文件URI)發送到第二個活動。 –

+0

@ user1420042:您的'GridView'適配器如何被填充?使用資源ID或文件URI? – Squonk

回答

2

所以,海報沒有(原來)精確指定的其他活動是否在點擊時立即打開,或者如果點擊的項目圖像只是需要記錄以備後用。第一個問題在我看來更常見,如顯示一組圖像縮略圖的GridView。用戶點擊它,並顯示一個新的活動,只顯示該項目的信息。該縮略圖圖像可能會全屏顯示。無論如何,也許這不是海報的想法,但那是我的假設(也許有人最終會在考慮到這種用例的情況下找到這個答案)。

也沒有指定圖像的存儲方式,所以我會假設

  1. ImageAdapter的圖像被捆綁這個應用程序的資源
  2. 我們可以做出一些更改ImageAdapter,存儲一些數據來解決這個問題

所以,就這樣,我拿一個標準ImageAdapter,並添加一行代碼記錄每個ImageView的整數資源ID,在的ImageView tag財產。有很多方法可以做到這一點,但這是我選擇的方式。

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

然後,當你好活動有一個網格項點擊,我檢索圖像的資源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編碼爲字符串,而不是整數。

+0

寫得很好。榮譽。 –

+0

嘿Nate,謝謝!我只有一個問題:活動(AnotherActivity)是我的啓動活動。當啓動我的應用程序時,它會崩潰,因爲它試圖獲得尚未發送的意圖。任何解決方案 – user1420042

+0

@ user1420042,我們需要更多關於您的活動如何使用的信息。您的啓動活動(AnotherActivity)...它只是在應用程序啓動時更改其背景圖片嗎?如果是這樣,那麼使用'SharedPreferences'的其他答案可能就是你想要的。或者,此啓動活動是否也會在應用程序運行時回到導航狀態?當發生這種情況時,背景圖像是否應該改變?請將您的澄清添加到原始問題中,這樣每個人都很容易看到您正在嘗試做什麼。謝謝。 – Nate

1

得到你想要的BG圖像的資源ID,並保存到偏好。

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

好運

+0

SharedPreferences應該用於應用程序首選項,而不是臨時數據。 GridView活動中的一個全局靜態變量用於存儲單擊的圖像的資源ID將是一個更好的解決方案。 – shanet

+0

「//獲取圖像的R.drawable。* id,你應該能夠弄清楚這一點。」如果我不知道它 – user1420042

+0

@shanet:您的評論中的建議沒有比從BananaNutTruffles回答更有效。你有沒有聽說過'意圖'額外? OP想要開始一個新的「活動」......這不是一個難以回答的問題。 – Squonk