android - 为什么我的动画不会在给定的 to-x 值处停止?

标签 android animation view

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

这是我的布局:

<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 中设置 View 移动的方式:

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

这就是我移动 View 并启动动画的方式:

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

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

最佳答案

你需要使用

mSlideAnimation.setFillAfter(true);

让它不再回到开始。

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

关于android - 为什么我的动画不会在给定的 to-x 值处停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13690444/

相关文章:

drupal - 如何将 View block 重定向到显示页面

android - 形状中的色调和纯色有什么区别?

c++ - 存储和访问动画的关键帧

android - 将 View 恢复到原始 xml 状态

php - Mysql Views - 自动增量不工作(通过 Eloquent ORM)

html - 更改 CSS 图像幻灯片放映速度

android - 跨多个 Activity 的计时器

android - Canvas 未正确重绘

android - 我怎样才能**禁用**互联网?

javascript - jquery 禁用点击直到动画完全完成