Android开启新线程实现电子广告牌项目
作者:光仔December 时间:2023-02-10 20:47:10
利用之前学过的多线程处理技术,我们做一个开启新线程实现电子广告牌的项目
界面布局文件,加入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[] title=new String[]{"美国进口葡萄酒","乐享移动4G时代",
"江山御景楼盘开售","大学康城新区现房","五粮液精品"};
@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);//创建新线程
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中,再发生
* 消息,最后让线程休眠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);//发送消息
try {
Thread.sleep(2000);//让线程休眠2秒钟
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();//输出异常信息
}
}
}
}
显示效果如图
来源:http://blog.csdn.net/acmman/article/details/46375661
标签:Android,广告牌
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android运动健康睡眠自定义控件的实现
2021-07-17 22:52:35
![](https://img.aspxhome.com/file/2023/4/138414_0s.png)
springboot-jta-atomikos多数据源事务管理实现
2022-08-29 19:45:47
![](https://img.aspxhome.com/file/2023/8/125558_0s.png)
C# 解决datagridview控件显示大量数据拖拉卡顿问题
2022-03-21 12:52:24
![](https://img.aspxhome.com/file/2023/6/100216_0s.png)
浅谈Android Studio 4.1 更新内容
2021-09-17 11:27:30
![](https://img.aspxhome.com/file/2023/5/138735_0s.gif)
SpringBoot redis分布式缓存实现过程解析
2023-10-10 22:32:25
![](https://img.aspxhome.com/file/2023/1/58541_0s.png)
android教程之使用popupwindow创建菜单示例
2023-01-24 22:08:10
C#基础知识之GetType与typeof的区别小结
2022-04-06 21:59:34
![](https://img.aspxhome.com/file/2023/0/96330_0s.png)
学习Java九大内置对象
2023-10-13 01:10:10
Android Studio快捷键生成TAG、Log.x日志输出介绍
2022-03-14 02:10:37
spring整合JMS实现同步收发消息(基于ActiveMQ的实现)
2022-06-09 06:00:36
![](https://img.aspxhome.com/file/2023/1/66701_0s.png)
JavaWeb简单文件上传流程的实战记录
2023-04-02 09:14:59
![](https://img.aspxhome.com/file/2023/9/71889_0s.png)
Java实现画图 给图片底部添加文字标题
2023-03-28 23:23:06
![](https://img.aspxhome.com/file/2023/3/79823_0s.jpg)
Android 广播大全 Intent Action 事件详解
2021-09-10 12:40:15
java迭代器原理及迭代map的四种方式
2021-08-27 12:04:30
c#实现用SQL池,多线程定时批量执行SQL语句的方法
2023-12-25 01:03:55
使用java实现猜拳小游戏
2023-11-25 17:30:16
![](https://img.aspxhome.com/file/2023/4/79224_0s.jpg)
Android RecyclerView添加上拉加载更多功能
2022-08-02 00:03:51
![](https://img.aspxhome.com/file/2023/8/94288_0s.gif)
Java实现发红包功能
2023-12-09 10:33:03
深入分析C#中的异步和多线程
2021-08-15 19:29:49
关于C++数组中重复的数字
2023-01-21 03:29:43