国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開啟新線程實現電子廣告牌項目

2019-10-22 18:18:36
字體:
來源:轉載
供稿:網友

利用之前學過的多線程處理技術,我們做一個開啟新線程實現電子廣告牌的項目

界面布局文件,加入ImageView圖片控件,用于顯示一個圖片,一個TextView控件,用于顯示廣告說明語。

res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:id="@+id/linearLayout1"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:orientation="vertical" >   <ImageView android:id="@+id/imageView1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:padding="10dp"     android:src="@drawable/hint"/>   <TextView android:id="@+id/TextView1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:padding="10dp"/> </LinearLayout> 

在res/drawable下加入幾張廣告圖片(ad1.jpg、ad2.jpg、ad3.jpg、ad4.jpg、ad5.jpg)

在主界面中,產生隨機數不斷的變換在ImageView空間上的圖片資源文件,來實現一個類似于幻燈片的電子廣告牌
MainActivity:

package com.example.test;    import java.util.Random;   import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ImageView; import android.widget.TextView;   public class MainActivity extends Activity implements Runnable{     private ImageView imageView;    private TextView textView;    private Handler handler;    private int[] path=new int[]{R.drawable.ad1,R.drawable.ad2,        R.drawable.ad3,R.drawable.ad4,R.drawable.ad5};    private String[]       "江山御景樓盤開售","大學康城新區(qū)現房","五糧液精品"};   @Override    public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);          imageView=(ImageView)findViewById(R.id.imageView1);     textView=(TextView)findViewById(R.id.TextView1);          Thread t=new Thread(this);//創(chuàng)建新線程     t.start();//開啟線程     //實例化一個Handler對象     handler=new Handler(){         @Override       public void handleMessage(Message msg) {         //更新UI         if(msg.what==0x101){           textView.setText(msg.getData().getString("title"));//設置標題           imageView.setImageResource(path[msg.arg1]);//設置要顯示的圖片         }         super.handleMessage(msg);       }            };   }   /*    * 判斷當前線程是否被中斷,如果沒有被中斷,    * 則首先產生一個隨機數,然后獲取一個Message,并將要顯示    * 的廣告圖片的索引值和對應標題保存到該Message中,再發(fā)生    * 消息,最后讓線程休眠2秒鐘    * */   @Override   public void run() {     int index=0;     while(!Thread.currentThread().isInterrupted()){       index=new Random().nextInt(path.length);//產生一個隨機數       Message m=handler.obtainMessage();//獲取一個Message       m.arg1=index;//保存要顯示廣告圖片的索引值       Bundle bundle=new Bundle();//獲取Bundle對象       m.what=0x101;//設置消息標識       bundle.putString("title",title[index]);//保存標題       m.setData(bundle);//將Bundle對象保存到Message中       handler.sendMessage(m);//發(fā)送消息       try {         Thread.sleep(2000);//讓線程休眠2秒鐘       } catch (InterruptedException e) {         // TODO Auto-generated catch block         e.printStackTrace();//輸出異常信息       }     }   }   } 

 顯示效果如圖

Android,電子廣告牌,項目,Android電子廣告牌,Android廣告牌

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 尼勒克县| 五台县| 尚志市| 巴彦县| 大姚县| 邵武市| 肥乡县| 仲巴县| 呼和浩特市| 顺昌县| 鞍山市| 鄄城县| 宿迁市| 历史| 夹江县| 寿光市| 交口县| 凭祥市| 奈曼旗| 武隆县| 宝丰县| 察隅县| 普兰县| 靖远县| 博罗县| 那坡县| 宜章县| 辉县市| 老河口市| 揭西县| 清新县| 曲麻莱县| 房产| 兰州市| 萝北县| 涪陵区| 章丘市| 黎平县| 游戏| 澄迈县| 梅州市|