码迷,mamicode.com
首页 > 其他好文 > 详细

用Handler图片轮播练习

时间:2016-04-09 00:12:51      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }


    }

 

用Handler图片轮播练习

标签:

原文地址:http://www.cnblogs.com/youshashuosha/p/5370392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!