How to create a ScaleX ObjectAnimator with increasingly fast end? - android

I'm developing a animation which must show a view increasing its size progresively more fast. I want to achieve the same effect that you see when a car is coming. When is far, the coming movement is slow, but the more near is the vehicle, the faster the car is for your vision.
When using ScaleX with ObjectAnimator, you have some possibilities of interpolation:
AccelerateDeccelerateInterpolator (default) -> very slow when the animation is close to finish...
AccelerateInterpolator -> Also slowing when the ScaleX is too much huge.
LinearInterpolator -> Even more slower when the ScaleX is too much huge.
FastOutSlowInInterpolator -> Still slow at the end.
How can the ScaleX animation get an effect of increasing speed of the scale with the time?
My sample code:
scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0.1f, 20f);
scaleX.setDuration(10000);
scaleX.setInterpolator(new AccelerateInterpolator());
scaleX.start();
Thank you

Solved creating a custom cubic bezier curve using this web: https://matthewlein.com/ceaser/
Interpolator customInterpolator = PathInterpolatorCompat.create(cpX1,cpX2,cpY1,cpY2)
the resulting curve is similar to this:
|
|
_____/

Related

How to give a RotationAnimation a steady speed?

In my Android app, I have a fan image whose rotation speed is set by regulator.
This is the animation part:
private fun setFanSpeed(toggle:Boolean, speed:Float){
var speed:Float = speed
if (!toggle)
speed = 0F
val animation = RotateAnimation(0F, 720F,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
)
animation.duration = speedToDuration(speed)*1000
animation.repeatCount = Animation.INFINITE
fan.animation = animation
fan.startAnimation(animation)
}
Unfortunately, the rotating fan feels weird because, instead of rotating at a constant speed i.e 720 rotation in a constant time it accelerates during the start of the animation, and decelerates to a stop during the end. Then speeds up again for the next rotation.
How can I remove the speed up and the speed down, and just make the animation work at a constant speed?
It seems that AccelerateDecelerateInterpolator is the default interpolator for Animation, against the caption in documentation. Specifying LinearInterpolator explicitly, you'll be able to make it rotate uniformly.
animation.interpolator = LinearInterpolator()

Animate image size

I would like to have an image "bump" (or change its size) according to music. I don't need the music detect side, I just need a way to resize the image dymaically and according to an animation. What is the best way to do this?
If you're using at least API 11 and want to animate it smoothly, you can use an ObjectAnimator to animate various changes to the properties of a View. For example:
ObjectAnimator oa = ObjectAnimator.ofFloat(myView, View.SCALE_X, 1.0, 1.25);
oa.setDuration(25); //how long the animation lasts in ms, probably short for your purposes
oa.start();
This will scale the X size of the view myView from the default (1.0) to slightly larger (1.25). You will then need to do the same for the SCALE_Y property. Note that you will probably want to keep track of the current scale value so that your next animations look nice. For example:
float currentScale = 1.0;
...
void scaleMyView(float newScale) {
ObjectAnimator oa = ObjectAnimator.ofFloat(myView, View.SCALE_X, currentScale, newScale);
oa.setDuration(25);
oa.start();
currentScale = newScale;
}
See also the documentation on ObjectAnimators if you need more info. Hope this helps!
Every View has setScaleX and setScaleY methods. I believe you can use it for this. As far as syncing it to the music, that depends on the way you're playing it.
There is greate animation library for Android.
https://github.com/daimajia/AndroidViewAnimations
https://github.com/2359media/EasyAndroidAnimations

Circular scale animation

I'm trying to achieve an animation which scales over a view like this:
At the moment I'm using a scale animation loaded from xml:
This in action looks like this:
It scales more like a square.
I was using ObjectAnimator/ValueAnimator with properties before.
Some devices had specific problems with measuring heights for scaling so I reverted to using xml definitions.
Does anyone have any clue how to achieve the above?
you must define programmatically you ScaleAnimation.
first get radius of your circle(ImageView) at first position(before start Animation).
for get radius you can get width of your imageview and then division to 2.
second you must get width and height of your square.
third do it :
anim_to = (width_square + height_Square) / radius_circle;
finally set "anim_to" to your ScaleAnimation and start animation like this:
ScaleAnimation my_reveal = new ScaleAnimation(1,anim_to, 1,anim_to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
my_reveal.setInterpolator(new DecelerateInterpolator());
my_reveal.setDuration(500);
my_circle.startAnimation(my_reveal);
sorry for my bad ENG ;)

android objectanimator scaleX only in one direction

I am using ObjectAnimator and cant figure out how to scale in x axe only in one direction for example to right side. Because when im scaling it scales both ways and then my ImageView is out of screen. I know i could add transitionX to prevent going off-screen but a better solution would be scaling in one direction.
ObjectAnimator headAnim = ObjectAnimator.ofFloat(imageHead, "scaleX", 1f, 1.5f);
headAnim.setDuration(1000);
headAnim.start();
Just set the pivot point of the View, using View.setPivotX() and View.setPivotY(). Set it to 0,0 to achieve the effect you want.
The param of View.setPivotX/Y is a float value. It is the absolute value of width or height, not the percent.

Rotate image on center not going smooth (Monodroid)

I have an Image (which looks like a round loading circle) that I want to rotate around it's own center.. I saw a lot of code which suggested to set the PivotY and PivotX to 0.5F or th half of the image. Both does not work.. After a lot of trial and error it does rotate around it's own center with the following code:
ImageView loading = FindViewById<ImageView>(Resource.Id.loadingGif);
RotateAnimation rAnim = new RotateAnimation(0.0F, 359.0F, Dimension.RelativeToSelf, 0.25F, Dimension.RelativeToSelf, 0.25F);
rAnim.Interpolator = new LinearInterpolator();
rAnim.RepeatCount = Animation.Infinite;
rAnim.Duration = 1000;
loading.StartAnimation(rAnim);
But the animation itself is not going smooth anymore, the image seems to rotate about 50degrees then starts to hang and skips half of the rotation and then continues normally on the top half of the rotation (I hope that makes any sense).
Any idea why my rotation does not rotate the full 360 degrees smoothly?
EDIT
I still haven't solved this problem, but I did just discover something peculiar about it. The animation is used on a loading screen with no extra functionalities on the moment of the animation. I noticed that when I keep my finger on the screen the animation DOES happen smoothly!!
This got me thinking that the issue maybe isn't in the animation code but something else.. but what I don't know yet..
I've finally found the answer!
I've included the line:
loading.setDrawingCacheEnabled(true);
And after that had to change the pivots to 0.5F too. And now the animation runs smoothly!
//Rotate image
ImageView loading = FindViewById<ImageView>(Resource.Id.loadingGif);
loading.setDrawingCacheEnabled(true);
rAnim = new RotateAnimation(0.0F, 359.0F, Dimension.RelativeToSelf, 0.5F, Dimension.RelativeToSelf, 0.5F);
rAnim.Interpolator = new LinearInterpolator();
rAnim.RepeatCount = Animation.Infinite;
rAnim.Duration = 1500;
loading.StartAnimation(rAnim);

Categories

Resources