[英]Android Button Doesn't Respond After Animation
在我的应用程序中当前按下按钮后,我有一个按钮的基本动画。 按钮完成动画后,我再也无法点击它了。 它甚至没有按橙色突出显示。
有帮助吗?
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
animation = new AnimationSet(true);
animation.setFillAfter(true);
Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 5.0f);
translate.setDuration(500);
animation.addAnimation(translate);
LayoutAnimationController controller = new LayoutAnimationController(animation, 0.25f);
generate = (Button)findViewById(R.id.Button01);
generate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
keyFromTop();
}
});
}
public void keyFromTop(){
generate.setAnimation(animation);
}
动画仅影响窗口小部件的绘制,这意味着在动画完成后,您的按钮仍处于其先前位置。 如果要将按钮移动到新位置,则需要手动更新按钮的布局参数。 此外,您的AnimationSet和您的AnimationController都没用。
如果您希望能够在动画完成后单击该按钮,则必须手动移动该组件。 以下是应用于按钮的翻译动画示例:
public class Test2XAnimation extends Activity {
private RelativeLayout buttonContainer;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
buttonContainer = (RelativeLayout) button.getParent();
}
public void startTapped(View view) {
animateButton(200, 100);
}
public void buttonTapped(View view) {
Toast.makeText(this, "tap", Toast.LENGTH_SHORT).show();
}
private RelativeLayout.LayoutParams params;
private CharSequence title;
private void animateButton(final int translateX, final int translateY) {
TranslateAnimation translate = new TranslateAnimation(0, translateX, 0,
translateY);
translate.setDuration(1500);
translate.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
buttonContainer.removeView(button);
button = new Button(Test2XAnimation.this);
button.setText(title);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttonTapped(button);
}
});
params.leftMargin = translateX + params.leftMargin;
params.topMargin = translateY + params.topMargin;
params.rightMargin = 0 + params.rightMargin;
params.bottomMargin = 0 + params.bottomMargin;
button.setLayoutParams(params);
buttonContainer.addView(button);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
params = (RelativeLayout.LayoutParams) button.getLayoutParams();
title = button.getText();
}
});
button.startAnimation(translate);
}
}
当用户单击UI中的按钮时,将触发startTapped()方法。 另一个按钮移动(200,100)。 最后,我删除旧的并创建一个新的,然后将其添加到父视图。 您可以看到在动画后调用buttonTapped()。
建议:如果要同时支持动画组件的新旧方法,可以使用NineOldAndroids项目,然后可以检查操作系统版本并仅在Gingerbread及更低版本上运行此代码。
我真的很努力地改变布局参数,使“真正的”按钮与其动画位置相匹配。 我终于成功地使用了这种方法 。 我扩展了ImageButton并覆盖了getHitRect:
@Override
public void getHitRect(Rect outRect) {
Rect curr = new Rect();
super.getHitRect(curr);
outRect.bottom = curr.bottom + 75;
outRect.top = curr.top + 75;
outRect.left = curr.left;
outRect.right = curr.right;
}
然后,我可以在麻烦的视图中使用此按钮:
<com.sample.YPlus75ImageButton a:id="@+id/....
值得注意的是3.0 的这一切都发生了变化 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.