【问题标题】:Android Button Doesn't Respond After AnimationAndroid 按钮在动画后没有响应
【发布时间】:2010-01-24 02:42:04
【问题描述】:

当前在我的应用程序中按下按钮后,我有一个基本动画。按钮完成动画后,我无法再单击它。它甚至没有以橙色突出显示。

有什么帮助吗?

这是我的代码:

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

【问题讨论】:

  • 不确定“按钮的基本动画”是什么意思。您使用的是 Button 对象还是某种自定义视图?您能否提供一些代码,以便我们了解您在做什么?

标签: java android


【解决方案1】:

动画仅影响小部件的绘制,这意味着在动画完成后,您的按钮仍位于其之前的位置。如果要将按钮移动到新位置,则需要手动更新按钮的布局参数。还有,你的 AnimationSet 和 AnimationController 也没用。

【讨论】:

  • 啊,好的,谢谢。是的,这些都是我以前做的事情遗留下来的。
【解决方案2】:

如果希望动画完成后能够点击按钮,则必须手动移动组件。 下面是一个应用于按钮的翻译动画示例:

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项目如果你想同时支持新的和旧的动画组件的方式,那么你可以检查操作系统版本并仅在姜饼和更低版本上运行此代码。

【讨论】:

  • 是我还是这两行很奇怪:params.rightMargin = 0 + params.rightMargin; params.bottomMargin = 0 + params.bottomMargin;
【解决方案3】:

我真的很努力改组布局参数以使“真实”按钮与其动画位置相匹配。我终于通过使用this approach 成功了。我扩展了 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/....

值得注意的是,all of this changes 用于 3.0。

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多