【问题标题】:Drawable doesn't work as expectedDrawable 不能按预期工作
【发布时间】:2015-03-25 07:10:03
【问题描述】:

我有工具栏和PagerSlidingTabStrips我的布局,我在选择不同的页面或滚动与页面关联的列表时,我会在同一页面上更改两者的抽屉。代码如下:

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 中看到我想要实现的目标

【问题讨论】:

  • AFAIK 所有资源值都存储为int 类型。将Drawable DRAWABLE[] 更改为int DRAWABLE[]
  • 当列表滚动时,我正在更改 drawable 的 alpha 值。

标签: android android-layout drawable android-drawable xml-drawable


【解决方案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]);

【讨论】:

    最近更新 更多