如何在Android中使用平移动画来实现视图动画效果

44

我在我的应用程序中有一个ImageView,它可以放置在屏幕的任何位置

触摸时,我想将此视图移动到屏幕的中心。我尝试使用Translate Animation并使用其RELATIVE_TO_PARENT功能实现此功能,代码如下:

TranslateAnimation translateAnimation1 = new TranslateAnimation(
      TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
      TranslateAnimation.RELATIVE_TO_PARENT,0.5f,
      TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
      TranslateAnimation.RELATIVE_TO_PARENT,0.5f);

但 ImageView 会在当前位置下移50%(屏幕的高度),有没有办法将该视图移动到屏幕中央,而不管它目前所处的位置?

1个回答

73

为了将一个视图(View)移动到屏幕上的任何位置,我建议将其放置在全屏布局中。这样做,您就不必担心剪辑或相对坐标。

您可以尝试此示例代码:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:id="@+id/rootLayout">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MOVE" android:layout_centerHorizontal="true"/>

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" android:layout_marginLeft="10dip"/>
    <ImageView
        android:id="@+id/img2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" android:layout_centerVertical="true" android:layout_alignParentRight="true"/>
    <ImageView
        android:id="@+id/img3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" android:layout_marginLeft="60dip" android:layout_alignParentBottom="true" android:layout_marginBottom="100dip"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" android:clipChildren="false" android:clipToPadding="false">

        <ImageView
            android:id="@+id/img4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" android:layout_marginLeft="60dip" android:layout_marginTop="150dip"/>
    </LinearLayout>

</RelativeLayout>

你的活动

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((Button) findViewById( R.id.btn1 )).setOnClickListener( new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            ImageView img = (ImageView) findViewById( R.id.img1 );              
            moveViewToScreenCenter( img );
            img = (ImageView) findViewById( R.id.img2 );
            moveViewToScreenCenter( img );
            img = (ImageView) findViewById( R.id.img3 );                
            moveViewToScreenCenter( img );
            img = (ImageView) findViewById( R.id.img4 );
            moveViewToScreenCenter( img );
        }
    });
}

private void moveViewToScreenCenter( View view )
{
    RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout );
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics( dm );
    int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();

    int originalPos[] = new int[2];
    view.getLocationOnScreen( originalPos );

    int xDest = dm.widthPixels/2;
    xDest -= (view.getMeasuredWidth()/2);
    int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;

    TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
    anim.setDuration(1000);
    anim.setFillAfter( true );
    view.startAnimation(anim);
}

moveViewToScreenCenter方法获取视图的绝对坐标,并计算从当前位置到达屏幕中心需要移动多少距离。而statusBarOffset变量则测量了状态栏的高度。

希望您可以继续使用这个例子。请记住,在动画结束后,视图的位置仍然是初始位置。如果您不断点击“MOVE”按钮,则会重复相同的移动。如果想要改变视图的位置,请在动画完成后进行。


我曾经使用过moveViewToScreenCenter,但现在将其更改为将视图移动到屏幕右上角。当从按钮启动时,它可以正常工作,就像你的示例一样;但是当从onCreate中启动moveViewToScreenCenter时,视图会朝完全不同的方向移动。似乎在onCreate中,View的originalPos始终为[0,0]。这是怎么回事? - Jonny
5
onCreate方法中,视图的尺寸为0,因为Activity对用户不可见。尝试将调用放置在onWindowFocusChanged方法中。 - Xavi Gil
它对我没有起作用,我的视图向下移动了! - S.M_Emamian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接