私のアプリケーションには、画面上のどこにでも配置できるImageViewが1つあります
タッチで、このビューを画面の中央に移動します。次のように、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%を下に移動します。
現在の位置に関係なく、このビューを画面の中心に移動する方法はありますか?
ビューを画面上の任意の場所に移動するには、フルスクリーンレイアウトに配置することをお勧めします。そうすることで、クリッピングや相対座標を心配する必要がなくなります。
このサンプルコードを試すことができます。
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ボタンを何度もタップすると、同じ動きが繰り返されます。ビューの位置を変更する場合は、アニメーションの終了後に変更します。