2

I am trying to to do a translate animation on an image view from the bottom to the middle of the screen. Upon finish of the animation, I want the image view to stay there. I dont want the setFillAfter(true) because I want the actual position of the imageview to be updated.

I do it currently by having 2 image view (one at the start of animation and one at the end) and I play with the setVisibility to achieve this. Is this the correct way to do things? Here is the code I used:

<ImageView
    android:id="@+id/ivStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/typer_step_1"
    android:gravity="center"
     />



<ImageView
    android:id="@+id/ivMiddle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:background="@drawable/typer_step_1"
    android:gravity="center"
    android:visibility="invisible"
     />    








     TranslateAnimation translate = new TranslateAnimation(0, mDestLoc1[0]-mSrcLoc1[0], 0, mDestLoc1[1]-mSrcLoc1[1]);                   
translate.setDuration(2000);
translate.setAnimationListener(new AnimationListener(){

    @Override
    public void onAnimationStart(Animation animation) {}

    @Override
    public void onAnimationEnd(Animation animation) {
        ivMiddle.setVisibility(View.VISIBLE)
                        ivStart.setVisibility(View.INVISIBLE)


    }

    @Override
    public void onAnimationRepeat(Animation animation) {}

});

ivStart.startAnimation(translate);
l-l
  • 3,804
  • 6
  • 36
  • 42
Snake
  • 14,228
  • 27
  • 117
  • 250
  • possible duplicate of [Android translate animation - permanently move View to new position using AnimationListener](http://stackoverflow.com/questions/19016674/android-translate-animation-permanently-move-view-to-new-position-using-animat) – JiTHiN May 05 '14 at 05:50
  • 1
    in your xml layout, you should instead place the ImageView you're animating on the position you want it to go, before the animation. On starting the animation, translate it such that it looks like it's coming from where you want it to come from. – josephus May 05 '14 at 05:52
  • @JosephusVillarey hmmm , good suggetion! – Snake May 05 '14 at 06:26

4 Answers4

2

Then you must set new LayoutParams for your View which is animating. When animation finishes, in your onAnimationEnd part, set new position of your View.

     TranslateAnimation translate = new TranslateAnimation(0, mDestLoc1[0]-mSrcLoc1[0], 0, mDestLoc1[1]-mSrcLoc1[1]);                   
     translate.setDuration(2000);
     translate.setAnimationListener(new AnimationListener(){

         @Override
         public void onAnimationStart(Animation animation) {}

         @Override
         public void onAnimationEnd(Animation animation) {
             RelativeLayout.LayoutParams par = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
             par.topMargin = mDestLoc1[1]-mSrcLoc1[1];
             par.leftMargin = mDestLoc1[0]-mSrcLoc1[0];
             view.setLayoutParams(par);              
         }

         @Override
         public void onAnimationRepeat(Animation animation) {}

     });

     view.startAnimation(translate);
canova
  • 3,965
  • 2
  • 22
  • 39
0

If you want the actual starting position of the image view you can get it at the time of animationStart And you can use setFillAfter(true). The setFillAfter(true) will update the position after the animation end.

If you need the new position you can use the setFilter(true). If you are not ready to use setFillAfter(true) (if you need the older position), then you have done the right thing by having two Image Views. But its better to get the position in animationStart and use setFillAfter(true).

Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Sripathi
  • 1,760
  • 15
  • 20
  • 1
    Please read my question. I said specifically I dont want to use it because it does not change the position, it just changes "the cosmatic location"... or you are missing my point – Snake May 05 '14 at 06:28
  • You can follow this http://graphics-geek.blogspot.in/2011/08/mysterious-behavior-of-fillbefore.html link to get the flags fillAfter, fillBefore and fillEnabled. Setting all the three flags to true will be working in your case I assumed. – Sripathi May 05 '14 at 07:16
0

Snake use my code this ll help you,

//Call this in your onCreate

 private void StartAnimationsDtoU() 
{
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alphadtou);
    anim.reset();
    RelativeLayout l=(RelativeLayout) findViewById(R.id.lin_lay);
    l.clearAnimation();
    l.startAnimation(anim);
    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.logo);
    iv.setImageResource(R.drawable.earth);
    iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    iv.clearAnimation();
    iv.startAnimation(anim);

}

This is your linear layout with an image which ll move from down to middle of the screen.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lin_lay">

<ImageView
    android:id="@+id/logo"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/earth" />

and this ll be your xml file for translation i.e translate.xml ...

<set xmlns:android="http://schemas.android.com/apk/res/android"><translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="200%"
android:toYDelta="0%"
android:duration="4000"
android:zAdjustment="top" /></set>

and this ll be your down to up alphadtou.xml...

<?xml version="1.0" encoding="utf-8"?><alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000" />

and also override onAttachedToWindow method like this...

@Override
public void onAttachedToWindow()
{
    // TODO Auto-generated method stub
    super.onAttachedToWindow();
    Window window = getWindow();
    window.setFormat(PixelFormat.RGBA_8888);
}

Hope this ll help you alot.

Rohit Goswami
  • 617
  • 5
  • 17
  • Thank you but I dont see the code that changes the position permanently of image view. This will just translate the image – Snake May 05 '14 at 17:39
-1

Add this line before starting the animation translate.setFillAfter(true);

upenpat
  • 685
  • 3
  • 12
  • 1
    Please read my question. I said specifically I dont want to use it because it does not change the position, it just changes "the cosmatic location" – Snake May 05 '14 at 06:27
  • It's about moving the view (with updating the position) when the animation is finished, so if you want to move the view again, to start from the updated position, not the initial one. – sunlover3 Jan 05 '17 at 08:40