如何用动画动画翻译在Android的视图 [英] How to animate a View with Translate Animation in Android

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

问题描述

我在我的应用的一个ImageView的,可以在任何地方位于屏幕

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

在触摸我想提出这个观点在屏幕的中央。我想这个功能与翻译动画及其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?

推荐答案

为了随时随地移动屏幕上的看法,我会建议将其放置在一个全屏幕的布局。通过这样做,你会不会担心剪报或相对坐标。

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.

您可以试试这个示例code:

You can try this sample code:

的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 变量措施的状态栏的高度。

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.

我希望你能跟上这个例子中去。请记住,动画完成后,你的看法的立场仍是初步的。如果你再次点击移动按钮,再而同样的动作会重复。如果你想改变你的观点的位置做动画完成后。

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的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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