Android基于定时器的图片轮播

时间:2021-01-18 00:09:43

Android基于定时器的图片轮播


很简单的一个功能,所以代码并不多。
新键一个项目,然后里面mipmap里放几张图片,然后看着代码很快就能实现了,
更过功能你们自己添加吧,有问题的在下面回复。

代码块

activity_main,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"
tools:context="xinge.com.xiankezu.zzxc.androidimageshuffling.MainActivity">

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="200dp" />


</RelativeLayout>

主代码


package xinge.com.xiankezu.zzxc.androidimageshuffling;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private ViewPager viewpager;
private int[] imgs = {R.mipmap.t1, R.mipmap.t2, R.mipmap.t3, R.mipmap.t4, R.mipmap.t5}; //图片集合
private List<ImageView> imageViewList;
protected int lastPosition;

private TimeCount time;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initData();
}


private void initViews() { //加载布局
viewpager = (ViewPager) findViewById(R.id.viewpager);
}

private void initData() { //加载数据
time = new TimeCount(3000,1000); //3秒倒计时
imageViewList = new ArrayList<>();
for (int img : imgs) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(img);
imageViewList.add(imageView);
}



//触摸的时候停止定时器,松开就开始计时
viewpager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
time.start();
} else {
time.cancel();
}
return false;
}
})
;

MyPageAdapter adapter = new MyPageAdapter();
viewpager.setAdapter(adapter);
viewpager.setCurrentItem(Integer.MAX_VALUE / 2); //默认在中间,使用户看不到边界
time.start();
}


Handler handler = new Handler(){ //时间到就自动进行轮播
@Override
public void handleMessage(Message msg) {
viewpager.setCurrentItem(viewpager.getCurrentItem() + 1);
time.start();
}
};

class MyPageAdapter extends PagerAdapter {

@Override
public int getCount() {
return Integer.MAX_VALUE;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(imageViewList.get(position % imageViewList.size()));
return imageViewList.get(position % imageViewList.size());
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}

/* 定义一个倒计时的内部类 */
class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
}

@Override
public void onFinish() {// 计时完毕时触发
time.cancel();
handler.sendEmptyMessage(0); //简单发了个消息过去
}

@Override
public void onTick(long millisUntilFinished) {// 计时过程显示
}

}
}