Android会在不应该的时候检测点击次数

时间:2014-01-13 13:20:20

标签: android android-animation

我有一个图片按钮和相关的点击处理程序。

当我动画按钮(使用翻译动画)时,按钮显然会改变它在屏幕上的位置。

但是存在一个问题:当我触摸初始按钮位置而不是当前按钮时,Android会检测到点击次数。

如何让Android尊重按钮的实际位置?

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/addButton"
        android:src="@android:drawable/ic_input_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="plusButtonClick"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends ActionBarActivity
{
    public void plusButtonClick(View v)
    {
        Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
        animateButton(R.id.addButton, R.anim.anim_move);
    }

    private void animateButton(int buttonId, int animationId)
    {
        View button = findViewById(buttonId);
        AnimationSet animationSet = (AnimationSet)AnimationUtils.loadAnimation(this, animationId);
        animationSet.setAnimationListener(getStartingButtonsListener(button));
        button.setVisibility(View.VISIBLE);
        button.startAnimation(animationSet);
    }

    private Animation.AnimationListener getStartingButtonsListener(final View v)
    {
        return new Animation.AnimationListener()
        {
            @Override
            public void onAnimationEnd(Animation arg0)
            {
                v.setVisibility(View.GONE);
            }
        };
    }
}

anim_move.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="true"
     android:fillAfter="true"
     android:duration="1000">

    <translate
        android:fromXDelta="0"
        android:toXDelta="180"
        android:fromYDelta="0"
        android:toYDelta="0"/>
</set>

3 个答案:

答案 0 :(得分:3)

我在这里遇到了类似的问题:Button moves to different part of screen but can only press it at its initial spot?

动画是动画像素,而不是小部件的触摸区域。 您还需要为属性设置动画:https://developer.android.com/guide/topics/graphics/prop-animation.html

答案 1 :(得分:1)

翻译动画实际上并不会移动您的对象,只会移动它的图像。您应该在动画结束时通过编辑LayoutParams

来自行重新定位按钮

答案 2 :(得分:0)

使用onTouchEvent作为按钮。此外,通过java文件提供此onTouch实现,但不使用XML布局文件。这比使用onClick事件要好得多。 请参阅:Triggering event when Button is pressed down in Android