如何在Android中使用翻译animation制作视图

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

在触摸我想要移动此视图在屏幕的中心。 我尝试了Translate Animation和RELATIVE_TO_PARENTfunction的这个function,如下所示

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

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

为了在屏幕上的任何位置移动视图,我build议将其放置在全屏布局中。 通过这样做,您不必担心剪报或相对坐标。

你可以试试这个示例代码:

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方法获取视图的绝对坐标,并计算从当前位置移动到屏幕中心的距离。 statusBarOffsetvariables测量状态栏的高度。

我希望你能继续这个例子。 请记住,在animation之后,您的视图的位置仍然是最初的位置。 如果您再次轻按MOVEbutton,将会重复相同的动作。 如果你想改变视图的位置,在animation完成之后再做。