Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Saurabh(aqua) in 2017.
- */
- public class ScrollActivity extends BaseActivity {
- /* view binding */
- @BindView(R.id.scrollView)ScrollView scrollView;
- @BindView(R.id.parallax_image)ImageView parallaxImage;
- @BindView(R.id.tv_perhour)TextView textView;
- @BindView(R.id.tv_heading)TextView heading;
- /* color binding */
- @BindColor(R.color.white)int whiteColor;
- @BindColor(android.R.color.transparent)int transparentColor;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /* intially hide the view */
- heading.setAlpha(0f);
- /* set the scroll change listener on scrollview */
- scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
- @Override
- public void onScrollChanged() {
- /* get the maximum height which we have scroll before performing any action */
- int maxDistance = parallaxImage.getHeight();
- /* how much we have scrolled */
- int movement = scrollView.getScrollY();
- /*finally calculate the alpha factor and set on the view */
- float alphaFactor = ((movement * 1.0f) / (maxDistance - heading.getHeight()));
- if (movement >= 0 && movement <= maxDistance) {
- /*for image parallax with scroll */
- parallaxImage.setTranslationY(-movement/2);
- /* set visibility */
- heading.setAlpha(alphaFactor);
- }
- }
- });
- }
- /* can be replaced with setContentView() in onCreate() */
- /* kept for brevity */
- @Override
- protected int getActivityLayout() {
- return R.layout.ac_parallax;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement