Android開發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能示例
本文實(shí)例講述了Android使用Handler實(shí)現(xiàn)圖片輪播功能。分享給大家供大家參考,具體如下:
提前定義好一個(gè)Runnable接口,然后用handler調(diào)用。
Mainactivity代碼如下:
package com.example.handle_01; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView; private Handler handler = new Handler(); private ImageView imageView; private int[]images = { R.mipmap.image1,R.mipmap.image2 }; private int index; private MyRunnable myRunnable = new MyRunnable(); class MyRunnable implements Runnable{ @Override public void run() { index++; index = index%2; imageView.setImageResource(images[index]); handler.postDelayed(myRunnable,1000); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); imageView = (ImageView) findViewById(R.id.imageView); handler.post(myRunnable); /* new Thread(){ public void run(){ try { Thread.sleep(1000); //post方法xiugai UI handler.post(new Runnable() { @Override public void run() { //在UI線程中執(zhí)行 textView.setText("update thread"); } }); // textView.setText("update thread"); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); */ } }
activity_main代碼如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.handle_01.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_centerInParent="true" /> </RelativeLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android開發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能
- Android使用開源框架ANDROID-IMAGE-INDICATOR實(shí)現(xiàn)圖片輪播部署
- Android線程實(shí)現(xiàn)圖片輪播
- Android ViewPager實(shí)現(xiàn)圖片輪播效果
- Android自動(dòng)播放Banner圖片輪播效果
- Android客戶端實(shí)現(xiàn)圖片輪播控件
- Android實(shí)現(xiàn)廣告圖片輪播效果
- Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼
- Android實(shí)現(xiàn)圖片輪播效果
- Android自定義圖片輪播Banner控件使用解析
相關(guān)文章
Android雙擊返回鍵退出程序的實(shí)現(xiàn)方法
這篇文章主要介紹了Android雙擊返回鍵退出程序的實(shí)現(xiàn)方法,是Android程序開發(fā)中非常具有實(shí)用價(jià)值的重要技巧,需要的朋友可以參考下2014-09-09詳解Android JetPack之LiveData的工作原理
這篇文章主要介紹了詳解Android JetPack之LiveData的工作原理,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03Android自定義圓環(huán)倒計(jì)時(shí)控件
這篇文章主要為大家詳細(xì)介紹了Android自定義圓環(huán)倒計(jì)時(shí)控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android ListView出現(xiàn)異常解決辦法
這篇文章主要介紹了Android ListView出現(xiàn)異常ListView:The content of the adapter has changed but ListView did not receive a notification解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11android 為應(yīng)用程序創(chuàng)建桌面快捷方式技巧分享
手機(jī)裝的軟件過多,找起來很不方便,所以在主頁面有一個(gè)快捷方式的話會(huì)很不錯(cuò)的,本文將介紹如何實(shí)現(xiàn),需要了解跟多的朋友可以參考下2012-12-12Android實(shí)現(xiàn)ListView左右滑動(dòng)刪除和編輯
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ListView左右滑動(dòng)刪除和編輯的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05