Android永久动画

时间:2014-09-03 18:40:40

标签: android animation

我想在android 2.3中执行简单的翻译动画。这就是我现在所拥有的

布局

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_test_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:contentDescription="@string/pointer_description"
        android:src="@drawable/red_dot" />

    <RelativeLayout
        android:id="@+id/testBottomPanel"
        android:layout_width="320dp"
        android:layout_height="295dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:scaleType="centerCrop"
            android:src="@drawable/measure_panel" />

        <ToggleButton
            android:id="@+id/startTestSwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/start_test_check"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_marginTop="20dp"
            android:textOff=""
            android:textOn="" />
    </RelativeLayout>

</RelativeLayout>

动画设置:

private void setUpView() {
            ToggleButton testSwitch = (ToggleButton)findViewById(R.id.startTestSwitch);
            final RelativeLayout lay = (RelativeLayout)findViewById(R.id.testBottomPanel);

            final TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 100);
            anim.setDuration(1000);
            anim.setFillAfter(true);
            anim.setAnimationListener(this);

            testSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked) {
                        lay.startAnimation(anim);
                    }
                    else {

                    }
                }
            });
        }

监听器:

@Override
public void onAnimationEnd(Animation animation) {
    final RelativeLayout lay = (RelativeLayout)findViewById(R.id.testBottomPanel);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)lay.getLayoutParams();
    params.topMargin += 100;
    lay.setLayoutParams(params);
}

我的问题是,当动画完成时,所有绘制都可以,但布局上的我的ToggleButton似乎保持其位置。我的意思是它比初始位置低100px但是当我点击它时没有响应。要点击它,我需要在动画后绘制的地方点击100px。如何将此Toggle Button输入转换为动画后绘制的位置?

1 个答案:

答案 0 :(得分:0)

您正在使用视图动画,该视图动画仅移动到绘制的位置,而不是可以轻触的位置。您可以切换到属性动画,该动画将在移动时更新实际位置(绘制位置和可点击位置)。