使用Animation和onAnimationEnd Listener时的Android Flicker

时间:2012-03-14 12:04:11

标签: android android-animation

我有3个ImageButtons,当填充到左边时会动画,如下所示:

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

    }

现在,我有一个像这样的onanimationEnd监听器:

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

}

问题在于,当我在做动画时(goRight除外),我的图标闪烁了一会儿。我不知道如何解决这个问题。请帮助。

2 个答案:

答案 0 :(得分:4)

安娜,我想你已经在这里阅读了我的答案

Android TranslateAnimation resets after animation

android animation is not finished in onAnimationEnd

我还回复了你的评论,基本上不是在mAnimation.setAnimationListener方法中执行功能,你需要创建自定义ImageButtons并将功能放在那里

public Class myImageButton extends ImageButton {
@Override
    protected void onAnimationEnd() {
        super.onAnimationEnd();
        //Functionality here
    }

答案 1 :(得分:0)

我搜索了所有stackoverflow帖子的动画问题(闪烁和缓慢)。 我没有找到任何完美的答案。但我找到了相同的解决方案,如下所示,

onStart of Animation use,

view_you_want_to_animate.setDrawingCacheEnabled(true);

on动画使用结束,

view_you_want_to_animate.setDrawingCacheEnabled(false);

现在,当我的视图是动画时,我的视图没有闪烁或缓慢的行为。 我对我很好。