动画不会结束 – Android

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

Animation doesn't end - Android

问题

在我的应用程序中,我有一个按钮用来显示一个下拉菜单,在该菜单内部,我们有一些选项,其中之一是“抛硬币”,
该选项的目的是进行一个简单的抛硬币动画,该动画出现在一个textView内,显示硬币的正面或反面,而不是textView中的文本。
我有两个问题:

  1. 动画不按照我想要的方式工作,它应该在1秒内出现一个硬币,而不是textView内的文本,它停留在那里,2秒后消失,但在消失后,硬币图像会再次出现在textView内,它不应该重新出现。
  2. 这不是一个真正的问题,而更像是一个可选问题,类似于“你知道如何做到这一点吗?”我不知道如何创建一个带有多次旋转的硬币翻转动画。

XML 下拉菜单:

    <item
        android:id="@+id/flipacoin"
        android:title="@string/flipACoin" />
    <item
        android:id="@+id/rolladice"
        android:title="@string/rollADice" />
    <item
        android:id="@+id/imagebackground"
        android:title="@string/changeImage" />

JAVA 调用动画函数的代码:

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.flipacoin:
                flipACoin();
                return true;

            case R.id.rolladice:
                Toast.makeText(this,"TODO 掷骰子",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.imagebackground:
                Toast.makeText(this,"TODO 更改图像",Toast.LENGTH_SHORT).show();
                return true;
            default:
                return false;
        }
    }

JAVA 动画函数:

public void flipACoin(){
        coin.setText(null); //这是为了移除textView内的文本
        coin.setBackground(RANDOM.nextFloat() > 0.5f ? getResources().getDrawable(R.drawable.tails) : getResources().getDrawable(R.drawable.heads));
        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new DecelerateInterpolator());
        fadeIn.setDuration(1000);

        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setStartOffset(2000);
        fadeOut.setDuration(1000);

        AnimationSet animation = new AnimationSet(false); 
        animation.addAnimation(fadeIn);
        animation.addAnimation(fadeOut);
        coin.setAnimation(animation); 
    }
英文:

In my app, I have a button to show a drop-down menu, inside of that menu we have some options, one of this is "flip A coin",
the purpose of this option is to flip a coin easy animation, that animation appears inside a textView, and show a head-side or a tail-side of a coin instead of the text in the textView.
I have two problems:

  1. The animation doesn't work how I want, it should appear in 1 second a coin instead of the text inside the textView, it stays there and after 2 seconds he disappears, but after the disappearance, the coin image come back inside the textView, it shouldn't reappear.动画不会结束 – Android
  2. This is not a real question for a problem but more an optional question like "you know how to do that?". I 'don't know how to create a flip animation with a coin multiple rotations.

XML drop down menu:

   &lt;item
       android:id=&quot;@+id/flipacoin&quot;
       android:title=&quot;@string/flipACoin&quot; /&gt;
   &lt;item
       android:id=&quot;@+id/rolladice&quot;
       android:title=&quot;@string/rollADice&quot; /&gt;
   &lt;item
       android:id=&quot;@+id/imagebackground&quot;
       android:title=&quot;@string/changeImage&quot; /&gt;

JAVA code that calls the animation function:

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.flipacoin:
                flipACoin();
                return true;

            case R.id.rolladice:
                Toast.makeText(this,&quot;TODO roll a dice&quot;,Toast.LENGTH_SHORT).show();
                return true;
            case R.id.imagebackground:
                Toast.makeText(this,&quot;TODO image background&quot;,Toast.LENGTH_SHORT).show();
                return true;
            default:
                return false;
        }
    }

JAVA animation function:

public void flipACoin(){
        coin.setText(null); //this is for remove the text inside the textView
        coin.setBackground(RANDOM.nextFloat() &gt; 0.5f ? getResources().getDrawable(R.drawable.tails) : getResources().getDrawable(R.drawable.heads));
        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new DecelerateInterpolator());
        fadeIn.setDuration(1000);

        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setStartOffset(2000);
        fadeOut.setDuration(1000);

        AnimationSet animation = new AnimationSet(false); 
        animation.addAnimation(fadeIn);
        animation.addAnimation(fadeOut);
        coin.setAnimation(animation); 
    }

