2

I am new to Animation in Android and searched a lot about Move up and Down an ImageView, but there are lot tuts about move left to right. I can't find a tut to move up and Down. Can please anyone tell me how can I achieve this? I am adding some code here, that I have learned from YouTube to move an image left to right(imga is ImageView) -

 Animation img = new TranslateAnimation(Animation.ABSOLUTE, 150, Animation.ABSOLUTE, Animation.ABSOLUTE);
            img.setDuration(3000);
            img.setFillAfter(true);

            imga.startAnimation(img);
  • What is the difference with up and down. Please specify your question. – julienduchow Mar 24 '18 at 11:28
  • So, @julien-100000, now I want to make an app that has an image(ball). What I am trying to do is when we click on that image, the ball will go up. and after that when we click again, it will revert back. – Deb Prakash Chatterjee Mar 24 '18 at 16:52

2 Answers2

6

Use this xml for animation anim/up_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >


<!-- Move -->

<translate
    android:duration="800"
    android:fillAfter="true"
    android:fromYDelta="0%p"
    android:toYDelta="70%p" />

<translate
    android:duration="800"
    android:fillAfter="true"
    android:fromYDelta="0%p"
    android:startOffset="800"
    android:toYDelta="-70%p" />


</set>

In Java Class:

// Animation
Animation animUpDown;

 // load the animation
animUpDown = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.up_dwon);

  // start the animation
  view.startAnimation(animUpDown);
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
3

You need to know the logic behind movement of image or object that will animate in the axis from x to x delta or from y to y delta where

  • Values in x=n variable to x= -n variable depict that image will move from right to left delta
  • Values in x= -n variable to x=n variable depict that image will move
    from left to right delta
  • Values in y=n variable to y=-n variable depict that image will move from top to bottom delta
  • Values in y=-n variable to y=n variable depict that image will move from bottom to top delta

Below image will take you to the 10 standard Mathematics movement graph for the image

Note: Here n is considered the DP or point of the vertices.

krishank Tripathi
  • 616
  • 1
  • 7
  • 23