2012-02-22 23 views
0

我有一個gridview設置了這樣的圖像的列表:更新gridview的位置與透明圖像

public int[] tb = 
{ R.drawable.tb1, R.drawable.tb2, R.drawable.tb3, R.drawable.tb4, 
     R.drawable.tb5, R.drawable.tb6, R.drawable.tb7, R.drawable.tb8, 
     R.drawable.tb9, R.drawable.tb10, R.drawable.tb11, R.drawable.tb12, 
     R.drawable.tb13, R.drawable.tb14, R.drawable.tb15, R.drawable.tb16, 
     R.drawable.tb17, R.drawable.tb18 }; 

我知道如何通過例如改變所述陣列中的位置更新特定圖像

tb[2] = R.drawable.image; 

然後使用

mAdapter.notifyDataSetChanged(); 

但我的問題是,我想更新在網格視圖圖像和設置alpha因此圖像是透明的。現在與正常的圖像視圖很簡單,因爲做

ImageView iv = (ImageView) findViewById(R.id.aTest); 
iv.setAlpha(127); 

但我如何適用於此到網格視圖中的特定圖像。圖像以一組整數形式存儲,並使用ImageAdapter應用圖像。因此,這意味着我不能只是做

tb[0].setAlpha(127); 

,因爲它不承認它的形象圖。我知道我可以在適配器Alpha中設置imageView,但這意味着所有圖像都是透明的,我只希望少數幾個透明。

所以任何人都可以告訴我如何設置它,以便我可以更換一個圖像在列表中,並使其透明。我已經設置了onClickItemListener,所以暫時試圖設置它,所以當我點擊網格視圖中的一個項目時,該圖像將變得透明。

嘗試了很多不同的解決方案,但似乎無法找到任何可行的方法。如果有人能指出我正確的方向,將非常感激!

+0

你想設置透明圖像後點擊位置。 – 2012-02-22 11:22:39

+0

現在是,只是爲了讓它工作。這些圖像是半小時的時間段,我現在擁有的是我有一個循環訪問數組的方法,並且每個時隙都有可能處於活動狀態,如果處於非活動狀態,圖像將變爲有點褪色的圖像。這工作正常,但我希望這個褪色的圖像是透明的,因此需要計算出這部分 – AdamM 2012-02-22 11:29:44

回答

1

還有就是要解決這個沒有簡單的方法,但在這裏你去 -

  • 修改您getViewAdapter以這種方式。 (我假設你有一個自定義適配器,否則你將不得不寫一個。看到這個example如何做到這一點)

    public View getView(int position, View convertView, ViewGroup parent) {   
        ImageView imageView;   
        if (convertView == null) { 
         //create new image view 
        } else {    
         // resuse 
        }   
    
        imageView.setImageResource(mThumbIds[position]);  
    
        // new code starts 
        if(clickedLocation == position){ 
         imageView.setAlpha(alphaValue) 
        } 
        // new code ends 
    
        return imageView;  
    } 
    
  • 存儲該項目的位置從onClickListenerclickedLocation

  • 現在在GridView對象上調用invalidateViews()方法。

注意事項:(可能不是去了解它的最佳方式)

  • 您將重繪整個GridView每次有一個點擊,你可能會看到一個閃爍效果
  • 如果您資源是JPG或PNG然後他們需要他們需要每次解碼
+0

非常感謝!你的代碼工作完美。花了很多時間試圖找出一個笑聲,歡呼! – AdamM 2012-02-22 11:54:55