您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在Andriod使用多線程實現一個輪播圖功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
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); } }
關于怎么在Andriod使用多線程實現一個輪播圖功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。