I am using objectAnimator for animating a button from bottom to top in Android. Now i am using the below code
ObjectAnimator transAnimation = ObjectAnimator.ofFloat(button,"translationY",0,440); transAnimation.setDuration(440); transAnimation.start();
I have also tried with sample code below. But still the problem exists
ObjectAnimator transAnimation = ObjectAnimator.ofFloat(loginLayout, "translationY",0f,0.8f); transAnimation.setDuration(480); transAnimation.start();
It works fine in large screen devices. But when it comes to small screen devices it goes off the screen. I want to stay it in the top of the screen irrespective of different screen sizes. I think i have to give values in percentage (say 0% to 100% or 0 to 100%p). So my question is how to give values in percentage in objectAnimator in Android. I also noticed one thing that this objectAnimator is introduced only in HoneyComb. So is there any backward compatibility libraries for running it in low versions. Could anyone guide me to find a solution for this.
I also tried extending View and writing getter and setter for the property in offset()
. Still it does not move fully to top of screen. Here is the code I have used.
@SuppressLint("NewApi") public float getXFraction() { int width = context.getWindowManager().getDefaultDisplay().getHeight(); return (width == 0) ? 0 : (getY()); } @SuppressLint("NewApi") public void setXFraction(float xFraction) { int width = context.getWindowManager().getDefaultDisplay().getWidth(); setY((width > 0) ? (width) : 0); }
Thanks inAdvance
Animations not based on physics—such as those built with ObjectAnimator APIs—are fairly static and have a fixed duration. If the target value changes, you need to cancel the animation at the time of target value change, re-configure the animation with a new value as the new start value, and add the new target value.
A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.
There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .
To use ObjectAnimator
on pre-Honeycomb devices add NineOldAndroids library to your project and change your imports to use com.nineoldandroids.animation.ObjectAnimator
.
To use percentage values in ObjectAnimator
instead of getXFraction
and setXFraction
you have to add getYFraction
and setYFraction
methods like this
public float getYFraction() { final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int height = wm.getDefaultDisplay().getHeight(); return (height == 0) ? 0 : getY() / (float) height; } public void setYFraction(float yFraction) { final WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int height = wm.getDefaultDisplay().getHeight(); setY((height > 0) ? (yFraction * height) : 0); }
And then you can create xml file project-folder/res/animator/move_bottom.xml
like this
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:propertyName="yFraction" android:valueFrom="0" android:valueTo="0.8" android:valueType="floatType" />
Or create animation in code
final LoginLayout loginLayout = (LoginLayout) findViewById(R.id.login_layout); final ObjectAnimator oa = ObjectAnimator.ofFloat(loginLayout, "yFraction", 0f, 0.8f); oa.start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With