如何将android视图向上移动20 dp?

sqserrrh  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(254)

我想从下往上看
我需要支持minapi8,不能添加外部库
如果:
1) 我想在2秒内移动20分?
2) 我想把它从原来的位置移动到它的左上角移动两倍于视图高度?
这是我的密码:

public static void slideTooltip(final LinearLayout toolTipLayout) {

    TranslateAnimation layoutTranslateAnimation =
        new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0F, Animation.RELATIVE_TO_SELF, 1.0F,
            Animation.RELATIVE_TO_SELF, 2.0F, Animation.RELATIVE_TO_SELF, 1.0F);

    layoutTranslateAnimation.setDuration(10 * 1000);
    layoutTranslateAnimation.setFillAfter(true);
    layoutTranslateAnimation.setInterpolator(new DecelerateInterpolator());

    final AlphaAnimation alphaAnimation = new AlphaAnimation(0.2F, 1.0F);
    alphaAnimation.setDuration(10 * 1000);

    layoutTranslateAnimation.setAnimationListener(new AnimationListener() {

      @Override
      public void onAnimationStart(Animation animation) {
        toolTipLayout.startAnimation(alphaAnimation);
      }

      @Override
      public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub

      }
    });

    toolTipLayout.startAnimation(layoutTranslateAnimation);
  }

但我只看到小而快速的跳跃。我错过了什么?

inb24sb2

inb24sb21#

我实际上会使用viewpropertyanimator:

// start 20 pixels down and with 0.2 alpha
toolTipLayout.setTranslationY(20);
toolTipLayout.setAlpha(0.2);
// animate up and to full alpha.
toolTipLayout.animate().translationY(0).alpha(1).setDuration(10*1000).start();

如果您需要支持api12之前的android设备,请使用http://nineoldandroids.com wrapper 图书馆。

相关问题