Android開發(fā)使用Handler實現(xiàn)圖片輪播功能示例
本文實例講述了Android使用Handler實現(xiàn)圖片輪播功能。分享給大家供大家參考,具體如下:
提前定義好一個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ā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
相關(guān)文章
詳解Android JetPack之LiveData的工作原理
這篇文章主要介紹了詳解Android JetPack之LiveData的工作原理,幫助大家更好的理解和學習使用Android開發(fā),感興趣的朋友可以了解下2021-03-03
Android 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-11
android 為應用程序創(chuàng)建桌面快捷方式技巧分享
手機裝的軟件過多,找起來很不方便,所以在主頁面有一個快捷方式的話會很不錯的,本文將介紹如何實現(xiàn),需要了解跟多的朋友可以參考下2012-12-12
Android實現(xiàn)ListView左右滑動刪除和編輯
這篇文章主要為大家詳細介紹了Android實現(xiàn)ListView左右滑動刪除和編輯的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05

