0

我有我的佈局的工具欄和PagerSlidingTabStrips,我在同時選擇不同的頁面或與頁面關聯的列表滾動時更改兩者的drawable。下面的代碼:Drawable不能按預期工作

Drawable DRAWABLE[] = new Drawable[8]; 
DRAWABLE[0] = getResources().getDrawable(R.drawable.actionbar_bg); 
DRAWABLE[1] = getResources().getDrawable(R.drawable.actionbar_bg_red); 
DRAWABLE[2] = getResources().getDrawable(R.drawable.actionbar_bg_blue); 
DRAWABLE[3] = getResources().getDrawable(R.drawable.actionbar_bg_orange); 
DRAWABLE[4] = getResources().getDrawable(R.drawable.actionbar_bg_grey); 
DRAWABLE[5] = getResources().getDrawable(R.drawable.actionbar_bg); 
DRAWABLE[6] = getResources().getDrawable(R.drawable.actionbar_bg_red); 
DRAWABLE[7] =getResources().getDrawable(R.drawable.actionbar_bg_blue); 

//使用onPageSelected方法:

@Override 
public void onPageSelected(int position) { 
    // TODO Auto-generated method stub 

    mHeaderLogo.setImageResource(IMAGES[position]); 

    mPagerStripView.setBackground(DRAWABLE[position]); 

    mActionBarView.setBackground(DRAWABLE[position]); 

} 

// OnScroll方法:

@Override 
public void onScroll(AbsListView view, int firstVisibleItem, 
     int visibleItemCount, int totalItemCount, int pagePosition) { 
    // TODO Auto-generated method stub 

    if (mViewPager.getCurrentItem() == pagePosition) { 

     int scrollY = getScrollY(view); 

     ViewHelper.setTranslationY(mHeader, 
       Math.max(-scrollY, mMinHeaderTranslation)); 
     float ratio = clamp(ViewHelper.getTranslationY(mHeader) 
       /mMinHeaderTranslation, 0.0f, 1.0f); 

     int alpha = (int) (ratio * 255); 

     Drawable currentDrawable = DRAWABLE[pagePosition]; 

     mCurrentDrawable.setAlpha(alpha); 
     mActionBarView.setBackground(mCurrentDrawable); 

     mPagerStripView.setBackground(mCurrentDrawable); 



    } 

} 

但Alpha值確實如預期都沒有改變。您可以從GIF看到我努力實現

enter image description here

+0

據我所知的所有資源值是s tored爲'int'類型。將'Drawable DRAWABLE []'更改爲'int DRAWABLE []' – Apurva 2015-03-25 07:24:28

+0

當列表滾動時,我正在更改drawable的alpha值。 – Pankaj 2015-03-25 07:30:10

回答

1

替換此

int DRAWABLE[] = new int[8]; 
     DRAWABLE[0] = R.drawable.ic_launcher; 
     DRAWABLE[1] = R.drawable.actionbar_bg_red; 
     DRAWABLE[2] = R.drawable.actionbar_bg_blue; 
     DRAWABLE[3] = R.drawable.actionbar_bg_orange; 
     DRAWABLE[4] = R.drawable.actionbar_bg_grey; 
     DRAWABLE[5] = R.drawable.actionbar_bg; 
     DRAWABLE[6] = R.drawable.actionbar_bg_red; 
     DRAWABLE[7] = R.drawable.actionbar_bg_blue; 

替換此

setBackgroundResource(DRAWABLE[position]); 

setbackground(DRAWABLE[position]);