WebView.scrollTo animation - android

I'm playing with webviews in Android and got stuck on the idea of making animation for scrollTo method. Basically the method scrollTo(x,y) scrolls the page to the given point, but it does it instantly.
I would like it to be slower, like an animation transition. Anyone knows how to do it?
Edit 1:
I just tried with xml and code based Translate animations. It seems to move the webview, but only as a "whole" view, so it scrolls down, but not the webview's scrollbar but rather the whole viewport of the webview (does it make sense?) So effectively I can see my current page sliding down with a black screen sliding after it (from top do the bottom). It kind of makes sense, because I'm translating the whole webview (move it down...). I've tried to set Animation.ABSOLUTE, Animation.RELATIVE_TO_PARENT and Animation.RELATIVE_TO_SELF respectively, with no success.
How can I then animate the position of the scroll directly in the webview rather then the whole view?
Edit 2:
After several more tries, I understand more how the animations work. I therefore claim that it's not possible to override default "instant" scrollTo behaviour. If anyone has a different opinion or knowledge about that, let me know.

Use ObjectAnimators. This code smoothly scrolls the WebView to the top:
ObjectAnimator anim = ObjectAnimator.ofInt(mWebView, "scrollY",
mWebView.getScrollPositionY(), 0);
anim.setDuration(400);
anim.start();

I think you need to first set animation to the web view and if you want to use it immediately then you need to say start animation. Use xml to define the Animations.
I have seen an answer related to this question, check this link

Related

How to implement animations/transitions in Android

I have a specific goal regarding animation using Recyclerview, but I cant find an exact title to convey my concern, so I'm open to any correction if necessary. So first , I have a recyclerview that acts as a ViewPager thanks to David Medenjak's Recyclerview ViewPager
Now I have a requirement to animate the background color for each swipe transition, thanks to different links and resources, I stumbled upon TransitionDrawables, putting an array of different drawables and invoking a necessary transition functions to animate,
recyclerView.setBackground(arrayOfDrawables)
transitionArrayOfDrawables.startTransition(...);
Now I want to take things deeper, I want to manually control the transition relative to the animation of the swipe of each item of the recyclerview, Im mentally blocked as I cant find the keyword to search for where should I start, "Interpolation", "Manually interpolating a views animation", things like that, but still no luck, the only thing that I have on my mind is "Get the recyclerview's visible item's interpolation/animation-value then supply it to the target view's animation manually", but I dont know where to start, I dont even know how to animate view's manually, I have experience with ObjectAnimators, simple translating, scaling, bouncing etc.. but controlling view's animation manually and relatively even by using knob/seek bar or such, its just out of my domain as of the moment.
Any help would be greatly appreciated.
[Edit] Apologies if my question's intent covers alot of demands :(
If I am understanding correctly, this link may be of some use. It provides instruction for how to gradually transition between viewpager background colors in relation to the user swipe.
The example uses the ArgbEvaluator class which -according to the doc: "...perform[s] type interpolation between integer values that represent ARGB colors."

Android Animation - Button stays clickable

I am making a game in which I have 5 buttons, looking like clouds, falling from the "sky".
That means that when my activity starts, 'clouds' cannot be seen, since the marginTop is set to -100dp.
From that position they start falling down untill they get lost on the bottom side of the screen.
The thing is, I need those buttons to be clickable, during the process of animation.
So far, I found some documentation about how I can make the buttons clickable AFTER the animation ends. But I don't need that. I need to be able to click on the buttons through the animation time itself.
NOTE: I need something that works with versions before 3.0.
Anybody has any link to documentation or some example or anything ?
After doing some research, I found out that there are two types of animations:
View Animation and Property Animation.
The view animation can only animate View objects. It also lack a variety of animations, since it can do only stuff as scale, rotate, move... It cannot change background color, for example.
Also, the disadvantage of the View Animation is that it only change the position of where the View object is DRAWN. Physically, it still stays in the same position.
That's why the button is un-clickable, after the View Animation is finished upon it.
Property Animation, in the other hand, can animate both View and non-View objects and it doesn't have constraints as the View Animation.
When objects are moved, for example, with the property animation, they are not just drawn on some other position on the screen, but they are actually MOVED there.
Now, Property Animation is a lot more complex to write than the View Animation, so if you don't really need all the advantages of the Property Animation, it is suggested to use View Animation.
Source:
Property vs View Animation
Tutorial and SupportLybrary up to API 1:
nineoldandroids
You can change the buttons to imageViews and then do
imageView.setOnClickListener(myListener)
then set myListener to do whatever you previously wanted to happen on the buttons onClick. Your activity will have to implement OnClickListener
Added bonus: you can make the images look like clouds :)

