android OnAnimationEnd,无法在OnAnimationEnd中添加图像到布局

时间:2013-11-13 15:17:32

标签: android animation

`各位大家好,我对Android OnAnimationEnd有疑问。我有两个布局A和B,A是父级布局,中间包含B,底部包含ImageView(imv)。现在我需要将imv从其原始位置转换为布局B,它位于布局A的中间,这样在动画结束时imv将属于布局B而不是布局A.为了实现这一点,在OnAnimationEnd中,我删除了imv从它的父布局开始,然后尝试将它添加到布局B.但是最后动画imv在进入布局B时就消失了.System.out显示OnAnimationEnd被调用,布局childCount也显示imv被添加到布局B,但imv在布局B中永远不会是可变的。下面是代码,顺便说一下,我不是一个经验丰富的Android开发人员。提前谢谢。

public class MainActivity extends Activity implements AnimatorListener {
    ImageView imv;
    RelativeLayout parentL;
    RelativeLayout middleL;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imv = (ImageView) findViewById(R.id.imageView1);
        imv.setOnClickListener(new MyClick(this));
        parentL = (RelativeLayout) findViewById(R.id.rl);
        middleL = (RelativeLayout) findViewById(R.id.rll);
     }

     private class MyClick implements OnClickListener {
        MainActivity ma;

        public MyClick(MainActivity ma) {
            this.ma = ma;
        }

        @Override
        public void onClick(View v) {
            ImageView imv = (ImageView) v;
            float x = middleL.getX();
            float y = middleL.getY();
            imv.animate().setListener(ma);
            imv.animate().x(x).y(y);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onAnimationCancel(Animator arg0) {
    }

    @Override
    public void onAnimationEnd(Animator anim) {
        System.out.println("onAnimationEnd");
        ViewGroup vg = (ViewGroup)imv.getParent();
        vg.removeView(imv);
        middleL.addView(imv);
        System.out.println("layout child count = "+middleL.getChildCount());
    }

    @Override
    public void onAnimationRepeat(Animator arg0) {
    }

    @Override
    public void onAnimationStart(Animator arg0) {
        System.out.println("onAnimationStart");
    }

}

2 个答案:

答案 0 :(得分:0)

尝试调用invalidate(),它会告诉视图需要重绘自己

答案 1 :(得分:0)

定义动画时,请尝试使用anim.setFillAfter(true);