android setAlpha在imageView到CollapsingToolbarLayout不起作用 [英] android setAlpha on imageView into CollapsingToolbarLayout does not work

查看:161
本文介绍了android setAlpha在imageView到CollapsingToolbarLayout不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CollapsingToolbarLayout imageview里面,我需要setAlpha(float)上下滚动时:

inside of CollapsingToolbarLayout imageview and i need setAlpha(float) When that scroll up and down:

这是获取偏移量并按偏移量计算浮点数的方法:

this is method for get Offset and calculate float by offset:

AppBarLayout.OnOffsetChangedListener:

AppBarLayout.OnOffsetChangedListener:

 @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        float offsetAlpha = (float) (verticalOffset / appBarLayout.getTotalScrollRange());
        imageView.setAlpha(offsetAlpha);
    }

xml布局:

 <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="50dp"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@android:color/transparent">

      <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax" />

 </android.support.design.widget.CollapsingToolbarLayout>

但是setAlpha不起作用.为什么?

but setAlpha does not work. why?

推荐答案

您的计算结果为0.0或1.0.
我尝试通过获取完整滚动区域的分数来计算偏移量.

Your calculation either gave 0.0 or 1.0.
I tried calculating the offset by getting the fraction of the complete scrollarea.

这是一个开始并为改进而开放.

It's a start and open for improvement.

appbar.addOnOffsetChangedListener(new OnOffsetChangedListener() {
    @Override
    public void onOffsetChanged(final AppBarLayout appBarLayout, final int verticalOffset) {
        float offsetAlpha = (appBarLayout.getY() / appbar.getTotalScrollRange());
        imageView.setAlpha( 1 - (offsetAlpha * -1));
    }
})

这篇关于android setAlpha在imageView到CollapsingToolbarLayout不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