1

what should I do to move the imageButton to a new palce and keep the OnClick event?

I have two questions on this:

  1. If I use fillAfter(true), the onclick will not taken to the new place.
  2. If use setAnimationListener(onAnimationEnd), and call layout() to move the ImageButton to a new place, the ImageButton will back to new beginning place, why?

codes here:

    tsla = new TranslateAnimation(0.0f,(float) (imgWidth * 0.45)-wh/2,0.0f,(float) (imgHeight * 0.566666667)-wh/2);
    tsla.setDuration(sleepX);

    tsla.setAnimationListener(new AnimationListener(){
        public void onAnimationStart(Animation arg0) {
        }
        public void onAnimationEnd(Animation arg0) {
             imgBtnChengdu.layout(
                (int) (imgWidth * 0.45),
                (int) (imgHeight * 0.566666667),
                (int) (imgWidth - wh - imgWidth * 0.45),
                (int) (imgHeight - wh - imgHeight * 0.566666667)
                );
        }
        public void onAnimationRepeat(Animation arg0) {
        }
    });

    imgBtnChengdu.setAnimation(tsla);      
SICON
  • 65
  • 1
  • 10

1 Answers1

2

The problem is that that Android will only animate the Image of the Button to the new place.

This means the onClick Area stays at the old place. You also need to change the position of the button at the end of the Animation to move the whole button to the new place and not only its View.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • the layout function should only be called internally from the Android System. Changing the position of the view could be done through changing the LayoutParameters of the view and how you do it depends on you layout. – Janusz Apr 13 '12 at 09:33
  • 1
    Yes, correct, thanks. Code like below: RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.leftMargin = left; lp.topMargin = top; imgBtn.setLayoutParams(lp); – SICON Apr 16 '12 at 02:40
  • @SICON ... left and top are this? `public void onAnimationEnd(Animation animation) { int[] pos={view.getLeft(),view.getTop(),view.getRight(),view.getBottom()}; }` – AlexAndro Jul 01 '12 at 14:40