android scrollview animation - android

I am trying to animate a Scrollview.
Particularly the SmoothScrollTo(x,y) function. Any ideas on how I go about this.
Thanks
ScrollView scroller = (ScrollView) findViewById(R.id.scroller);
if(true){
scroller.smoothScrollTo(0,30);
animation anim = AnimationUtils.loadAnimation(this, R.anim.down);
anim.setFillAfter(true);
scroller.startAnimation(anim);
} else{
.....
}

If you want to achieve smooth scrolling there is no need to animate the scroll.
Use reflection to set up your own custom Scroller & Interpolator.
something like this:
Field mScroller;
mScroller = ScrollView.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
CustomScroller scroller = new CustomScroller(getContext(), new AccelerateInterpolator());
mScroller.set(this, scroller);
Surround Try/Catch & there u go.
user smoothScrollTo method.

Im not sure whether you add animation to the scroller. I maybe wrong. One method would be to use the Timer and TimerTask to schedule a task to increment/decrement the scrollTo values.
You can vary the interval in which the task executes to speedup and slow down the scrolling.

Related

How to change animations used by animateLayoutChanges mechanism?

I have a RecyclerView in which user selects an item. Upon selection certain view becomes visible before the text (see image). I want to animate its appearance.
animateLayoutChanges property on the item's layout does the job perfectly, except for the animation type for view appearance - it fades in. I want it to scale from 0 to 100% size, I believe it's called 'pop up' animation.
If I disable animateLayoutChanges and use XML animation for that, it works, but the nearby text is no longer animated (it should slide to accomodate space for the view and its margin). It instantly shifts to the right and then animation is played. This is worse with reverse animation, since the text overlaps the view before it has disappeared.
So I need to combine default mechanism and my own animation somehow.
What would be the simplest way to accomplish that without delving into custom animations?
After much digging in the wrong places, found the answer myself. Might help somebody.
animateLayoutChanges property if enabled utilizes an instance of LayoutTransition to do its job in ViewGroup.
So in order to change animations, you can create an instance of LayoutTransition, set Animator for the transition that you need to change and then assign that instance to ViewGroup via setLayoutTransition().
In my case the result is:
Animator scaleDown = ObjectAnimator.ofPropertyValuesHolder((Object)null, PropertyValuesHolder.ofFloat("scaleX", 1, 0), PropertyValuesHolder.ofFloat("scaleY", 1, 0));
scaleDown.setDuration(300);
scaleDown.setInterpolator(new OvershootInterpolator());
Animator scaleUp = ObjectAnimator.ofPropertyValuesHolder((Object)null, PropertyValuesHolder.ofFloat("scaleX", 0, 1), PropertyValuesHolder.ofFloat("scaleY", 0, 1));
scaleUp.setDuration(300);
scaleUp.setStartDelay(300);
scaleUp.setInterpolator(new OvershootInterpolator());
LayoutTransition itemLayoutTransition = new LayoutTransition();
itemLayoutTransition.setAnimator(LayoutTransition.APPEARING, scaleUp);
itemLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, scaleDown);
ViewGroup av = (ViewGroup)v.findViewById(R.id.animated_layout);
av.setLayoutTransition(itemLayoutTransition);
More info in LayoutTransition reference documentation.
You can do same thing with animateLayoutChange just see following article
https://proandroiddev.com/the-little-secret-of-android-animatelayoutchanges-e4caab2fddec

Android animation only works once?

So i'm trying to do 2 animations simultaneously, one to move a textview, and one to show a linearlayout (also 2 animations to hide them). I have another animation working as intended to show/hide a seperate layout. When i execute showing the view with 2 animations, it works once, it hides fine, but then doesn't work again. Then when i show the other view, it plays all 3 animations (not intended). I can't figure out why this is happening? When i try to show the 2 animations it does nothing, but then when i try the other show view its like it was added to a queue and it shows all 3.
My code for initiating the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_show);
anim.setAnimationListener(new AnimListener(layoutMsgs, View.VISIBLE)); // sets visibility on animation end
layoutMsgs.startAnimation(anim);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation tvAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_show);
tvMsgs.startAnimation(tvAnim);
My code for hiding the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation animLayout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_hide);
animLayout.setAnimationListener(new AnimListener(layoutMsgs, View.INVISIBLE));
layoutMsgs.startAnimation(animLayout);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation animMsgs = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_hide);
tvMsgs.startAnimation(animMsgs);
Then this is the other animation that is working fine, it's only one animation, no textView, just a layout
LinearLayout pokeLayout = (LinearLayout)findViewById(R.id.layoutPokes);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.poke_show);
anim.setAnimationListener(new AnimListener(pokeLayout, View.VISIBLE));
pokeLayout.startAnimation(anim);
So how can i fix this? Sorry if my explanation is bad i'm finding it difficult to explain all the details, please ask for any missing information.
solved by using solution in this post Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension
wasn't pretty as I had to create seperate translate X and translate Y res files for each view I need to move.. ended up with like 15 anim files. Would still like to know if theres a better solution

ObjectAnimator in android

I want to show a objectanimator in android for a imageview.This imageview should move from top of the screen to about 30 pixels down from top of the screen.Once it(imageview) reaches there and onclick of the imageview the image view should disappear.I have read that i cannot use translate animation as the onclick event wont get fired and hence i will have to use Objectanimator.
Can somebody please help me out in this ...stuck up for a long time now.
Following is my code i have used however it does not seem to work also i do not understand what parameters(for that points) i need to pass.
ObjectAnimator scaleXOut = ObjectAnimator.ofFloat(tv, "translationX", 1f, 9f);
AnimatorSet set = new AnimatorSet();
set.play(scaleXIn);
set.setDuration(1000);
set.start();
you can use translateAnimation and add an animationlistener so when animation ends its onAnimationEnd() method will fired up, so inside it you can put the codes that was in onclick method, or you can put an if statement inside your onclicked method to check whether animation ends, for more about animations you can check out these resources
http://www.youtube.com/playlist?list=PLxLa2tjhVPOZ9KMqanm12_xPDBjKPZWgM
and
http://www.vogella.com/articles/AndroidAnimation/article.html

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.

HorizontalScrollView, auto-scroll to end with animation

I have a horizontalScrollView and I need to make an auto-scroll to end with animation when I load the view. I have implemented this method to do it:
final HorizontalScrollView strip = (HorizontalScrollView) contentView.
findViewById(R.id.horizontalScrollView1);
strip.postDelayed(new Runnable() {
public void run() {
strip.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 1000L);
It works fine, but, the main problem is the animation of the scrolling is too fast and I need to implement a slower scrolling. Have you any idea?
This is a demo project I created for one of my projects. Its a scroller the scroll automatically and continously. It was made to show a credits screen by continously scrolling through a list of images.
This might help you or give you some idea.
https://github.com/blessenm/SlideshowDemo
Try this:
ObjectAnimator animator=ObjectAnimator.ofInt(buttonHolderScrollView, "scrollX",targetXScroll );
animator.setStartDelay(100);
animator.setDuration(100);
animator.start();

Categories

Resources