简体   繁体   English

如何在Android中使用翻译动画为视图设置动画

[英]How to animate a View with Translate Animation in Android

I have one ImageView in my application which can be situated anywhere on screen 我的应用程序中有一个ImageView,它可以位于屏幕上的任何位置

On touch I want to move this view at the center of the Screen. 在触摸时我想将此视图移动到屏幕的中心。 I tried this functionality with Translate Animation and its RELATIVE_TO_PARENT functionality as follows 我使用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);

but ImageView moves 50% (of the screen) down from its current position. 但ImageView从当前位置向下移动50%(屏幕)。

Is there any way to move this view to the center of the screen regardless of its current position? 有没有办法将此视图移动到屏幕中心,无论其当前位置如何?

In order to move a View anywhere on the screen, I would recommend placing it in a full screen layout. 为了在屏幕上的任何位置移动视图,我建议将其放置在全屏布局中。 By doing so, you won't have to worry about clippings or relative coordinates. 通过这样做,您不必担心剪报或相对坐标。

You can try this sample code: 您可以尝试以下示例代码:

main.xml 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>

Your activity 你的活动

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

The method moveViewToScreenCenter gets the View's absolute coordinates and calculates how much distance has to move from its current position to reach the center of the screen. 方法moveViewToScreenCenter获取View的绝对坐标,并计算从当前位置移动到达屏幕中心的距离。 The statusBarOffset variable measures the status bar height. statusBarOffset变量测量状态栏高度。

I hope you can keep going with this example. 我希望你能继续这个例子。 Remember that after the animation your view's position is still the initial one. 请记住,在动画之后,您的视图位置仍然是初始位置。 If you tap the MOVE button again and again the same movement will repeat. 如果再次点击“移动”按钮,将重复相同的移动。 If you want to change your view's position do it after the animation is finished. 如果要在动画结束后更改视图的位置,请执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM