您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关怎么在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。