如何在 Android 中使用 Translate Animation 为视图设置动画 [英] How to animate a View with Translate Animation in Android

查看:29
本文介绍了如何在 Android 中使用 Translate Animation 为视图设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one ImageView in my application which can be situated anywhere on screen

触摸时,我想将此视图移动到屏幕的中心.我使用 Translate Animation 及其 RELATIVE_TO_PARENT 功能尝试了此功能,如下所示

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

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%(屏幕的).

but ImageView moves 50% (of the screen) down from its current position.

有没有办法把这个视图移动到屏幕的中心而不管它的当前位置?

Is there any way to move this view to the center of the screen regardless of its current position?

推荐答案

为了将 View 移动到屏幕上的任何位置,我建议将其置于全屏布局中.这样做,您就不必担心剪报或相对坐标.

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:

ma​​in.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 方法获取 View 的绝对坐标并计算从其当前位置移动到屏幕中心的距离.statusBarOffset 变量测量状态栏的高度.

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. The statusBarOffset variable measures the status bar height.

我希望你能继续这个例子.请记住,在动画之后,您的视图位置仍然是初始位置.如果您一次又一次地点击 MOVE 按钮,同样的动作将重复.如果您想更改视图的位置,请在动画完成后进行.

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.

这篇关于如何在 Android 中使用 Translate Animation 为视图设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