I would implement this using a scroll callback in a ScrollView, this does not exist by default, but it is easy to create it by extending the ScrollView:
public class UpdatingScrollView extends ScrollView { private OnScrollChangedListener mScrollChangedListener; public interface OnScrollChangedListener { public void onScrollChanged(int x, int y, int oldx, int oldy); } public UpdatingScrollView(Context context) { super(context); } public UpdatingScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public UpdatingScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setOnScrollChangedListener(OnScrollChangedListener listener) { mScrollChangedListener = listener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (mScrollChangedListener != null) mScrollChangedListener.onScrollChanged(x, y, oldx, oldy); } }
Therefore, replace ScrollView in your layout with com.your.package.UpdatingScrollView .
In your fragment / event where the UpdatingScrollView parameter is defined, you set the scroll listener and update the bottom edge of the image based on the scroll offset. For every 2 pixels of scroll scrolling, you want to update the bottom margin by -1 pixel, which will cause the image to move up the screen at half the speed of the rest of the content.
So something like this:
scrollView.setOnScrollChangedListener(new OnScrollChangedListener() { @Override public void onScrollChanged(int x, int y, int oldx, int oldy) {
source share