ビューを画面のどこにでも移動するには、フルスクリーンレイアウトで配置することをおすすめします。そうすることで、切り抜きや相対座標を心配する必要はありません。
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ボタンをもう一度タップすると、同じ動きが繰り返されます。ビューの位置を変更したい場合は、アニメーションが終了した後に行います。
このmoveViewToScreenCenterを使用しましたが、ビューを画面の右上に移動するように変更しました。あなたの例のようにボタンから起動すると動作しますが、onCreateからmoveViewToScreenCenterを起動すると、ビューはまったく別の方法になります。 onCreate内のViewのoriginalPosは常に[0,0]のようです。何がありますか? – Jonny
'onCreate'ビューでは、アクティビティがまだユーザには見えないので、次元は0です。 'onWindowFocusChanged'メソッドを呼び出してみてください。 –
それは私のために目が覚めていない、私のビューは下に移動! –