I have a view. I want to change its alpha from 1 to 0 and then repeat for a number of time.
I believe I have to use animation for that. But I don't know how to use the CycleInterpolator in android. Might anyone show me a code sample? I have gotten as far as
CycleInterpolator ci = new CycleInterpolator(5f);
ViewPropertyAnimator anim = myView.animate();
anim.alpha(0);
anim.setDuration(10000);
anim.setInterpolator(ci);
EDIT
the code above works, but it is very brisk. All I see is blinking. I want the view to fade out and then back in very slowly about once every second or two or three. I really need the transition to be smooth and slow and visible to the human eye.
Have you tried
myview.animate().alpha(0).setInterpolator(new CycleInterpolator(<Number of cycles>))
You can also find all of the different methods of a ViewPropertyAnimator here.
Related
In this mock up, see the screen where it said "Decrease Traffic", the traffic light pops up from below.
https://dribbble.com/shots/2106573-Intro-App-Interaction
I'm wondering how to create that effect on Android, if possible, any library to do that. Thanks
you can achieve this with Android Property Animations
in your example it seems to be a scale animation from 0 to 1 in x and y direction.
you can also combine different animations to get such or other effects.
My guess is that these kind of animation are to develop with a 3rd party program, and are not done by Android itself.
This way you have a ready-to-instantiate file (.gif maybe?) that you just call when the view pager is switched.
Looking also to the other animations in the example (look # "Meet New Friends", how every circle pops by himself) I think this was the used approach in your example-link.
If you are anyway looking for something that would look like what is done there, I'd suggest you start playing around Animator objects (and its subclasses like Value/ObjectAnimator) to animate the view you want.
This can be done also via XML files, describing the animation you need.
An important role in this do-it-yourself animation is played by Interpolators , who give you the chance to further customize the animation in an easier way (take a look at OvershootInterpolator, which lets your animation go a bit after the destination value you set, just to turn back later, giving you a "blob" effect. This might give a better idea what I'm meaning)
I'm tackling the task of an overlaying drawable over a view that animates the drawing of a checkmark as in the following video https://vid.me/MsQj
I don't have a preferred method for doing this but it's just not coming out the way I wanted it to, I tried:
Two views, each with on side of the checkmark to be revealed with an animation, however I'm stuck at the "revealed with an animation" since I can't use the circular reveal on -21
Frame by frame animation, this is the easiest but I'd hate to have 60 images for this stupid animation if it can be done programmatically
Drawing on a custom view canvas
My question would be, is there anything that can make this easier on me, or do I have to tackle it head first and just get on with it
You could create a custom View class which contains two lines defined by ShapeDrawables, one for each leg of the tick. Expose the lengths of these two lines as properties of the class, and then use Property Animation to animate the lengths of the lines.
Property Animation is flexible enough to handle pretty complex timing and sequencing of various properties. In this particular case you would probably want to use an AnimatorSet to sequence the two line animations so the second starts once the first has finished.
I ended up developing a custom View thanks to #SoundConception suggestion and finding out about ObjectAnimator which are very powerful in Android. In essence what goes on is we set a width for the first and second line that make the checkmark and using the animator change the value of those properties from 0 to the desired one.
On the setter for the property, we invalidate the View to redraw it with the new value and with a little tweaking I made a nice View that while its currently only working for my specific layout (ie it needs some more work on the offset calculation) it's able to draw an animated checkmark with some stuff that is customizable.
Precisely, you can set the line width, the color, the length and the animation time. And touching the java file, you can change the interpolator and all the rest of the stuff.
Hopefully the code, while not really commented serves as a basis for someone trying something similar.
For example the following code would generate something like this video, although not really because I was testing opacity and thinner lines, but you get my drift.
<coop.devtopia.CheckmarkView
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:id="#+id/view"
app:first_leg_length="50"
app:second_leg_length="100"
app:total_duration="1500"
app:stroke_width="20"
app:stroke_color="#22000000"/>
Repository
Update 4/2/15
I've played with this a little further and added dynamic offset calculation (fancy way of saying centering) to the tick, meaning we can generate big checkmarks, small checkmarks, skinny or thick, reversed or straight and they will be centered in the view. Can't guarantee the same for checkmarks bigger than the container, they will likely be cropped.
Here are a few of the checkmarks generated for this demonstration, of course the animate as if drawn and the effect can be very pleasing and resource friendly. This turn out to be a pretty interesting subject after all.
I am working on a game for the Android platform. The layout consist elements which should move without stopping from top to bottom. I decided to use an Handler and I set handler.postDelayed(this, 10).
the animation "step" is 5px (It's actually not px but doesn't matter for the discussion).
The thing is, the animation frequently twitching and isn't smooth. With every call of the handler, I make some simple checks and use setX(), setY() to change the elements position.
What should I do to make the animation smooth? Should I tweak the numbers mention above? Also, I've understood the method setWillNotDraw(boolean b) might be useful - How to use it wisely?
I'd be glad if you could guide me what should I do (Kind of "Do and Don't").
Thank you!
As pskink suggests ViewPropertyAnimator is much friendlier than animating Views yourself. If you want/need to manage it manually, consider View.postOnAnimation instead of Handler.postDelayed to run your update with new animation frames.
Regardless of how you move objects, be wary of how many views you have on the screen. Moving one view can force the entire view hierarchy to redo its layout -- this can definitely kill animation speeds. Try animating a single view in an otherwise empty layout.
Profiling is very helpful for tracking down performance issues.
I have several ImageViews each with an indefinitely throbbing ScaleAnimation and three attempts that don't make sense to me.
If I set the animation immediately after inflating the animation the animation runs perfectly. This also tells me my helper function to create the animation (used in all three cases) is working fine.
If I set the animation immediately, but with a start time of Long.MAX, and later call icon.getAnimation().start(), that animation starts but the speed is faster than it should be and the scale is not what I set. No matter what I set the animation scale and duration parameters to, it always looks the same, unlike the previous case.
If I call icon.startAnimation(..) with a new Animation object, I get no animation. Digging into Android source, startAnimation(..) calls invalidate(), unlike setAnimation(..). If I call icon.getAnimation().start() followed by icon.invalidate() I get the same (lack of) behavior. This hints to me that the invalidate somehow breaks the animation.
So, given all of the above, is this symptomatic of a mistake I might have made? Is there something I didn't describe that I should be doing?
Finally, I've tried this with both a scale pivot specified both relatively and aboslutely, thinking there might be a bug in event order with respect to View.invalidate() and Animation.reset().
A lot of people are having problems with animation. Working with animation on anything before Honeycomb is a real pain.
I recommend downloading the NineOldAndroids library and using it as imports, that way you can use the Honeycomb animation API on all versions of android (even up to 1.0).
I'm working on a game that in some ways is similar to Tetris (imagine a 2D array of colored squares that sometimes move around)
I am trying to animate the individual squares so they will smoothly slide down from coordinate to the next. Since I wanted to use Android's built-in tweening feature, the animation has to apply to the whole View (rather than parts of it). This doesn't work well for me because I only want some of the colored squares to slide down, and the rest of them to stay still.
The (theoretical) solution I came up with to resolve this is to make 2 Views, layered directly on top of each other. The top view is for animating squares when they need to move, and the bottom layer is for the static squares. The animation-layer is transparent until I am ready to animate something. I then simply turn on the colored square in the animation-layer, tween it to the new location, and turn it back off when done. In the same time span, the static-layer just turns squares on and off at the right time to make the whole thing look seamless to the end user.
The proposed solution is just a theory, since I haven't been able to make it work correctly yet. Since I have been having trouble, I was wondering if this is even the best way to solve the problem? Perhaps there is a more elegant solution that I am over looking? Anyone know of a better way?
If you just want to animate a single element check out the namespace android.view.animation.Animation. You can also use Drawable shapes and draw them directly. Finally, if you want a simulation then you will have to look into threading. Basically you will create a timer to update the canvas for you based on an interval. There are some other view canvases you can use as well like the GLView canvas.