0

I have 3 ImageButtons which are animated when Filling to left, like this:

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
    ImageButton top;
    ImageButton left;
    ImageButton right;

    top     = meMap.get("top");
    left    = meMap.get("left");
    right   = meMap.get("right");

    goLeft =
            new TranslateAnimation(0, -(top.getLeft() - left.getLeft()), 0, -(top.getTop() - left.getTop()));

    goRight =
            new TranslateAnimation(0, -(left.getLeft() - right.getLeft()), 0, -(left.getTop() - right.getTop()));

    goTopFromRight =
            new TranslateAnimation(0, top.getLeft() - right.getLeft(), 0, top.getTop() - right.getTop());


    goRight.setAnimationListener(this);

    if (velocityX<-500)
    {
        System.out.println("aaaa "+"ScrollLeft");


        goLeft.setDuration(500);   
        goLeft.setFillAfter(true);
        goLeft.setFillAfter(true);
        top.startAnimation(goLeft);

        goRight.setDuration(500);  
        goRight.setFillAfter(true);
        goRight.setFillAfter(true);
        left.startAnimation(goRight);

        goTopFromRight.setDuration(500); 
        goTopFromRight.setFillAfter(true);
        goTopFromRight.setFillAfter(true);
        right.startAnimation(goTopFromRight);

    }

Now, I have an onanimationEnd listener like this:

public void onAnimationEnd(Animation animation) {
    ImageButton top;
    ImageButton left;
    ImageButton right;

    top     = meMap.get("top");
    left    = meMap.get("left");
    right   = meMap.get("right");



    top.clearAnimation();
    left.clearAnimation();
    right.clearAnimation();


    RelativeLayout.LayoutParams layoutLeft = 
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

    layoutLeft.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.CenterTextView);
    layoutLeft.addRule(RelativeLayout.LEFT_OF,R.id.CenterTextView);
    top.setLayoutParams(layoutLeft);


    RelativeLayout.LayoutParams layoutRight = 
            new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

    layoutRight.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.CenterTextView);
    layoutRight.addRule(RelativeLayout.RIGHT_OF,R.id.CenterTextView);
    left.setLayoutParams(layoutRight);

    RelativeLayout.LayoutParams layoutTop = 
            new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

    layoutTop.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
    layoutTop.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
    right.setLayoutParams(layoutTop);


    meMap.put("top", right);
    meMap.put("left", top);
    meMap.put("right", left);

}

The problem is that when I am doing the animations (except goRight), my icons are Flickering for a litle moment. I don't know how to resolve this. Please Help.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

2 Answers2

4

Ana, I think you have already read my answers here

Android TranslateAnimation resets after animation

android animation is not finished in onAnimationEnd

I had also replied to your comment, basically in stead of doing the functionality inside the mAnimation.setAnimationListener method, you need to create custom ImageButtons and put the functionality there

public Class myImageButton extends ImageButton {
@Override
    protected void onAnimationEnd() {
        super.onAnimationEnd();
        //Functionality here
    }
Community
  • 1
  • 1
Soham
  • 4,940
  • 3
  • 31
  • 48
  • Thank you very much for the replay @Soham. In fact, if I understand, I have to delate the goRight.setAnimationListener(this); and then, I have to create 3 classes for my 3 image buttons? I am sory but I am a novice. – Milos Cuculovic Mar 14 '12 at 12:24
  • Yess, you absolutely correct, try creating those classes and then define the functionality inside them. – Soham Mar 14 '12 at 13:17
  • In fact, the public class myImageButton shuld be inside of my Activity? What's abaut the constructor of this class? Inside, I can't Override the onAnimationEnd(). – Milos Cuculovic Mar 14 '12 at 13:40
  • Sorry, i can Override now the onAnimationEnd, but don't know how to use this, could you please give me an exemple with my code? – Milos Cuculovic Mar 14 '12 at 13:43
  • Can you please help me @Soham – Milos Cuculovic Mar 15 '12 at 07:22
0

I searched for all stackoverflow posts for animation issue (flicker and sluggish). I didnt find any perfect answer. But I have found the solution for the same, which is as below,

onStart of Animation use,

view_you_want_to_animate.setDrawingCacheEnabled(true);

onEnd of Animation use,

view_you_want_to_animate.setDrawingCacheEnabled(false);

Now my view does not have flicker or sluggish behavior when my view is animating. I works well for me.

Abhijit Kurane
  • 834
  • 15
  • 20