【问题标题】:How to move image up and down from a fixed location, rather than starting from the bottom?如何从固定位置上下移动图像,而不是从底部开始?
【发布时间】:2017-11-19 13:15:32
【问题描述】:

我目前使用的代码:

    TranslateAnimation mAnimation = new TranslateAnimation(
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 1.0f);
    mAnimation.setDuration(2000);
    mAnimation.setRepeatCount(-1);
    mAnimation.setRepeatMode(Animation.REVERSE);
    mAnimation.setInterpolator(new LinearInterpolator());
    imageView.setAnimation(mAnimation);

这里是result.(忽略画质,我改一下)。

我将图像放置在水平中心,与浮动操作按钮垂直对齐。我希望它从那里开始。

另外,可选地,我创建了一个淡入淡出AlphaAnimation。如何同步它们?

AlphaAnimation 代码:

AlphaAnimation blinkAnimation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
    blinkAnimation.setDuration(1000); // duration - half a second
    blinkAnimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
    blinkAnimation.setRepeatCount(5000); // Repeat animation infinitely
    blinkAnimation.setRepeatMode(Animation.REVERSE);

【问题讨论】:

    标签: android android-animation translate-animation


    【解决方案1】:

    这里是 Fade-In|Fade-Out 和 Move-Up|Move-Down 的详细代码

    上移和下移图像查看代码:

        TranslateAnimation anim = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, -1.0f); // this is distance of top and bottom form current positiong
    
               anim.setDuration(2000);
               anim.setRepeatCount(3);
               anim.setRepeatMode(Animation.REVERSE);  
               downloadIV.startAnimation(anim);
    

    淡入淡出:

       AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
        anim.setDuration(500);
        anim.setRepeatCount(4);
        anim.setRepeatMode(Animation.REVERSE);
    
        //anim.setFillAfter(true);
       downloadIV.startAnimation(anim);
    

    【讨论】:

      【解决方案2】:

      试试这个:

      TranslateAnimation mAnimation = new TranslateAnimation(
                  TranslateAnimation.RELATIVE_TO_SELF, 0f,
                  TranslateAnimation.RELATIVE_TO_SELF, 0f,
                  TranslateAnimation.RELATIVE_TO_SELF, 0f,
                  TranslateAnimation.RELATIVE_TO_SELF, -1.0f);
      //Less up :     TranslateAnimation.RELATIVE_TO_SELF, -0.5f);
      

      与 alphaAnimation 结合:

      AlphaAnimation blinkAnimation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
          blinkAnimation.setDuration(1000); // duration - half a second
          blinkAnimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
          blinkAnimation.setRepeatCount(5000); // Repeat animation infinitely
          blinkAnimation.setRepeatMode(Animation.REVERSE);
      
      AnimationSet set = new AnimationSet(true);
      set.addAnimation(trAnimation);
      set.addAnimation(mAnimation);
      imageView.startAnimation(set)
      

      【讨论】:

      • 谢谢,这确实有帮助,但是现在图像有点太高了,之前如何阻止它?或者如果你能解释TranslateAnimation上的参数,我可以自己试试。
      • 如果想停在之前,改变TranslateAnimation.RELATIVE_TO_SELF, -1.0f);转为 TranslateAnimation.RELATIVE_TO_SELF, -.0.5f);
      猜你喜欢
      • 1970-01-01
      • 2018-01-29
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多