Vertical smooth scrolling of values in a text view

I have a text view where in i have to keep ages between 1-99. I also have two buttons ^ and v(i mean up and down arrows) on clicking them the values of the age should smoothly scroll to next or previous value.
I have been trying different ways but couldnt achieve smooth scrolling. Can anyone please give me any idea of how to achieve the task.
I think the easiest way is to simply put the TextView within a ScrollView, and let the arrows interact with the ScrollView, by using things like the fling method and playing around with the velocity parameter to suit your needs.
Use the animation framework.
When pressing down, start the 'down'-animation.
When pressing up, start the 'up'-animation.
Read more about animation here: http://developerlife.com/tutorials/?p=343
View animation is not much matured and hence i am noy sure if that can be used for moving the views.
Please find the description below:
Another disadvantage of the view
animation system is that it only
modified where the View was drawn, and
not the actual View itself. For
instance, if you animated a button to
move across the screen, the button
draws correctly, but the actual
location where you can click the
button does not change, so you have to
implement your own logic to handle
this.
Source
To scroll smoothly you can try using the scroller component.
Reference
What you would need to do is pass the duration of the scroll in the constructor and then use the property
setFinalY(int newY)
to increment the counter position by 1 unit (equal to the height of the item).
Please let me know if that helps!

Android: Detect when ScrollView has finished scrolling and bouncing back?

I have a ScrollView which has two hidden images, one at the top and one at the bottom. In between there is a bunch of visible content.
What I need to do is make these images hidden by default but when you scroll all the way up or all the way down you could see them as you're scrolling. But then as soon as you stop scrolling it should bounce back to the visible area so that the hidden images aren't showing.
Basically I'm trying to imitate the bounce scrolling feature of the iphone UIScrollView.
I have my ScrollView all setup and I do a scroll at the beginning so as to hide the top hidden image. Now all I need to do is detect when a scrolling has ended, figure out the Y position, and check whether a hidden image is shown. If it is, I would just programmatically scroll the view back so that the hidden image is hidden.
I hope all that made sense.
So anyways, I know how to programmatically scroll a ScrollView. Now what I need is some sort of callback to tell me when a ScrollView ended scrolling and also a way to get the ScrollView's current 'Y' position. Are there any such methods I could use?
I looked through the ScrollView docs but nothing jumped out at me. I'm still not very familiar with the Android naming schemes so maybe I missed something obvious somewhere.
Anyways, any help would be appreciated here. Cheers.
You can use an OnTouchListener to detect when the user presses/releases the list.
You can also use the onScrollStateChanged method of the OnScrollListener class (most likely in conjunction with a touch listener) to detect changes in the SCROLL_STATE - when the list has stopped scrolling the state will change from a state that is not SCROLL_STATE_IDLE to SCROLL_STATE_IDLE.
Alternatively if you are using 2.3 or above you can use an OverScroller to get the desired effect (see Modifying Android OverScroll for how to change the over scroll effect to an iPhone like one).

ViewFlipper caching issue

The views are not cached in a ViewFlipper. Is there a way wherein we can get an image of the view and show it to user so that he sees the Ui as we see on Home scrren(when we swipe the previous view also moves along and when we lift our finger, only then the next view is shown completely.)
What I want to do is that when the user starts moving his finegr on screen, the view should also move along(create an image of view).
I am not getting to do this, as when we swipe the present view goes and next view comes, we do not get both visible when we r moving our finger on screen.
Please if anyone gets what I am trying to do, do help me.
Thanks,
Farha
It's tricky to get scroll and swipe tracking working on Android, while using ViewAnimator or its subclasses.
They allow you to set in and out animations and start them at a given moment, but they work with discrete, either-this-or-the-other-view animations. They are actually using FrameLayout and after in or out animation is executed, other views' visibility is set to View.GONE to hide them from showing up under/over your current View.
The Launcher and the Gallery application are actually doing the functionality you want, by using a different approach.
They track the user touch input (onTouchEvent()), on MotionEvent.ACTION_MOVE they perform animations manually and on MotionEvent.ACTION_UP snap to the appropriate view, just like in the iPhone.
Unfortunately, this approach is actually more complicated than it looks like.
With the manual handling, you have to ensure that you are taking care of everything related to the touch input. This includes a lot of flag-raising, value-checking, event-delegating, etc.
If you want to get better acquainted with this, take a look at these classes from Gallery3D or Launcher's source code.
One other way to get nice horizontal scrolling is to use HorizontalScrollView.
You have to figure out a way to recycle your views, like you would with a ListView and you have to add the snap-to-view logic, but if you have to take care of a small number of views it could be the easiest approach.
Hope that helps.

Categories

Resources