Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android setAlpha on imageView into CollapsingToolbarLayout does not work

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:

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

xml layout:

 <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>

but setAlpha does not work. why?

like image 398
Farzad Avatar asked Jun 18 '16 14:06

Farzad


1 Answers

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));
    }
})
like image 140
timr Avatar answered Sep 22 '22 17:09

timr