Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

java - Move imageview after animation (update position)

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...