這篇文章將為大家詳細(xì)講解有關(guān)怎么在Andriod使用多線程實(shí)現(xiàn)一個輪播圖功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
為西疇等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及西疇網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、西疇網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
MainActivity了初始化控件。
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); } }
關(guān)于怎么在Andriod使用多線程實(shí)現(xiàn)一個輪播圖功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。