【问题标题】:Java Android ImageView-Image rotation only works for the first timeJava Android ImageView-图像旋转仅第一次起作用
【发布时间】:2020-07-23 14:31:20
【问题描述】:

我在 java 中为图像视图编写了一个代码,该代码将旋转并消失,另一个图像将可见,我使用 imageViewer.animate.rotation(3600).alpha(0); 现在当我在模拟器上运行代码时它工作正常,图像消失并重新- 看起来很好,但问题是我第一次编译应用程序后点击图像时的旋转,它会旋转并消失并显示下一张图片,但是当我再次点击图像时,它不会旋转,而是只有淡入和淡出会起作用,而不是旋转(请注意,它在编译后第一次旋转,但随后不旋转并且仅淡入/淡出起作用)。代码如下:

public class MainActivity extends AppCompatActivity
{
    boolean eggview = true;

    public void fade(View view)
    {
        ImageView eggImageView = (ImageView) findViewById(R.id.egg);
        ImageView chickImageView = (ImageView) findViewById(R.id.chick);
        if (eggview)
        {
            eggview = false;
            eggImageView.animate().rotation(3600).setDuration(1200).alpha(0);
            chickImageView.animate().alpha(1);
        }
        else
        {
            eggview = true;
            chickImageView.animate().rotation(3600).alpha(0).setDuration(1200);
            eggImageView.animate().alpha(1);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

【问题讨论】:

  • 所以你有 2 张图片。如果您单击您想要一个旋转和淡入淡出并显示第二个?而当再次点击你想要一样吗?
  • 是的,正是……!

标签: java android android-studio android-imageview image-rotation


【解决方案1】:

在将 alpha 设置为 1 时,您必须将 rotation 设置为 0。当您将rotation 设置为3600 时,它不是当前状态的3600 度数,而是默认状态,这就是它只工作一次的原因。

boolean eggview = true;
public void fade(View view)
{
    ImageView eggImageView = (ImageView) findViewById(R.id.egg);
    ImageView chickImageView = (ImageView) findViewById(R.id.chick);
    if (eggview)
    {
        eggview = false;
        eggImageView.animate().rotation(3600).setDuration(1200).alpha(0);
        chickImageView.animate().alpha(1).rotation(0);
    }
    else
    {
        eggview = true;
        chickImageView.animate().rotation(3600).alpha(0).setDuration(1200);
        eggImageView.animate().alpha(1).rotation(0);
    }
}

如果您不想在淡入时进行反向旋转,只需将旋转设置为actual state + 3600。但是您的代码的主要问题是将旋转设置为实际状态。

【讨论】:

  • 就像一个魅力。几天以来我一直在寻找这个问题,但无法解决。最后我在这里问和中提琴......谢谢!
【解决方案2】:

使用

ViewPropertyAnimator

方法rotationBy()而不是rotation()

eggImageView.animate().rotationBy(3600)...

rotation() 将 View 移动到 360f,rotationBy() 将 View 移动到 360f

与 alphaBy() 相同

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多