1

ビューをタッチで移動したいのですが、ユーザーがこのビューを離すと、ビューを親の最後に移動するアニメーションが開始されます。

これは私のレイアウトです:

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

問題は、ビューが最後に到達すると、画面の中央のポイントに戻ることです。これは、ユーザーがビューを保持しないポイントです。どうすればこれを修正できますか?ありがとうございました!

4

2 に答える 2

3

あなたが使用する必要があります

mSlideAnimation.setFillAfter(true);

最初に戻らないようにします。

それが機能しない場合は、Animation.setFillAfter / Beforeの提案に従う必要があるかもしれません-それらは機能しますか/それらは何のためにありますか?

于 2012-12-03T20:06:39.903 に答える
1

を使用して、アニメーションを手動でシミュレートできます(アニメーションフレームワークを使用せずに、ビューを自分で移動します)。

View.offsetLeftAndRight(int offset)
View.offsetTopAndBottom(int offset)
于 2012-12-03T20:00:14.897 に答える