【问题标题】:Why my animation does not stop in the to-x given value?为什么我的动画不会在 to-x 给定值处停止?
【发布时间】:2012-12-04 04:12:22
【问题描述】:

我想在触摸时移动一个视图,当用户松开这个视图时,它会启动一个动画,将我的视图移动到其父视图的末尾。

这是我的布局:

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

问题在于,当视图到达末尾时,它会回到屏幕中间的一个点,即用户松开视图的点。 我怎样才能解决这个问题? 谢谢!

【问题讨论】:

    标签: android animation view


    【解决方案1】:

    你需要使用

    mSlideAnimation.setFillAfter(true);
    

    让它不会回到起点。

    如果这不起作用,您可能必须遵循 Animation.setFillAfter/Before - Do they work/What are they for? 上的建议

    【讨论】:

      【解决方案2】:

      您可以使用手动模拟动画(自己移动视图,无需动画框架)

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

      【讨论】:

      • 谢谢。我也会试试这个。
      猜你喜欢
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      相关资源
      最近更新 更多