2012-12-03 33 views
1

我想要移動觸摸視圖,並且當用戶取消此視圖時,它會啓動一個動畫,將我的視圖移動到其父視圖的末尾。爲什麼我的動畫不停止在to-x給定值?

這是我的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/time_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<AbsoluteLayout 
    android:id="@+id/slide_layout" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_margin="20dp" 
    android:background="#0000FF" > 

    <View 
     android:id="@+id/slide_to_pause" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:background="#00FFFF" /> 
</AbsoluteLayout> 
</RelativeLayout> 

這是我怎麼設置我的onCreate移動視圖:

slideView = ((View) findViewById(R.id.slide_to_pause)); 
slideView.setOnTouchListener(this); 

這是我的移動視圖,並啓動動畫:

@Override 
public boolean onTouch(View view, MotionEvent event) { 
    AbsoluteLayout.LayoutParams layoutParams = (AbsoluteLayout.LayoutParams) view.getLayoutParams(); 

    switch (event.getAction() & MotionEvent.ACTION_MASK) { 

    case MotionEvent.ACTION_DOWN: 
     mX = event.getRawX(); 
     break; 

    case MotionEvent.ACTION_UP: 
     int endOfAnimation = findViewById(R.id.slide_layout).getWidth() - view.getWidth(); 
     mSlideAnimation = new TranslateAnimation(0, endOfAnimation - layoutParams.x, 0, 0); 
     mSlideAnimation.setDuration(1000);   
     view.startAnimation(mSlideAnimation); 
     Log.d(TAG, "endOfAnimation = " + layoutParams.x + " | " + endOfAnimation); 
     break; 

    case MotionEvent.ACTION_MOVE: 
     layoutParams.x = (int) event.getRawX();   
     view.setLayoutParams(layoutParams); 

     break; 
    } 
    return true; 
} 

問題是,當視圖到達最後它返回到屏幕中間的一個點,wh ich是用戶不支持視圖的點。 我該如何解決這個問題? 謝謝!

回答

1

您可以通過使用

View.offsetLeftAndRight(int offset) 
View.offsetTopAndBottom(int offset) 
+0

謝謝。我也會試試這個。 – androidevil