Android开启新线程实现电子广告牌项目

 更新时间:2017年12月26日 09:48:06   作者:光仔December  
这篇文章主要为大家详细介绍了Android开启新线程实现电子广告牌项目,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

利用之前学过的多线程处理技术,我们做一个开启新线程实现电子广告牌的项目

界面布局文件,加入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();//输出异常信息 
      } 
    } 
  } 
  
} 

 显示效果如图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android 如何获取手机总内存和可用内存等信息

    Android 如何获取手机总内存和可用内存等信息

    这篇文章主要介绍了Android系统检测程序内存占用各种方法,并对内存信息的详细介绍,需要的朋友可以参考下
    2016-07-07
  • Android仿外卖购物车功能

    Android仿外卖购物车功能

    这篇文章主要为大家详细介绍了Android仿外卖购物车功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android adb 出错解决方法

    Android adb 出错解决方法

    本文主要介绍Android 中的adb,相信大家在开发过程中经常使用adb进行调试,这里主要说明adb 出错问题解决方法,希望能帮助有需要的同学
    2016-07-07
  • 深入了解Android IO的底层原理

    深入了解Android IO的底层原理

    这篇文章主要介绍了深入了解Android IO的底层原理,IO有缓冲与非缓冲 IO、直接与非直接 IO、阻塞与非阻塞 IO、同步与异步 IO等分类,具体详情感兴趣的小伙伴可以参考下面文章内容
    2022-06-06
  • Android自定义View实现拖拽效果

    Android自定义View实现拖拽效果

    这篇文章主要为大家详细介绍了Android自定义View实现拖拽效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • Android仿微信录制小视频

    Android仿微信录制小视频

    这篇文章主要为大家详细介绍了Android仿微信录制小视频,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • android贝塞尔曲线实现波浪效果

    android贝塞尔曲线实现波浪效果

    这篇文章主要为大家详细介绍了android贝塞尔曲线实现波浪效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Android常用布局使用技巧示例讲解

    Android常用布局使用技巧示例讲解

    本文详细介绍了Android常用布局,包括LinearLayout、RelativeLayout、FrameLayout、ConstraintLayout等,分析了它们的适用场景、优缺点和使用技巧,帮助开发者更好地选择和使用合适的布局,提升UI设计和开发效率
    2023-04-04
  • Android开发中Flutter组件实用技巧

    Android开发中Flutter组件实用技巧

    这篇文章主要为大家介绍了Android开发中Flutter组件实用技巧,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Retrofit2.0 实现图文(参数+图片)上传方法总结

    Retrofit2.0 实现图文(参数+图片)上传方法总结

    本篇文章主要介绍了Retrofit2.0 实现图文(参数+图片)上传方法总结,具有一定的参考价值,有兴趣的可以了解一下
    2017-08-08

最新评论