滑动时颜色变化平滑

时间:2016-03-30 09:47:53

标签: java android layout

我正在使用SlidingUpPanelLayout,我希望在#4096c3向上滑动时(浮动slideOffset介于0.0到1.0之间),使用此代码将布局颜色从白色更改为SlidingUpPanelLayout

    SlidingUpPanelLayout.PanelSlideListener onslide = new SlidingUpPanelLayout.PanelSlideListener() {
    @Override
    public void onPanelSlide(View panel, float slideOffset) {
        backcolor = android.graphics.Color.argb(Math.round(255 * slideOffset), 64, 150, 195);
        panel_layout.setBackgroundColor(backcolor);
    }

    @Override
    public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) {

    }
};

此代码有什么问题,因为当我突然滑动时,它会从白色变为#4096c3

1 个答案:

答案 0 :(得分:1)

您的LinearLayout(Header)参考: -

LinearLayout mTitleLinearLayout= (LinearLayout) findViewById(R.id.llyout_header);

您的SlidingUpPanelLayout参考: -

SlidingUpPanelLayout mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);

你的听众: -

mLayout.addPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() {

        @Override
        public void onPanelSlide(View panel, float slideOffset) {

            Log.i(TAG, "onPanelSlide, offset " + slideOffset);

            //Here is the code which change titleBar color gradually
           mTitleLinearLayout.setBackgroundColor((int) new ArgbEvaluator().evaluate(slideOffset, Color.parseColor("#03A9F4"), Color.parseColor("#F44336")));


        }

        @Override
        public void onPanelStateChanged(View panel, PanelState previousState, PanelState newState) {



        }

        // During the transition of expand and collapse onPanelSlide function will be called.

    });