国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)使用Handler實現(xiàn)圖片輪播功能示例

2019-10-22 18:28:17
字體:
供稿:網(wǎng)友

本文實例講述了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>

希望本文所述對大家Android程序設計有所幫助。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 民和| 开阳县| 莫力| 金堂县| 中卫市| 英吉沙县| 武陟县| 阿拉善右旗| 新巴尔虎右旗| 星座| 铅山县| 齐河县| 大丰市| 利津县| 韶山市| 凤台县| 井陉县| 克拉玛依市| 天等县| 常熟市| 宝坻区| 广丰县| 罗定市| 高碑店市| 长白| 和政县| 兴隆县| 黄浦区| 常山县| 曲周县| 北碚区| 柯坪县| 织金县| 西盟| 临夏市| 新余市| 天津市| 浙江省| 襄汾县| 荃湾区| 肇庆市|