Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > android 多线程实现轮播图片

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

作者:笑对空

这篇文章主要介绍了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);
 }
}

您可能感兴趣的文章:
阅读全文