Note that there are some explanatory texts on larger screens.

plurals
  1. POTop to bottom - translate animation
    text
    copied!<p>Need to make next animation (on android 2.2 and above):</p> <p>1.Moving button from top to bottom (after clicking on him), </p> <p>2.Moving back from bottom to top (After clicking on him again).</p> <p>First animation works fine, but the second not, the btn "jumps" from bottom to top and not animate.</p> <p>Code:</p> <pre><code>public class MainActivity extends Activity { static RelativeLayout relativeLayout; static Button btn; static Boolean isUp = true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button1); relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(isUp){ isUp = false; v.startAnimation(MainActivity.getVerticalSlideAnimation(0,relativeLayout.getBottom() - v.getHeight(),500,0)); }else{ isUp = true; v.startAnimation(MainActivity.getVerticalSlideAnimation(relativeLayout.getBottom() - v.getHeight(),0,500,0)); } } }); } public static Animation getVerticalSlideAnimation(int fromYPosition, final int toYPosition, int duration, int startOffset) { TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 0, fromYPosition, 0, toYPosition); translateAnimation.setDuration(duration); translateAnimation.setInterpolator(new AccelerateInterpolator()); translateAnimation.setStartOffset(startOffset); //Stop animation after finishing. //translateAnimation.setFillAfter(true); translateAnimation.setAnimationListener(new AnimationListener() { public void onAnimationStart(Animation animation) { } public void onAnimationRepeat(Animation animation) { } public void onAnimationEnd(Animation animation) { btn.setY(toYPosition); } }); return translateAnimation; } } </code></pre> <p>Layout:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relative_layout" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Button" /&gt; &lt;/RelativeLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload