动画后移动imageview(更新位置)

时间:2014-05-05 05:44:20

标签: java android imageview android-animation

我正试图在屏幕底部到中间的图像视图上进行翻译动画。完成动画后,我希望图像视图保持不变。我不想要setFillAfter(true),因为我希望更新imageview的实际位置。

我目前通过2个图像视图(一个在动画开始,一个在结尾)来做,我玩setVisibility来实现这一点。这是正确的做事方式吗?这是我使用的代码:

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

4 个答案:

答案 0 :(得分:2)

然后,您必须为您的View设置新的LayoutParams动画。动画完成后,在onAnimationEnd部分中设置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);

答案 1 :(得分:0)

如果您想要图像视图的实际起始位置,则可以在animationStart时获取它并且您可以使用setFillAfter(true)setFillAfter(true)将在动画结束后更新位置。

如果您需要新职位,可以使用setFilter(true)。如果您还没准备好使用setFillAfter(true)(如果您需要较旧的位置),那么您已经通过两个图像视图做了正确的事情。但最好是在animationStart中找到位置并使用setFillAfter(true)

答案 2 :(得分:0)

Snake使用我的代码,这将帮助你,

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

}

这是您的线性布局,其中的图像将从屏幕的下方移动到中间。

<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" />

这将是你的xml翻译文件,即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>

这将是你的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" />

并覆盖onAttachedToWindow方法,就像这样...

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

希望这会帮助你。

答案 3 :(得分:-1)

在开始动画之前添加此行 translate.setFillAfter(true);