2

I have toolbar, tab layout and imageview inside Collapsing layout. I need to change alpha of that image to be equal of collapse state of that imageview. It means ->

image completely hide : alpha = 0%

image 50% scrolled : alpha = 50%

image completely shown : alpha = 100%

Unfurtunatelly, i haven't found any xml parameter which could affect this. Is there any simple solution for this?

Tom Wayne
  • 1,288
  • 1
  • 12
  • 31

2 Answers2

3

Also if you have some layouts you could use scroll listener like this:

appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
            float offsetAlpha = (appBarLayout.getY() / appBarLayout.getTotalScrollRange());
            int value = 255 - (int)(255 * offsetAlpha * -1);
            toolbarTotalContainer.getBackground().setAlpha(value);
        });

Where toolbarTotalContainer is some container of some Views.

Djek-Grif
  • 1,391
  • 18
  • 18
2

You can try a couple of things depending on your needs.

If you're looking for a simple solution you may be able to use the app:contentScrim XML attribute on CollapsingToolbarLayout to achieve the effect you're looking for. See: Coordinator Behavior ImageView

If that isn't sufficient, a more "correct" solution would involve adding an OnOffsetChangedListener and using that to calculate and apply the alpha change for your ImageView. See: android setAlpha on imageView into CollapsingToolbarLayout does not work

Community
  • 1
  • 1
Bryan Dunlap
  • 1,156
  • 10
  • 7