本文實(shí)例講述了Android使用Handler實(shí)現(xiàn)圖片輪播功能。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括網(wǎng)站制作、成都網(wǎng)站制作、電商網(wǎng)站制作開發(fā)、小程序開發(fā)、微信營銷、系統(tǒng)平臺開發(fā),與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!
提前定義好一個(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é)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
網(wǎng)頁題目:Android開發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能示例
網(wǎng)頁網(wǎng)址:http://www.chinadenli.net/article30/gsphpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、軟件開發(fā)、定制網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)