用Handler图片轮播练习

XML代码

<?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.dell.wodelianxi.Tupianlunbo">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image1"/> </RelativeLayout>

JAVA代码

package com.example.dell.wodelianxi;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView; import java.util.Timer;
import java.util.TimerTask; public class Tupianlunbo extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tupianlunbo);
final ImageView image1 = (ImageView) findViewById(R.id.image1); final int[] imageid = {R.drawable.touxiang1, R.drawable.touxiang2, R.drawable.touxiang13,
R.drawable.touxiang14, R.drawable.touxiang15, R.drawable.touxiang16,
R.drawable.touxiang17, R.drawable.touxiang18, R.drawable.touxiang19};
final Handler handler = new Handler() {
int i = 0; @Override
public void handleMessage(Message msg) {
super.handleMessage(msg); if (msg.what == 1) {
image1.setImageResource(imageid[i++ % imageid.length]);
} }
}; new Timer().schedule(new TimerTask() {
@Override
public void run()
{
handler.sendEmptyMessage(1);
}
},0,3000);
} }
上一篇:10款专为设计师打造的热门工具清单


下一篇:Oralce sysaux WRH$_ACTIVE_SESSION_HISTORY清理