2012-04-23 8 views
43

でアニメーションを翻訳して表示をアニメーション化する方法。私は画面の中央で、このビューを移動したいのタッチ画面</p> <p>上のどこにでも配置することができる自分のアプリケーション内の1つのImageViewのを持っているAndroidの

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); 

を次のようにアニメーションとそのRELATIVE_TO_PARENT機能を翻訳すると、私はこの機能を試みたが、ImageViewのダウン現在の位置から(画面)50%を移動させます。

このビューを現在の位置に関係なく画面の中央に移動する方法はありますか?

答えて

71

ビューを画面のどこにでも移動するには、フルスクリーンレイアウトで配置することをおすすめします。そうすることで、切り抜きや相対座標を心配する必要はありません。

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ボタンをもう一度タップすると、同じ動きが繰り返されます。ビューの位置を変更したい場合は、アニメーションが終了した後に行います。

+0

このmoveViewToScreenCenterを使用しましたが、ビューを画面の右上に移動するように変更しました。あなたの例のようにボタンから起動すると動作しますが、onCreateからmoveViewToScreenCenterを起動すると、ビューはまったく別の方法になります。 onCreate内のViewのoriginalPosは常に[0,0]のようです。何がありますか? – Jonny

+5

'onCreate'ビューでは、アクティビティがまだユーザには見えないので、次元は0です。 'onWindowFocusChanged'メソッドを呼び出してみてください。 –

+0

それは私のために目が覚めていない、私のビューは下に移動! –

関連する問題