Animation transition between ViewPager - android

Please see the two screenshots :
The top part seems to be using a ViewPager. The current screen on the ViewPager shows partial of the previous and next screens.
Question : How do we achieve this animation ? i.e. where partial of the previous ane next screens can be seen ??

Just use margins/paddings in your case:
int leftRightPadding = ... ;
int margin = .... ;
viewPager.setClipToPadding(false);
viewPager.setPadding(leftRightPadding, 0, leftRightPadding, 0);
viewPager.setPageMargin(margin);
Also check JazzyViewPager or equivalent libs for zoom/fade animations.

Related

How to show tab indicator on top of tab in android?

I have an app in which I have added TabLayout with three tabs. I want to show tab indicator from bottom to top but the problem is that when I show indicator from bottom to top tab icon also rotate.How do I resolve this issue?
mTabLayout.getTabAt(0).setCustomView(view);
mTabLayout.getTabAt(1).setIcon(tabIcons[1]);
mTabLayout.getTabAt(2).setIcon(tabIcons[2]);
mTabLayout.setRotationX(180);
I have similar requirement in my previous application
Please try below code it works perfectly for me
//First rotate the tab layout
tabOrderType.setRotationX(180);
//Find all childs for tablayout
for (int i = 0; i <tabOrderType.getChildCount() ; i++) {
LinearLayout linearList = ((LinearLayout)tabOrderType.getChildAt(i));
for(int position = 0;position<linearList.getChildCount();position++) {
//One by one again rotate layout for text and icons
LinearLayout item=((LinearLayout) linearList.getChildAt(position));
item.setBackground(getResources().getDrawable(R.drawable.custom_button_border));
item.setRotationX(180);
}
}
This is working nicely in my application and not creating any issue till now.Another way is you need to make custom tab to manage this.
Hope this helps you...if any issue please ask me...
Add this in your TabLayout XML
app:tabIndicatorGravity="top"

Android: PageTransformer applying correct scale but not alpha before first touch

