android中利用ImageView 圖片迴圈播放

來源:互聯網
上載者:User

xml

 代碼如下 複製代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout" >
 
</LinearLayout>

主檔案:

 代碼如下 複製代碼

public class picBarActivity extends Activity{
        private LinearLayout linearLayout;
        private picBar myView;
         
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.pic_progress);
                 
                linearLayout = (LinearLayout)this.findViewById(R.id.linearLayout);
                myView = new picBar(this);
                linearLayout.addView(myView);
        }
}

View檔案:

 代碼如下 複製代碼


public class picBar extends View{          
 
        int COMPONENT_WIDTH;//控制項的寬度  
        int COMPONENT_HEIGHT;//控制項的高度  
        boolean initflag = false;//是否已經初始化圖片  
        Bitmap[] bmp;//用來存放圖片的數組  
        int currIndex = 0;//當前播放圖片的ID  
        int[] bitmapId;//圖片編號ID  
        boolean workFlag = true;//播放圖片的線程標識位  
 
        public picBar(Context context) {
                super(context);
                 
                //首先,要播放圖片,就先要有圖片,那就先給各個圖片編號吧,這裡的圖片資源存放在了res下的drawable檔案夾下了  
                bitmapId =new int[]{R.drawable.loading1_03, R.drawable.loading2_03, R.drawable.loading3_03}; 
                 
                //好了,圖片的編號現在已經搞定了,接下來該幹什麼呢?對,應該將資源裡的圖片塞進Bitmap數組了,那麼我們先來確定將要播放的圖片的數量,即Bitmap數組的長度
                bmp = new Bitmap[bitmapId.length];//這裡不要直接將數值賦給bmp,因為我們可能會不定期地更換圖片資源,這樣我們就要修改多處代碼,而我們這樣根據  
 
                //圖片的ID來確定圖片的數量,以減少不必要的麻煩,下面開始初始化圖片,我們將初始化圖片放在一個函數裡  
                initBitmap();//圖片初始化完畢  
 
                //圖片初始化完畢了,接下來我們要做的就是播放圖片了,但是播放圖片之前,我們有一個問題,就是怎樣讓圖片實現迴圈播放?這裡我們另開一個新的線程來定時更改  
 
                //要播放的圖片的ID,以實現圖片的迴圈播放,要實現迴圈播放圖片的功能,我們需要覆寫onDraw函數,首先,我們來新開一個線程  
 
                new Thread(){  
 
                        //重寫run方法  
                        public void run(){
                                // TODO Auto-generated method stub  
                                while(workFlag)//一直執行這個迴圈(死迴圈)  
                                {  
                                        currIndex = (currIndex+1)%bitmapId.length;//更改圖片的ID  
                                        picBar.this.postInvalidate();//重新整理螢幕,導致螢幕重繪  
                                        try
                                        {  
                                                Thread.sleep(3000);//到此處暫停3秒鐘,然後繼續執行run函數,即實現每隔3秒鐘重新整理螢幕一次  
                                        } 
                                        catch (InterruptedException e)   
                                        {  
                                                // TODO Auto-generated catch block  
                                                e.printStackTrace();  
                                        } 
                                } 
                        } 
                }.start(); 
        }
 
        //初始化圖片  
 
        public void initBitmap()  
 
        {  
 
                //擷取資源圖片  
 
                Resources res = this.getResources();  
 
                for(int i=0;i<bitmapId.length;i++){  
                        bmp<I> = BitmapFactory.decodeResource(res, bitmapId<I>);  
                } 
        } 
 
        //覆寫onDraw方法  
        @Override
        protected void onDraw(Canvas canvas)   
        {  
                // TODO Auto-generated method stub  
                super.onDraw(canvas);  
                if(!initflag)//檢查是偶已經擷取控制項的寬和高,如果沒有,那麼就擷取控制項的寬和高  
                {  
                        COMPONENT_WIDTH = this.getWidth();  
                        COMPONENT_HEIGHT = this.getHeight();  
                        initflag = true;  
                } 
                Paint p = new Paint();
                canvas.drawBitmap(bmp[currIndex], 0, 0, p);//繪製圖片  
        } 
 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.