I have created a scrollview and an imageview is placed within it. I'd like on scroll for it to resize in the same fashion it is done in the image below but so far I've had little success.
In my attempts the image is resized on scroll but, there is space left over after the resize. How would you modify the below:
Image:
My Code so far:
activity_main.xml
<ImageView
android:layout_gravity="center"
android:adjustViewBounds="true"
android:layout_width="601dp"
android:layout_height="250dp"
android:paddingTop="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:scaleType="fitXY"
android:id="@+id/contactPic"
android:src="@drawable/stock"
android:clickable="true"/>
MainActivity:
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
final ImageView contactPicture = (ImageView) findViewById(R.id.contactPic);
final RelativeLayout contactLayout = (RelativeLayout) findViewById(R.id.ContactRLayout);
if (scrollView == contactScrollView) {
View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
int distanceFromPageEnd = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));
Log.e("onScrollChanged", "distance from bottom = " + String.valueOf(distanceFromPageEnd));
if (distanceFromPageEnd >= 1408)
{
contactPicture.getLayoutParams().height = (distanceFromPageEnd - 1408);
contactPicture.requestLayout();
}
}
ScrollViewListener:
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
ObservableScrollView:
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if(scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}
There is simple example below that shows how to achieve parallax effect.
First, put your
ImageView
and other views intoFrameLayout
:LinearLayout
's top margin is equal to theImageViews
's height (@dimen/contact_photo_height
in our example).Then we should listen scroll position of the
ScrollView
and change the position ofImageView
: