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
Related
I want to rotate a simple imageview which has an elevation of 5dp.
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
animRotate.start();
The animation for the above code is smooth if the android:elevation value for the ImageView is not set in the layout file. But when i set the elevation, the animation becomes jerky.
Can someone please suggest a fix?
Maybe the reason is that you create and run animatuion at once. As docs say, it is better first to init your animation
//OnCreate
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
And then when it is time for animation to be fired run it
animRotate.start();
Also, consider reading about what PivotX and PivotY are, it may be useful.
Also, using default interpolator will give strange result for rotating 5 times - i think using simple linear interpolator is much better choice.
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.
I'm currently working on an Android game
I want to create an image flip effect using animation.
How would I do it?
There is a good example of such here
try this:
Android Animation - Flip
or more complex:
http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html
I managed to achieve this using setScaleX (for flip/reflection around the Y axis). Use setScaleY for flip/reflection around the X axis.
final ValueAnimator rotation = ValueAnimator.ofFloat(0, 360);
Then in your onAnimationUpdate function, set scale on X or Y using the cosine function on your range of angles.
view.setScaleX((float)Math.cos(Math.toRadians((Float)valueAnimator.getAnimatedValue())));
Hope this helps
I have a view that is positioned totally off screen and I am trying to animate it onto the screen.
When I call:
view.startAnimation(tA);
nothing happens, tA.initialize and tA.applyTransformation never get called.
If I move the view so that any part of it is visible before I start the animation, then the animation works correctly.
What is preventing a view from being animated when it is positioned off the parent View?
It's my understanding from researching the same problem that Android Animations do not do well when provided with offscreen coordinates for their start or finish.
There is some dialog on the Android forums about this bug having been addressed but I'm still experiencing problems on 4.2.
Edit:
On second thought, I just ran across this answer and it provides a working alternative if you can use the newer APIs (ObjectAnimator).
View view = this;
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "y", 0, 100);
anim.setDuration(super.animationDuration());
anim.start();
Where the properties of ObjectAnimator.ofFloat(view, "y", 0, 100); are
ObjectAnimator.ofFloat(Object objBeingAnimated, String propertyBeingAnimated, float startValue, float endValue)
I found this answer using ValueAnimator to modify the MarginLayoutParams.topMargin (in my case) in onAnimationUpdate(), which fixed the issue. My View starts out with its margin set so that the View is off screen.
The ObjectAnimator approach was promising but did not work for me, it had the same cutoff issue for off-screen views that I got with TranslateAnimation.
I'd like to create an indeterminate animation that simply fades from one color to another (a pulse, if you will). I don't think this should require the use of images but despite my best efforts, I'm not sure I understand how to use something like AlphaAnimation with a Shape to accomplish this.
Could someone please provide some insight as to how to accomplish this? I have a feeling I'm missing something pretty straightforward here. (Examples are always appreciated!)
Thanks!
This is a trivial task in 3.0 - you can set up an ObjectAnimator to change the "color" or "backgroundColor" of an object (View, ColorDrawable, whatever has the property) between two values. See the ApiDemo animations/BouncingBalls for an example of this.
But assuming you're using pre-3.0 APIs, there are a couple of approaches. First, you could set up your own handler to give you the timing events you need, then calculate the new color at each point.
It's probably slightly easier (if not entirely intuitive) to use an AlphaAnimation. All you really want from the animation is percentage values, not to fade anything. So you don't set the animation on a view, but just set it up to run internally from a value of 0 to 1, then get the current animated value in your onDraw() method and set the current color appropriately.
For example, this will set up and start the alpha animation to run for one second:
Transformation transform = new Transformation();
AlphaAnimation anim = new AlphaAnimation(0f, 1f);
anim.setDuration(1000);
anim.start();
Then in your drawing loop, you grab the current animated value:
long time = getDrawingTime();
anim.getTransformation(time, transform);
float elapsedFraction = transform.getAlpha();
Once you have the elapsedFraction (a value between 0 and 1), you can calculate the appropriate in-between color value.
The code above may not match your situation exactly, but you should be able to do something similar to get what you want.