在为Android按钮应用移动动画后,点击操作不起作用。

5

大家好,我设计了一个视图,在这个视图中添加了三个按钮。我给所有三个按钮都提供了 on_click 功能。我的要求是,如果我点击 button_1,则 button_2 和 button_3 应该移动到一个新位置。但问题是,在对 button_2 应用移动动画后,它们在新位置上的 on_click 功能失效了。

MainActivity.java

public class MainActivity extends Activity {

ImageButton slider,slidernew,sliderexisting;
boolean flag1 = true;
boolean flag2 = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //final LinearLayout container = (LinearLayout) findViewById(R.id.container);
    slider = (ImageButton) findViewById(R.id.imageButton1);
    slidernew = (ImageButton) findViewById(R.id.imageButton2);
    sliderexisting = (ImageButton) findViewById(R.id.imageButton3);

    // Set long default duration for the animator, for the purposes of this demo
    //animate(slidernew).setDuration(2000);
    slider.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*int xValue = container.getWidth() - slidernew.getWidth();
            int yValue = container.getHeight() - slidernew.getHeight();
            animate(slidernew).x(xValue).y(yValue);*/
            if(flag1==true)
            {
                animateSliderForward();
            flag1=false;
            }
            else{
                animateSliderBackward();
                flag1=true;
            }
        }
    });

    slidernew.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(MainActivity.this,SecondActivity.class));

        }
    });
    sliderexisting.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(MainActivity.this,SecondActivity.class));

        }
    });
}

void animateSliderForward(){
    float fromX=0;
    float toX=100;
    float fromY=0;
    float toY=100;
    TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
    animation.setDuration(1000);
    animation.setFillAfter(true);
    float fromA=0;
    float toA=-100;
    float fromB=0;
    float toB=100;
    TranslateAnimation animation1 = new TranslateAnimation(fromA, toA, fromB, toB);
    animation1.setDuration(1000);
    animation1.setFillAfter(true);
    slidernew.startAnimation(animation1);
    sliderexisting.startAnimation(animation);
}
   void animateSliderBackward(){
   float fromX=100;
   float toX=0;
   float fromY=100;
   float toY=0;
   TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
   animation.setDuration(1000);
   animation.setFillAfter(true);
   float fromA=-100;
   float toA=0;
   float fromB=100;
   float toB=0;
   TranslateAnimation animation1 = new TranslateAnimation(fromA, toA, fromB, toB);
   animation1.setDuration(1000);
   animation1.setFillAfter(true);
   slidernew.startAnimation(animation1);
   sliderexisting.startAnimation(animation);
   }


}
2个回答

2
考虑使用属性动画

根据上述文档。

视图动画系统的另一个缺点是它只修改了View的绘制位置,而不是实际的View本身。例如,如果您将一个按钮动画移动到屏幕上,按钮会正确绘制,但您可以单击按钮的实际位置不会改变,因此您必须实现自己的逻辑来处理这个问题。

使用属性动画将解决此问题。


1
你已经在动画和跳转到下一个活动中都使用了onClickListener。将其中一个更改为onTouchListener...确保在onTouchListener中返回false,以便onClick执行。

1
先生,将 onClickListener 更改为 onTouchListener 后没有效果。 - Sritam Jagadev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接