Android 簡單動畫的製作以及遇到的問題

來源:互聯網
上載者:User
package wht.android.loading;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity{        public AnimationDrawable frameAnimation;    public TextView tv;    public ImageView iv;        public boolean flag = false;            public Handler handler = new Handler(){                public void handleMessage(Message msg){            StopAnimation();        }    };    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);                setContentView(R.layout.donghua);        iv = (ImageView) findViewById(R.id.imgeView);        iv.setBackgroundResource(R.anim.loading);        frameAnimation =  (AnimationDrawable) iv.getBackground();                frameAnimation.setOneShot(true); //為真的時候最後一針會停下            }        public void StopAnimation(){                flag = true;        System.out.println(flag);        frameAnimation.stop();        iv.setVisibility(View.GONE);        tv = (TextView)findViewById(R.id.text);        tv.setText("啟動成功!");            }        public void onWindowFocusChanged(boolean hasFocus) {                  super.onWindowFocusChanged(hasFocus);        frameAnimation.start();                new Thread(new Runnable(){            @Override            public void run() {                // TODO Auto-generated method stub                try {                    Thread.sleep(2000); // 線程休眠時為什麼主線程也不運行                                        handler.sendMessage(Message.obtain());                                    } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                            }                    }).start();            }    }

 其中在onCreate 中建立了線程然後sleep,出現了一個問題,那就是介面沒有顯示出來

 介面完全顯示出來要等到 onWindowFoucosChanged時才可以

還有一點,介面只能在UI主線程中更改,所以,我們要去實現handler機制來進行通訊。

 

 

關於animation的使用,還有一點就是xml的編寫

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false">    <item android:drawable="@drawable/loader_frame_1" android:duration="300" />    <item android:drawable="@drawable/loader_frame_2" android:duration="300" />    <item android:drawable="@drawable/loader_frame_3" android:duration="300" />    <item android:drawable="@drawable/loader_frame_4" android:duration="300" />    <item android:drawable="@drawable/loader_frame_5" android:duration="300" />    <item android:drawable="@drawable/loader_frame_6" android:duration="300" />       </animation-list>

其中包括每一幀的圖片,和持續的時間。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.