欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Andriod使用多线程实现轮播图片

程序员文章站 2023-12-09 20:10:15
效果图: 布局文件里包括了一个imageview和两个button控件,而在mainactivity了初始化控件。

效果图:

Andriod使用多线程实现轮播图片

布局文件里包括了一个imageview和两个button控件,而在mainactivity了初始化控件。

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.administrator.chinesezodiacapplication.mainactivity">
 <imageview
  android:id="@+id/imgnet"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"
  android:src="@drawable/zi"
  />
 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <button
   android:id="@+id/btnstart"
   android:layout_width="0dp"
   android:layout_weight="1"
   android:layout_height="wrap_content"
   android:text="开始播放"
   android:textsize="16dp"
   />
  <button
   android:id="@+id/btnpause"
   android:layout_width="0dp"
   android:layout_weight="1"
   android:layout_height="wrap_content"
   android:text="暂停播放"
   android:textsize="16dp"
   />
 </linearlayout>
</linearlayout>
public class mainactivity extends appcompatactivity {
 private button btnstart;
 private button btnpause;
 private imageview imageview;
 private thread thread;
 private handler handler;
 private int msg_img_change = 1001;
 private boolean ispause = true;
 private string url1 ="https://qqpublic.qpic.cn/qq_public/0/0-2366261503-87ffa35d0792fec1048083429d12687e/900";
 private string url2 ="https://qqpublic.qpic.cn/qq_public/0/0-3097962509-f7cfddbde07dafac9568fe9abfe7153f/900";
 private string url3 ="https://qqpublic.qpic.cn/qq_public/0/0-2851260680-186f3c52c3ddcb6bbaee05aef83d89da/900?tp=webp";
 private string url4 ="https://qqpublic.qpic.cn/qq_public/0/0-2738303735-b0fd9aba222886fcfa325d1ad4db6156/900?tp=webp";
 private string url5 ="https://qqpublic.qpic.cn/qq_public/0/0-2776144047-cd42fe909edd232753d51c54329d7ba6/900?tp=webp";
 private string url6 ="https://qqpublic.qpic.cn/qq_public/0/0-2628537209-cfba5c4c446cc492775303478f5825a2/900?tp=webp";
 private string url7 ="https://qqpublic.qpic.cn/qq_public/0/0-3079871717-6b0b52bde99eb32e481f049c077a3e6d/900?tp=webp";
 private string url8 ="https://qqpublic.qpic.cn/qq_public/0/0-2906573180-bb6e88099d4db7c27354591d8620a6bb/900?tp=webp";
 private string url9 ="https://qqpublic.qpic.cn/qq_public/0/0-2365621724-14715472acea17237f988207a7ea862f/900?tp=webp";
 private string url10 ="https://qqpublic.qpic.cn/qq_public/0/0-2362096419-a1371db9f39e23b07061e1e4d5e4b995/900?tp=webp";
 private string url11 ="https://qqpublic.qpic.cn/qq_public/0/0-2473426908-4c9010d824ef29e08bf20c6e32d3502a/900?tp=webp";
 private string url12="https://qqpublic.qpic.cn/qq_public/0/0-3067218096-f5a001f06e9d0c9b91e15db4dff5fb7b/900?tp=webp";
 private string[] imgurls ={url1,url2,url3,url4,url5,url6,url7,url8,url9,url10,url11,url12};
 private int index = 0;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  initviews();
  initthread();
  inithandler();
 }
 private void inithandler() {
  handler = new handler(new handler.callback() {
   @override
   public boolean handlemessage(message message) {
    if(message.what ==msg_img_change){
     glide.with(mainactivity.this).load(imgurls[message.arg1]).into(imageview);
     return true;
    }
    return false;
   }
  });
 }
 private void initthread() {
  new thread(new runnable(){
   @override
   public void run() {
    while (true){
     if(!ispause){
      index =(index+1)%imgurls.length;
      message msg = message.obtain();
      msg.what = msg_img_change;
      msg.arg1 =index;
      handler.sendmessage(msg);
     }
     try {
      thread.sleep(2000);
     } catch (interruptedexception e) {
      e.printstacktrace();
     }
    }
   }
  }).start();
 }
 private void initviews() {
  initimageview();
  initbutton();
 }
 private void initbutton() {
  btnstart = (button) findviewbyid(r.id.btnstart);
  btnstart.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
    ispause = false;
   }
  });
  btnpause = (button) findviewbyid(r.id.btnpause);
  btnpause.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
    ispause = true;
   }
  });
 }
 private void initimageview() {
  imageview = (imageview) findviewbyid(r.id.imgnet);
 }
}