为什么我的动画不会停在正确的位置?

时间:2012-12-04 13:21:38

标签: android animation slide ontouchlistener

在may app中,我有两个动画。一个是从屏幕的底部到顶部增长。它在我的startNewAnimation()方法中调用:

mSurfaceGrowingAnimation = new TranslateAnimation(0, 0, mViewHeight, 0);
mSurfaceGrowingAnimation.setInterpolator(new LinearInterpolator());

此方法在此内部调用:

ViewTreeObserver observer = ViewTimer.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    @SuppressWarnings("deprecation")
public void onGlobalLayout() {
    Log.d(TAG, "onGlobalLayout");
        mViewHeight = ViewTimer.getHeight();
        startNewAnimation();
        ViewTimer.getViewTreeObserver().removeGlobalOnLayoutListener(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_UP:
        showDialog();
        pauseAnimation();
        int endOfAnimation = findViewById(R.id.slide_included).getWidth() - mSlideView.getWidth();
        mSlideAnimation = new TranslateAnimation(event.getX(), endOfAnimation - layoutParams.x, 0, 0);
        mSlideAnimation.setDuration(1000);          
        mSlideAnimation.setFillAfter(true);
        mSlideView.startAnimation(mSlideAnimation);

        break;

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

        break;
    }

也就是说,当我移动幻灯片视图时,我调用pauseAnimation()方法来清除主动画。

问题是:当我的主动画停止时,如果它位于幻灯片按钮下方,则它会被绘制到幻灯片视图的顶部。我该如何解决这个问题?

这是我的布局:

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

    <RelativeLayout
        android:id="@+id/time_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <View
            android:id="@+id/surface_view_timer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="0dp"
            android:background="#77B21B00" /> 
    </RelativeLayout>

<include
    android:id="@+id/slide_included"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_margin="20dp"
    layout="@layout/slide" />

</FrameLayout>

slide.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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>

0 个答案:

没有答案