• TranslateAnimation 运行动画后实际位置不正确问题


    最近在调试android 动画时候发现一个很奇怪问题,网上搜索都说TranslateAnimation 动画运行后,实际位置要在动画结束的监听里面重新设置才会正确,不然物体位置还是在原位。

    我根据网上所说之动画监听里面添加了动画结束监听,设置了物体到新位置,发现物体在视图里面还是错位,

    后来经过调试发现,必须在动画结束时候移除动画,然后重新设置位置才会正确

    AnimationSet set = new AnimationSet(true);
    set.addAnimation(translateAnimation);
    set.addAnimation(alpha);
    set.setDuration(1000);
    set.setFillAfter(true);
    texas.startAnimation(set);

    set.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

    texas.clearAnimation();// 增加这句后,重新设置位置,物体才会移动正确
    Log.d(TAG,"end Top:" + texas.getTop() + ",Y:" + texas.getY());

    RelativeLayout.LayoutParams paramsTexas = new RelativeLayout.LayoutParams(30,30);
    paramsTexas.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    paramsTexas.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    if(targetdirect == 0)
    {
    paramsTexas.setMargins(gridLayout.getLeft() + left + playerPanel.getWidth() / 2,(top + playerPanel.getHeight() + 40),0,0);
    }
    if(targetdirect == 1)
    {
    paramsTexas.setMargins((left - 40),top + playerPanel.getHeight() / 2,0,0);
    }
    if(targetdirect == 2)
    {
    paramsTexas.setMargins(gridLayout.getLeft() + left + playerPanel.getWidth() / 2,top - 60,0,0);
    }
    if(targetdirect == 3)
    {
    paramsTexas.setMargins(gridLayout.getLeft() + left + playerPanel.getWidth() + 40,top + playerPanel.getHeight()/2,0,0);
    }
    texas.setLayoutParams(paramsTexas);



    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
    });

  • 相关阅读:
    actionbar
    记:使用Xenocode加壳混淆后,无法“自杀覆盖”的自动更新
    用c# 实现设计器 DesignSurface
    将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)
    【转】给自己的软件制作注册码
    免费的Web Service网址
    MS SQL 2005/8 无法远程连接的解决方案【留】
    IIS 部署 ASP.NET 的一个注意点
    成语验证
    C#利用msscript.ocx控件执行JS脚本
  • 原文地址:https://www.cnblogs.com/maikkk/p/3596101.html
Copyright © 2020-2023  润新知