grizzly grizzly - 5 months ago 68
Android Question

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?

tim tim
Answer

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));
    }
})
Comments