I have a PageTransformer that correctly displays my ViewPager with the second element correctly scaled, but there is no alpha applied to the second element. However, upon dragging the ViewPager a miniscule amount, the next view is properly displayed. The code for both my alpha and scale is identical, I'm not sure what the issue here is.
I have tried calling code from a handler once the ViewPagerAdapter has things added to it (such as viewPager.scrollBy() and pager.beginFakeDrag()) but these didn't help.
viewPager.setOffscreenPageLimit(2) is set. I have tried setting alpha to the views programmatically when instantiated in the Adapter but it has no effect. If the scale is correctly being applied then the alpha should be as well, surely.
#Override
public void transformPage(View page, float position)
{
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float alphaFactor = Math.max(MIN_ALPHA, 1 - Math.abs(position));
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
page.setAlpha(alphaFactor);
}
(EDIT) CORRECT SOLUTION:
Replace ViewPager with ViewPager2.
Useful resources:
https://developer.android.com/training/animation/vp2-migration
https://stackoverflow.com/a/57694887/10504386
LAZY (AND NOT SO COOL, ERROR PRONE) SOLUTION:
Unfortunately for me, I did not have the property android:animateLayoutChanges=true set in my layout.
I faced the same problem when I moved the ViewPager from an Activity to a Fragment.
I think the problem is due to the fact I reused the fragment. Upon a FragmentTransaction, the ViewPager technically still has its old value, so its view doesn't need to change, that's why the PageTransformer isn't fired.
Basically, all you need to do is to set the ViewPager's currentItem to 0, and then set it to the value you want to.
This is my snippet (MyViewPager.kt, but you don't need to override ViewPager).
init {
post {
currentItem = 0
}
}
fun setCurrentPage(position: Int) {
post {
setCurrentItem(position, true) // I want smooth scroll
}
}
I had android:animateLayoutChanges=true set on the ViewPager. Once I removed this, it loaded correctly. Don't ask me why it was there in the first place.

PagerSlidingTabStrip making horizontalscrollview's width fixed

I'm using PagerSlidingTabStrip library in my application now. I have 5 tabs, so it's over screen width. so I have to scroll to see last tab.
I want to see all tabs on the screen and never wanna scroll to see other items.
I tried changing HorizontalScrollView to LinearLayout in PagerSlidingTabStrip.java but it's weird a little. Indicator moved bad.
// public class PagerSlidingTabStrip extends HorizontalScrollView
public class PagerSlidingTabStrip extends LinearLayout
and also I tried shouldExpand options is true. but it didn't work again.
app:pstsShouldExpand="true"
What can i do for this ????
I had exactly same problem but following code ,I found that if you need to set tabs shouldExpand you need to do it before setting viewpager to tabs.
tabs = (PagerSlidingTabStrip) findViewById(R.id.slidingTabStrip);
//Before setting view pager
tabs.setShouldExpand(true); //Works
tabs.setViewPager(vpPager);
//After setting view pager
tabs.setShouldExpand(true); //Will not work
I solved this problem by myself. The problem is shouldExpand Attr doesn't work because our tabs are 5 so I can't. but when I set my tab count 4, it works and looks good. They filled with device screen width.
Anyway I changed this width size.
defaultTabLayoutParams = new LinearLayout.LayoutParams(dm.widthPixels/your tab count, LayoutParams.MATCH_PARENT);
I hope it will be helpful and save your time.

Parallax Effect in Android's ViewPager

I'm trying to emulate a parallax effect when sliding through my fragments, I've read about beginFakeDrag and fakeDragBy but to be honest, I don't know even if it's the best approach to my problem.
Anyone has done something similar with the ViewPager or have a hint about how should I approach this?
Thank you
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:
public class Transformer implements ViewPager.PageTransformer{
#Override
public void transformPage(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.
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setPageTransformer(false, new Transformer());
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
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, new ParallaxTransformer(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
Maybe this library can help you:
https://github.com/garrapeta/ParallaxViewPager
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
This is a ViewPager subclass I wrote, pretty easy to use. You don't need to do anything different with it, just include the dependency and use it as a standard ViewPager. Available on GitHub.
This library is fully customizable in x and y directions and includes alpha effects:
https://github.com/prolificinteractive/ParallaxPager
Installation (as of v0.7, check README for updates):
Add as a Maven Central dependency with Gradle
Use a ParallaxContainer in layout XML instead of ViewPager
Create a layout XML file for each page (the x/y/alpha attributes can be set separately for each object moving in/out of the page)
There are a few copy/paste lines to add to onCreate of your Activity
Maybe this library could be useful:
https://github.com/matrixxun/ProductTour
?
it uses "ViewPager.PageTransformer" for making things move differently inside.

Android animation moves the Layout, but not the buttons

Say I have a "main screen" Layout which has to move up, to reveal a "history bar".
if ( HistoryBar == false )
{
HistoryBar = true;
TranslateAnimation MainScreenPullUp = new TranslateAnimation(0, 0, 0, -200 );
MainScreenPullUp.setDuration(350);
MainScreenPullUp.setFillAfter(true);
LinLayMain.startAnimation(MainScreenPullUp);
}
Right now I am doing it like that. But the problem is, that while the main screen layout itself moves up, all the button places keep the same, i.e. the button picture moves, but the click-spot remains in place. Any ideas how to fix that?
I tried useing an animationListener, instead setFillAfter(true) , which would TanslateY the main screen layout, once the animation stops, but it makes the layout jump and flicker. Are there any other ways?
edit:
Thanks, Vineet.
Got it working with:
ObjectAnimator pullUp = ObjectAnimator.ofFloat(LinLayMain, "translationY", 0f, -200f);
pullUp.setDuration(350);
pullUp.start();
I faced the same problem. From 3.0 android has property animation. Below 3.0 I tackled this issue with the help of view's method offsetLeftAndRight (int offset) and offsetTopAndBottom (int offset). You can refer this link for more details.
For Property animation refer this link.

Categories

Resources