本文实例为大家分享了Android实现自动播放图片功能的具体代码,供大家参考,具体内容如下
第一种方式:回退到第一张
效果图
第二种方式:无限循环
效果图:
源代码
逻辑代码--MainActivity
package com.example.week4_day4_viewpager2; import java.util.ArrayList; import java.util.List; import android.R.integer; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.support.v4.view.ViewPager; import android.view.Menu; import android.widget.ImageView; public class MainActivity extends Activity { private ViewPager viewPager; // 数据源 private int[] images = { R.drawable.image1, R.drawable.image2, R.drawable.image3 }; // 存放数据源的集合 Listlist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewpager); // 实例化list list = new ArrayList (); for (int i = 0; i
逻辑代码--自定义适配器
package com.example.week4_day4_viewpager2; import java.util.List; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class MyViewPagerAdapter extends PagerAdapter{ private Context context; private Listlist; public MyViewPagerAdapter(Context context, List list) { super(); this.cOntext= context; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE;//设置viewPager里有N条数据 //return list.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { //假设有3张照片,走到id=2时,走id=0的,所以取余 container.addView(list.get(position%list.size())); return list.get(position%list.size()); //return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(list.get(position%list.size())); //container.removeView(list.get(position)); //super.destroyItem(container, position, object); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } }
布局文件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。