zoukankan      html  css  js  c++  java
  • 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) {

    }
    });

  • 相关阅读:
    eclipse/myeclipse介绍
    HDU 5802 Windows 10
    LaTeX test
    数据结构2 静态区间第K大/第K小
    数据结构1 「在线段树中查询一个区间的复杂度为 $O(log N)$」的证明
    HDU 1007 Quoit Design
    HDU 5761 Rower Bo
    hihocoder #1341 Constraint Checker
    HDU #5733 tetrahedron
    hihocoder #1327
  • 原文地址:https://www.cnblogs.com/maikkk/p/3596101.html
Copyright © 2011-2022 走看看