答案1

得分: 1

当您在开始时设置背景,动画后它会保持不变。要将文本和背景设置回空值,您可以添加动画监听器。以下是执行此操作的示例应用程序:

public class MainActivity extends AppCompatActivity {
    TextView coin;
    Random RANDOM;

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

        RANDOM = new Random();

        coin = findViewById(R.id.coin);

        findViewById(R.id.click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flipACoin();
            }
        });
    }

    public void flipACoin() {
        coin.setText(null);
        coin.setBackground(ResourcesCompat.getDrawable(getResources(),
                RANDOM.nextFloat() > 0.5f ? R.drawable.ic_launcher_background : R.drawable.ic_launcher_foreground,
                null
        ));

        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new DecelerateInterpolator());
        fadeIn.setDuration(1000);

        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setStartOffset(2000);
        fadeOut.setDuration(1000);

        AnimationSet animation = new AnimationSet(false);
        animation.addAnimation(fadeIn);
        animation.addAnimation(fadeOut);

        // listener, it will execute function when animation starts/ends/repeats
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d("MyTag", "onAnimationStart:");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d("MyTag", "onAnimationEnd:");
                coin.setBackground(null);
                coin.setText("Default");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d("MyTag", "onAnimationRepeat:");
            }
        });
        coin.setAnimation(animation);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"
        />

    <TextView
        android:id="@+id/coin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default"
        />

</androidx.appcompat.widget.LinearLayoutCompat>
英文:

When You set background at the beginning it just stays after animation. To set back text and background to null You can add Animation listener. Below is sample application which does it:

public class MainActivity extends AppCompatActivity
{
    TextView coin;
    Random RANDOM;

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

        RANDOM = new Random();

        coin = findViewById(R.id.coin);

        (findViewById(R.id.click)).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                flipACoin();
            }
        });
    }

    public void flipACoin()
    {
        coin.setText(null);
        coin.setBackground(ResourcesCompat.getDrawable(getResources(),
                                                       RANDOM.nextFloat() &gt; 0.5f ? R.drawable.ic_launcher_background : R.drawable.ic_launcher_foreground,
                                                       null
        ));

        Animation fadeIn = new AlphaAnimation(0, 1);
        fadeIn.setInterpolator(new DecelerateInterpolator());
        fadeIn.setDuration(1000);

        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setStartOffset(2000);
        fadeOut.setDuration(1000);

        AnimationSet animation = new AnimationSet(false);
        animation.addAnimation(fadeIn);
        animation.addAnimation(fadeOut);

        // listener, it will execute function when animation starts/ends/repeats
        animation.setAnimationListener(new Animation.AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {
                Log.d(&quot;MyTag&quot;, &quot;onAnimationStart:&quot;);
            }

            @Override
            public void onAnimationEnd(Animation animation) // when animation ends, set text and background to null
            {
                Log.d(&quot;MyTag&quot;, &quot;onAnimationEnd:&quot;);
                coin.setBackground(null);
                coin.setText(&quot;Default&quot;);
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {
                Log.d(&quot;MyTag&quot;, &quot;onAnimationRepeat:&quot;);
            }
        });
        coin.setAnimation(animation);
    }
}

activity_main.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.appcompat.widget.LinearLayoutCompat xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    &gt;

    &lt;Button
        android:id=&quot;@+id/click&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;click&quot;
        /&gt;

    &lt;TextView
        android:id=&quot;@+id/coin&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Default&quot;
        /&gt;

&lt;/androidx.appcompat.widget.LinearLayoutCompat&gt;

huangapple
  • 本文由 发表于 2020年10月8日 22:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64264230.html
匿名

发表评论

匿名网友

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

确定