Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ui element animation

I'm trying to animate UI elements. I would like to move an editText and a Button from the middle to the top of the screen and display results of an http call below them in a table. It would be great if anyone could point me in the right direction, at this point I don't know wether I should use Java or XML for this.

Thanks in advance.

like image 579
Bart Avatar asked Feb 06 '12 11:02

Bart


2 Answers

Use Translation framework to achieve this, this works as:

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

So you need to write your code for moving view in y-axis direction, as follows:

 mAnimation = new TranslateAnimation(0, 0, 0, 599);
    mAnimation.setDuration(10000);
    mAnimation.setFillAfter(true);
    mAnimation.setRepeatCount(-1);
    mAnimation.setRepeatMode(Animation.REVERSE);
    view.setAnimation(mAnimation);

Here view may be anything, textview, imageView etc.

like image 63
jeet Avatar answered Sep 21 '22 14:09

jeet


accepted answer caused an error inside my code, code snippet below almost identical to accepted answer & worked without causing errors, to slide an object off the screen. i needed gestures tied to keyPad to also 'slide away' , and switched from TranslateAnimation to ObjectAnimator (second block of code below).

final LinearLayout keyPad = (LinearLayout)findViewById(R.id.keyPad);

moveKeyPadUp(keyPad);

private void moveKeyPadUp(LinearLayout keyPad){
        Animation animation = new TranslateAnimation(0,0,0,-500);
        animation.setDuration(1000);
        animation.setFillAfter(true);
        keyPad.startAnimation(animation);
}

private void moveKeyPadUpdated(LinearLayout keyPad){
        ObjectAnimator mover = ObjectAnimator.ofFloat(keyPad,"translationY",0,-500);
        mover.setDuration(300);
        mover.start();
}
like image 35
tmr Avatar answered Sep 20 '22 14:09

tmr