我想在Android2.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的地方,它绘制后动画。如何在动画后将此切换按钮输入转换到其绘制位置?
发布于 2014-09-10 16:18:09
您使用的是视图动画,它只会在绘制的位置移动,而不会移动到可以点击的位置。您可以切换到属性动画,该动画将在其移动时更新实际位置(包括绘制位置和可点击位置)。
https://stackoverflow.com/questions/25651436
复制相似问题