Android開啟新線程實(shí)現(xiàn)電子廣告牌項(xiàng)目
利用之前學(xué)過的多線程處理技術(shù),我們做一個(gè)開啟新線程實(shí)現(xiàn)電子廣告牌的項(xiàng)目
界面布局文件,加入ImageView圖片控件,用于顯示一個(gè)圖片,一個(gè)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)
在主界面中,產(chǎn)生隨機(jī)數(shù)不斷的變換在ImageView空間上的圖片資源文件,來實(shí)現(xiàn)一個(gè)類似于幻燈片的電子廣告牌
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[] title=new String[]{"美國進(jìn)口葡萄酒","樂享移動(dòng)4G時(shí)代",
"江山御景樓盤開售","大學(xué)康城新區(qū)現(xiàn)房","五糧液精品"};
@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();//開啟線程
//實(shí)例化一個(gè)Handler對(duì)象
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
//更新UI
if(msg.what==0x101){
textView.setText(msg.getData().getString("title"));//設(shè)置標(biāo)題
imageView.setImageResource(path[msg.arg1]);//設(shè)置要顯示的圖片
}
super.handleMessage(msg);
}
};
}
/*
* 判斷當(dāng)前線程是否被中斷,如果沒有被中斷,
* 則首先產(chǎn)生一個(gè)隨機(jī)數(shù),然后獲取一個(gè)Message,并將要顯示
* 的廣告圖片的索引值和對(duì)應(yīng)標(biāo)題保存到該Message中,再發(fā)生
* 消息,最后讓線程休眠2秒鐘
* */
@Override
public void run() {
int index=0;
while(!Thread.currentThread().isInterrupted()){
index=new Random().nextInt(path.length);//產(chǎn)生一個(gè)隨機(jī)數(shù)
Message m=handler.obtainMessage();//獲取一個(gè)Message
m.arg1=index;//保存要顯示廣告圖片的索引值
Bundle bundle=new Bundle();//獲取Bundle對(duì)象
m.what=0x101;//設(shè)置消息標(biāo)識(shí)
bundle.putString("title",title[index]);//保存標(biāo)題
m.setData(bundle);//將Bundle對(duì)象保存到Message中
handler.sendMessage(m);//發(fā)送消息
try {
Thread.sleep(2000);//讓線程休眠2秒鐘
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();//輸出異常信息
}
}
}
}
顯示效果如圖

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開屏布局
- Android利用ViewPager實(shí)現(xiàn)滑動(dòng)廣告板實(shí)例源碼
- Android實(shí)現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實(shí)現(xiàn)手動(dòng)滑動(dòng)循環(huán))
- Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)效果
- Android筆記之:App應(yīng)用之發(fā)布各廣告平臺(tái)版本的詳解
- Android實(shí)現(xiàn)廣告圖片輪播效果
- Android編程實(shí)現(xiàn)ListView頭部ViewPager廣告輪詢圖效果
- Android 破解視頻App去除廣告功能詳解及解決辦法總結(jié)
- Android新聞廣告條滾動(dòng)效果
- Android 應(yīng)用啟動(dòng)歡迎界面廣告的實(shí)現(xiàn)實(shí)例
相關(guān)文章
Android 如何獲取手機(jī)總內(nèi)存和可用內(nèi)存等信息
這篇文章主要介紹了Android系統(tǒng)檢測(cè)程序內(nèi)存占用各種方法,并對(duì)內(nèi)存信息的詳細(xì)介紹,需要的朋友可以參考下2016-07-07
Android自定義View實(shí)現(xiàn)拖拽效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android開發(fā)中Flutter組件實(shí)用技巧
這篇文章主要為大家介紹了Android開發(fā)中Flutter組件實(shí)用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Retrofit2.0 實(shí)現(xiàn)圖文(參數(shù)+圖片)上傳方法總結(jié)
本篇文章主要介紹了Retrofit2.0 實(shí)現(xiàn)圖文(參數(shù)+圖片)上傳方法總結(jié),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

