簡體   English   中英

Animation 沒有結束 - Android

[英]Animation doesn't end - Android

在我的應用程序中,我有一個顯示下拉菜單的按鈕,在該菜單中我們有一些選項,其中之一是“擲硬幣”,此選項的目的是輕松擲硬幣 animation,即 animation出現在 textView 中,並顯示硬幣的正面或反面,而不是 textView 中的文字。我有兩個問題:

  1. animation 不按我的意願工作,它應該在 1 秒內出現一個硬幣而不是 textView 內的文字,它停留在那里,2 秒后他消失,但消失后,硬幣圖像又回到 textView 內,它不應該再次出現。 在此處輸入圖像描述
  2. 這不是一個真正的問題,而是一個可選的問題,比如“你知道怎么做嗎?”。 我不知道如何用硬幣多次旋轉來創建翻轉 animation。

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調用 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,"TODO roll a dice",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.imagebackground:
                Toast.makeText(this,"TODO image background",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() > 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); 
    }

當您在開始時設置背景時,它只停留在 animation 之后。要將文本和背景設置回 null,您可以添加 Animation 偵聽器。 下面是執行此操作的示例應用程序:

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) // when animation ends, set text and background to null
            {
                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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM