说明:
android定时器加for循环实现轮播图
效果:
step1:
package com.example.iosdialogdemo;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private final int Time = 1000; //时间间隔, 单位 ms
private Handler handler = new Handler();
private int num = 0; //用来观测重复执行
private int img[] = {1, 2, 3, 4, 5, 6, 7};
private String imgString[] = {"12345", "23456", "34567", "45671", "56712", "67123", "71234"};
private String nameString[] = {"刘备", "关羽", "张飞", "诸葛亮", "袁绍", "曹操", "孙权"};
private int mips[] = {R.mipmap.liubei, R.mipmap.guanyu, R.mipmap.zhangfei, R.mipmap.zhuge, R.mipmap.yuan, R.mipmap.caocao, R.mipmap.sun};
private TextView tv_img0, tv_img1, tv_img2, tv_img3, tv_img4;
private int[] tvIds = {R.id.tv_img0, R.id.tv_img1, R.id.tv_img2, R.id.tv_img3, R.id.tv_img4};
private TextView[] tvGroups = {tv_img0, tv_img1, tv_img2, tv_img3, tv_img4};
private ImageView iv_img0, iv_img1, iv_img2, iv_img3, iv_img4;
private int[] ivIds = {R.id.iv_img0, R.id.iv_img1, R.id.iv_img2, R.id.iv_img3, R.id.iv_img4};
private ImageView[] ivGroups = {iv_img0, iv_img1, iv_img2, iv_img3, iv_img4};
private int[] ages = {0, 1, 2, 3, 4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < tvIds.length; i++) {
tvGroups[i] = findViewById(tvIds[i]);
ivGroups[i] = findViewById(ivIds[i]);
tvGroups[i].setText(nameString[i]);
ivGroups[i].setImageResource(mips[i]);
}
System.out.println(Arrays.toString(img));
handler.postDelayed(new Runnable() {
@Override
public void run() {
handler.postDelayed(this, Time);
//每隔一段时间要重复执行的代码
System.out.println("第次执行");
num++;
int age = num % 7;
int sex = num % 5;
System.out.println(age);
System.out.println(nameString[age]);
tvGroups[sex].setText(String.valueOf(nameString[age]));
ivGroups[sex].setImageResource(mips[age]);
}
}, Time);
}
}
step2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_img0"
android:layout_width="130dp"
android:layout_height="70dp"
android:textSize="30sp"
/>
<TextView
android:id="@+id/tv_img1"
android:layout_width="130dp"
android:layout_height="70dp"
android:textSize="30sp"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_img2"
android:layout_width="100dp"
android:layout_height="70dp"
android:gravity="center"
android:textSize="30sp"
/>
<TextView
android:id="@+id/tv_img3"
android:gravity="center"
android:layout_width="130dp"
android:layout_height="70dp"
android:textSize="30sp"
/>
<TextView
android:id="@+id/tv_img4"
android:layout_width="130dp"
android:gravity="center"
android:layout_height="70dp"
android:textSize="30sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iv_img0"
android:layout_width="130dp"
android:layout_height="70dp"
android:scaleType="fitStart"
/>
<ImageView
android:id="@+id/iv_img1"
android:layout_width="130dp"
android:layout_height="70dp"
android:scaleType="fitStart"
/>
<ImageView
android:id="@+id/iv_img2"
android:layout_width="130dp"
android:layout_height="70dp"
android:scaleType="fitStart"
/>
<ImageView
android:id="@+id/iv_img3"
android:layout_width="130dp"
android:layout_height="70dp"
android:scaleType="fitStart"
/>
<ImageView
android:id="@+id/iv_img4"
android:layout_width="130dp"
android:layout_height="70dp"
android:scaleType="fitStart"
/>
</LinearLayout>
</LinearLayout>
end