【问题标题】:ImageView animateImageView 动画
【发布时间】:2016-11-10 11:17:31
【问题描述】:

我的动画有问题。我写了这段代码,但什么也没发生。

public void animate(View v){
    ImageView img = (ImageView) findViewByid(R.id.imageview);
    img.animate().translationYBy(-1000f).setDuration(300);
}

我使用的是 Android Studio 2.2.2

【问题讨论】:

    标签: android animation imageview


    【解决方案1】:

    试试这样可能对你有帮助

    public void SlideToY() {
        Animation slide = null;
        slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -5.0f, Animation.RELATIVE_TO_SELF, 0.0f);
    
        slide.setDuration(2000);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        img.startAnimation(slide);
    
        slide.setAnimationListener(new Animation.AnimationListener() {
    
            @Override
            public void onAnimationStart(Animation animation) {
    
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
    
    
            }
    
        });
    
    }
    

    【讨论】:

      【解决方案2】:

      试试下面的 imageview 动画代码。下面的例子向右平移 150 像素(x 坐标)

       Animation anim= new TranslateAnimation(xCurrentPos, xCurrentPos+150, yCurrentPos, yCurrentPos); 
      anim.setDuration(1000); 
      anim.setFillAfter(true); 
      anim.setFillEnabled(true); 
      animSurprise2Movement.setAnimationListener(new AnimationListener() {
      
          @Override
          public void onAnimationStart(Animation arg0) {}
      
          @Override
          public void onAnimationRepeat(Animation arg0) {}
      
          @Override
          public void onAnimationEnd(Animation arg0) {
              xCurrentPos -= 150;
          }
      });
      imageview.startAnimation(anim);
      

      【讨论】:

        【解决方案3】:

        您可以阅读this 了解android 视图动画。

        【讨论】:

          【解决方案4】:

          试试这个动画

          public void animate(View v){
                  ImageView img = (ImageView) findViewByid(R.id.imageview);
          
                  Animation anim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                  anim.setInterpolator(new LinearInterpolator());
                  anim.setRepeatCount(Animation.INFINITE);
                  anim.setDuration(4000);
          
                  img.startAnimation(anim);
              }
          

          【讨论】:

          • 感谢大家,现在正在使用我的代码。认为我在 .xml 文件中有错误。
          【解决方案5】:

          您缺少start() 方法。

          public void animate(View v){
              ImageView img = (ImageView) findViewByid(R.id.imageview);
              img.animate().translationYBy(-1000f).setDuration(300).start();
          }
          

          【讨论】:

            猜你喜欢
            • 2019-07-25
            • 2011-01-03
            • 2014-10-30
            • 2011-01-17
            • 1970-01-01
            • 2014-10-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多