Parallax Effect In Android's Viewpager
Solution 1:
An easy way to do this without using a custom library is to implement ViewPager.PageTransformer
I created my layouts to use the same id
for their background element. For my example all background images will use the id background
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/background"
android:src="@drawable/img12"
android:scaleType="fitXY"/>
Then when I go in to implement ViewPager.PageTransformer
I can reference that image like so:
publicclassTransformerimplementsViewPager.PageTransformer{
@OverridepublicvoidtransformPage(View page, float position) {
if(position >= -1 && position <= 1){
page.findViewById(R.id.background).setTranslationX(-position * page.getWidth() / 2);
} else {
page.setAlpha(1);
}
}
}
Finally, I assign my ViewPager.PageTransformer
to my ViewPager
like so.
ViewPagerpager= (ViewPager) findViewById(R.id.pager);
pager.setPageTransformer(false, newTransformer());
Solution 2:
This question is a bit old, but I found it when I was trying to do exactly that...
I implemented my own solution, basically extending ViewPager
and overriding onDraw
.
You can find all the code with a simple example here
Solution 3:
I know that it's a bit old but take a look to that https://github.com/xgc1986/ParallaxPagerLibrary
It not overrides the onDraw method, and the effect not only with images, it works with every kind of view
mPager.setPageTransformer(false, newParallaxTransformer(R.id.parallaxContent));
R.id.paraallaxContent is the id of the View you want to have this effect
unless the other solutions, don't need any any concrete structure to work, and also is layout independant
demo: youtube
Solution 4:
Maybe this library can help you:
Solution 5:
You can take a look at this :
https://github.com/luxsypher/ParallaxViewPager
you can apply a parallax effect on any element of your view and gave them all different speed
Post a Comment for "Parallax Effect In Android's Viewpager"