Java Android ImageView-Image rotation only works for the first time

huangapple go评论152阅读模式
英文:

Java Android ImageView-Image rotation only works for the first time

问题

我已经为图像视图编写了 Java-Android 代码,可以使图像旋转和淡出,另一张图像将可见。我使用了 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);
    }
}
英文:

I have written java-android code for image view that will rotate and fade away and another image will be visible, I use imageViewer.animate.rotation(3600).alpha(0); now this works fine when I run the code on an emulator, the images fade and re-appear just fine, but the problem is about the rotation after I compile the app for the first time when I click on the image, it will rotate and fade away and the next picture is shown, but when I tap on the image again, it will not rotate, instead only the fade in and fade out will work but not the rotation (Note that it rotates for the first time after compilation but then doesn't rotates and fade in/out works only). Here is the code:

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);
    }
}

答案1

得分: 2

你在将 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);
    }
}

如果你不想在淡入时进行反向旋转,只需将旋转角度设置为“当前状态 + 3600”。但你的代码主要问题在于将旋转角度设置为当前状态。

英文:

You have to set rotation to 0 when setting alpha to 1. When You set rotation to 3600 it isn't 3600 degrees from the current state but from the default state, that's why it works only one time.

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);
    }
}

If You don't want to make a backward rotation when fading in just set rotation to actual state + 3600. But the main problem with Your code is setting rotation to the actual state.

答案2

得分: 1

使用ViewPropertyAnimator 方法rotationBy(),而不是rotation()

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

rotation() 将视图移动到 360 度,而rotationBy() 则将视图旋转 360 度。

alphaBy() 同理。

英文:

Use the

ViewPropertyAnimator

method rotationBy() instead of rotation()

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

rotation() moves the View TO 360f and rotationBy() moves the View BY 360f

same with alphaBy()

huangapple
  • 本文由 发表于 2020年7月23日 22:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63056703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定