Android循环图片广告的实现
此文章是阅读大神博客之后的一些笔记,原文地址:http://blog.csdn.net/singwhatiwanna/article/details/46541225
1.解决循环定时轮换图片。
2.解决用户手动滑动和定时滑动的冲突问题
3.解决因为给getCount返回一个较大值机率性应用ANR问题
3.解决当进入应用position为0时不能向右滑问题
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity implements Runnable {
private static final String TAG = "MainActivity";
private ViewPager mBanner;
private BannerAdapter mBannerAdapter;
private ImageView[] mIndicators;
private Timer mTimer = new Timer();
private int mBannerPosition = 0;
private final int FAKE_BANNER_SIZE = 100;//传给适配器的条目数
private final int DEFAULT_BANNER_SIZE = 5;//实际的条目的数量
private boolean mIsUserTouched = false;
private int[] mImagesSrc = {//图片资源数组
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5
};
private TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
if (!mIsUserTouched) {//判断用户是否用手在滑动viewpager
mBannerPosition = (mBannerPosition + 1) % FAKE_BANNER_SIZE;//通过定时每次位置加1,直到加到100
runOnUiThread(MainActivity.this);//因为MainActivity直接实现了Runnable接口所以直接传MainActivity的对象
Log.d(TAG, "tname:" + Thread.currentThread().getName());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mTimer.schedule(mTimerTask, 5000, 5000);//开启定时
}
private void initView() {
mIndicators = new ImageView[] {//加载图片上的小点imageview控件,没个点都是一个imageview
(ImageView)findViewById(R.id.indicator1),
(ImageView)findViewById(R.id.indicator2),
(ImageView)findViewById(R.id.indicator3),
(ImageView)findViewById(R.id.indicator4),
(ImageView)findViewById(R.id.indicator5)
};
mBanner = (ViewPager) findViewById(R.id.banner);
mBannerAdapter = new BannerAdapter(this);
mBanner.setAdapter(mBannerAdapter);
mBanner.setOnPageChangeListener(mBannerAdapter);//设置viewpager的页面改变监听,因为适配器类实现此接口所以传适配器对象
mBanner.setOnTouchListener(new View.OnTouchListener() {//设置viewpager的滑动监听
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_MOVE) {
mIsUserTouched = true;
} else if (action == MotionEvent.ACTION_UP) {
mIsUserTouched = false;
}
return false;
}
});
}
private void setIndicator(int position) {
position %= DEFAULT_BANNER_SIZE;
for(ImageView indicator : mIndicators) {
indicator.setImageResource(R.drawable.indicator_unchecked);//设置全部点为未选择状态
}
mIndicators[position].setImageResource(R.drawable.indicator_checked);//根据传过来的position设置为选中状态
}
@Override
public void run() {//定时后要执行的代码
if (mBannerPosition == FAKE_BANNER_SIZE - 1) {
mBanner.setCurrentItem(DEFAULT_BANNER_SIZE - 1, false);//此方法传false时,比如从第五张到第一张会直接跳到第一张,ture时会从第五张快速的按照4,3,2,1的显示。
} else {
mBanner.setCurrentItem(mBannerPosition);
}
}
@Override
protected void onDestroy() {
mTimer.cancel();//取消定时
super.onDestroy();
}
private class BannerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
private LayoutInflater mInflater;
public BannerAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return FAKE_BANNER_SIZE;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position %= DEFAULT_BANNER_SIZE;
View view = mInflater.inflate(R.layout.item, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
imageView.setImageResource(mImagesSrc[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);//销毁看不见的view
}
@Override
public void finishUpdate(ViewGroup container) {//此方法是当viewpager改变或发生点击的时候调用
int position = mBanner.getCurrentItem();
//此方法当position为零或为最大时
Log.d(TAG, "finish update before, position=" + position);
if (position == 0) {
position = DEFAULT_BANNER_SIZE;//处理当位置为零的时候不能向右滑的问题
mBanner.setCurrentItem(position, false);//为零时设置position为图片总张数
} else if (position == FAKE_BANNER_SIZE - 1) {
position = DEFAULT_BANNER_SIZE - 1;
mBanner.setCurrentItem(position, false);
}
Log.d(TAG, "finish update after, position=" + position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mBannerPosition = position;
setIndicator(position);//改变viewpager上面点的状态
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
}