Created
February 21, 2014 11:08
-
-
Save rndstr/9132543 to your computer and use it in GitHub Desktop.
vertical parallax image scroller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.schilter.faactors.view; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.ScrollView; | |
/** | |
* ScrollView with a callback | |
* | |
* @author Roland Schilter <roli@schilter.me> | |
*/ | |
public class ObservableScrollView extends ScrollView { | |
private ObservableScrollViewListener mScrollViewListener = null; | |
public interface ObservableScrollViewListener { | |
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); | |
} | |
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(ObservableScrollViewListener scrollViewListener) { | |
mScrollViewListener = scrollViewListener; | |
} | |
@Override | |
protected void onScrollChanged(int x, int y, int oldx, int oldy) { | |
super.onScrollChanged(x, y, oldx, oldy); | |
if (mScrollViewListener != null) { | |
mScrollViewListener.onScrollChanged(this, x, y, oldx, oldy); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.schilter.faactors.view; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
/** | |
* Image view that shifts the image vertically while scrolling | |
* | |
* Make sure to set the translation in your OnScrollListener | |
* | |
* @author Roland Schilter <roli@schilter.me> | |
*/ | |
public class ParallaxImageView extends ImageView { | |
protected int mTranslation = 0; | |
public ParallaxImageView(final Context context) | |
{ | |
super(context); | |
} | |
public ParallaxImageView(final Context context, final AttributeSet attrs) | |
{ | |
super(context, attrs); | |
} | |
public ParallaxImageView(final Context context, final AttributeSet attrs, final int defStyle) | |
{ | |
super(context, attrs, defStyle); | |
} | |
public void setTranslation(int translation) { | |
mTranslation = translation; | |
invalidate(); | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
canvas.save(); | |
canvas.translate(0, mTranslation/2); | |
super.draw(canvas); | |
canvas.restore(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...] | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = (LinearLayout) inflater.inflate(R.layout.movie_detail_fragment, container, false); | |
ObservableScrollView scroller = (ObservableScrollView) view.findViewById(R.id.scrollview); | |
final ParallaxImageView backdrop = (ParallaxImageView) scroller.findViewById(R.id.backdrop_picture); | |
scroller.setScrollViewListener(new ObservableScrollView.ObservableScrollViewListener() { | |
@Override | |
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { | |
backdrop.setTranslation(y); | |
} | |
}); | |
return view; | |
} | |
[...] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment