• 用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);
        }
    
    
        }
  • 相关阅读:
    asp .net 页面回车触发button 按钮事件
    Asp.netPost&Get转
    2012.8.16近期总结,1模态窗口回传重新被弹出2,清空缓存 3,
    面试感言
    2012.6.27近期总结,1.sql字符串转换(cast,CONVERT )调用wcf服务,关闭模态窗口刷新付页面
    self
    空指针
    枚举和结构体
    typedef
    指针
  • 原文地址:https://www.cnblogs.com/youshashuosha/p/5370392.html
Copyright © 2020-2023  润新知