【问题标题】:Move imageview after animation (update position)动画后移动imageview(更新位置)
【发布时间】:2014-06-21 08:20:05
【问题描述】:

我正在尝试在图像视图上从屏幕底部到中间进行平移动画。动画完成后,我希望图像视图留在那里。我不想要 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);

【问题讨论】:

标签: java android imageview android-animation


【解决方案1】:

然后你必须为你的动画视图设置新的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);

【讨论】:

  • 嗯。好建议,我试试看
【解决方案2】:

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】:

如果你想要图像视图的实际起始位置你可以在animationStart的时候得到它你可以使用setFillAfter(true)setFillAfter(true) 将在动画结束后更新位置。

如果您需要新职位,可以使用setFilter(true)。如果您还没有准备好使用setFillAfter(true)(如果您需要较旧的职位),那么您通过拥有两个图像视图来做正确的事情。但最好在animationStart 中获得职位并使用setFillAfter(true)

【讨论】:

  • 请阅读我的问题。我特别说我不想使用它,因为它不会改变位置,它只会改变“宇宙位置”......或者你错过了我的意思
  • 您可以通过此graphics-geek.blogspot.in/2011/08/… 链接获取标志fillAfter、fillBefore 和fillEnabled。我假设将所有三个标志都设置为 true 将适用于您的情况。
【解决方案4】:

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

【讨论】:

  • 请阅读我的问题。我特意说了我不想用它,因为它不改变位置,它只是改变“宇宙位置”
  • 这是关于在动画完成时移动视图(更新位置),所以如果你想再次移动视图,从更新的位置开始,而不是从最初的位置开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2015-05-09
  • 2012-04-20
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多