簡體   English   中英

如何在逐幀中的兩幀之間延遲 Animation

[英]How to Delay between Two frames in Frame-By-Frame Animation

現在我有四個圖像,我在這四個圖像上應用了框架 animation

1.一個

2.二

3.三

4.Go

我想在三個和 Go 幀之間給出一個時間延遲,這將是一個隨機時間。

這是我的代碼

int d=5000;
AnimationDrawable drawable=new AnimationDrawable();
    drawable.addFrame(getResources().getDrawable(R.drawable.one), 1000);
    drawable.addFrame(getResources().getDrawable(R.drawable.two), 1000);        
    drawable.addFrame(getResources().getDrawable(R.drawable.three),d);      
    drawable.addFrame(getResources().getDrawable(R.drawable.go),d);
    drawable.setOneShot(true);
    iv3.setBackgroundDrawable(drawable);
    drawable.start();

iv3 是我的 ImageView

而且我還想在這些圖像上提供 Scale animation 和逐幀 animation。

請幫幫我...

謝謝@nks

看到這個對我來說很好。

  public class MainActivity extends ActionBarActivity {
ImageView view;
AnimationDrawable Anim;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view = (ImageView) findViewById(R.id.ivAnimation);
    view.setId(1);
    view.setBackgroundResource(R.drawable.image);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (view.getId() == 1) {
                animationStart();
                view.setId(2);
            } else {
                view.setBackgroundResource(R.drawable.image);
                view.setId(1);

            }
        }

        private void animationStart() {
            try {
                BitmapDrawable frame1 = (BitmapDrawable) getResources()
                        .getDrawable(R.drawable.one);
                BitmapDrawable frame2 = (BitmapDrawable) getResources()
                        .getDrawable(R.drawable.two);
                BitmapDrawable frame3 = (BitmapDrawable) getResources()
                        .getDrawable(R.drawable.three);

                BitmapDrawable frame4 = (BitmapDrawable) getResources()
                        .getDrawable(R.drawable.four);
                BitmapDrawable frame5 = (BitmapDrawable) getResources()
                        .getDrawable(R.drawable.five);
                BitmapDrawable frame6 = (BitmapDrawable) getResources()
                        .getDrawable(R.drawable.six);

                Anim = new AnimationDrawable();
                Anim.addFrame(frame1, 200);
                Anim.addFrame(frame2, 200);
                Anim.addFrame(frame3, 200);
                Anim.addFrame(frame4, 200);
                Anim.addFrame(frame5, 200);
                Anim.addFrame(frame6, 200);
                Anim.setOneShot(false);
                view.setBackgroundDrawable(Anim);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {

                    public void run() {

                        Anim.start();

                    }
                }, 100);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
}

}

這是一個例子: 檢查這個

看來您必須在 XML 中編寫 animation 持續時間,例如:

<item android:drawable="@drawable/wheel0" android:duration="50" />

希望能幫助到你。

編輯::也檢查鏈接。

在搜索並做了很多在線工作之后,我相信我不能在逐幀 animation 中給出兩個后續幀之間的延遲時間,我也無法同時實現縮放和逐幀。所以我執行了另一種方法如下

    b=AnimationUtils.loadAnimation(this, R.anim.scalealpha);
a=AnimationUtils.loadAnimation(this, R.anim.scalealpha);
        c=AnimationUtils.loadAnimation(this, R.anim.scalealpha);
        e=AnimationUtils.loadAnimation(this, R.anim.scalealpha);

                a=AnimationUtils.loadAnimation(this,R.anim.scalealpha);
                iv3.setImageResource(R.drawable.one);
                iv3.startAnimation(a);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable()
            {

                public void run()
                {

                       iv3.setImageResource(R.drawable.two);  
                       iv3.startAnimation(b);
                       Handler handler = new Handler();
                        handler.postDelayed(new Runnable()
                        {
                            public void run()
                            {

                                   iv3.setImageResource(R.drawable.three);
                                   iv3.startAnimation(c);
                                   Handler handler = new Handler();
                                    handler.postDelayed(new Runnable()
                                    {
                                        public void run()
                                        {

                                               iv3.setImageResource(R.drawable.go);                                                                  
                                               iv3.startAnimation(e);
                                        }}, 3000);

                            }}, 1000);


                }}, 1000);  

如果有人知道比這更好的方法,請告訴我,我很高興知道............感謝誰試圖解決這個問題......

暫無
暫無

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

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