【问题标题】:Rotating Image view by clicking on the button通过单击按钮旋转图像视图
【发布时间】:2017-04-17 12:31:01
【问题描述】:

我想通过单击按钮来旋转图像视图。 我要这个: 当用户单击按钮时,将图像视图旋转 90 度。 再次单击我的按钮时,将图像视图旋转 180 度。 再次单击我的按钮时,将图像视图旋转 270 度。 再次单击我的按钮时,将图像视图旋转 0 度。 这继续这样...... 通过此代码,我可以将图像视图正确旋转一次 90 度:

        mImageView.setRotation(0);

通过这个我可以旋转到 90 度并返回到 0 度。

float deg = (mImageView.getRotation() == 90F) ? 0F : 90F;
    mImageView.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());

我该怎么做?

【问题讨论】:

标签: java android


【解决方案1】:

在你的Button点击上写下下面的代码

imageView.setRotation(imageView.getRotation() + 90);

最初和完成整圈旋转后将0 rotation值设置为ImageView

【讨论】:

    【解决方案2】:
    private void rotate(float degree) {
        final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    
        rotateAnim.setDuration(0);
        rotateAnim.setFillAfter(true);
        imgview.startAnimation(rotateAnim);
    }
    

    【讨论】:

      【解决方案3】:

      您可以创建一个旋转动画并将其设置为Reverse 重复动画,这样视图将旋转到所需的角度并且会继续立即回到初始角度:

      ImageView diskView = (ImageView) findViewById(R.id.imageView3);
      
      // Create an animation instance
      Animation an = new RotateAnimation(0.0f, 360.0f, pivotX, pivotY);
      
      // Set the animation's parameters
      an.setDuration(10000);               // duration in ms
      an.setRepeatCount(1);                // -1 = infinite repeated
      an.setRepeatMode(Animation.REVERSE); // reverses each repeat
      an.setFillAfter(true);               // keep rotation after animation
      
      // Aply animation to image view
      diskView.setAnimation(an);
      diskView.startAnimation();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-17
        • 1970-01-01
        相关资源
        最近更新 更多