Android-ViewPager的自动滑动
- 在Android-ViewPager循环滑动的基础上添加了自动滑动
- 添加了一个广播,用于滑动到下一张图片
- 添加了一个服务,用于向广播发送消息,表示该滑动图片了。。。
- 效果图
添加的服务代码如下
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ViewPagerSlideService extends Service{
private Intent intent;
public static boolean isRun = true;
@Override
public void onCreate() {
super.onCreate();
intent = new Intent("com.jz.viewpager.CHANGPAGERRECEIVER");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread()
{
public void run()
{
while(isRun)
{//每隔三秒向广播发送消息,滑动到下一张图片
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sendBroadcast(ViewPagerSlideService.this.intent);
}
};
}.start();
return super.onStartCommand(intent, flags, startId);
}
}
改变后的Activity代码如下
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private ViewPager viewPager;
private ImageView[] tips;// 小点
private ImageView[] imageViews;//显示的图片
private int[] imgIds;//图片的资源id
private Intent intent;//启动服务的意图
private ChangePagerReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);//获取装小点的线性布局
viewPager = (ViewPager) findViewById(R.id.viewPager);
// 图片资源
imgIds = new int[] { R.drawable.p20, R.drawable.p21, R.drawable.p22};
tips = new ImageView[imgIds.length];
for (int i = 0; i < tips.length; i++) {
ImageView img = new ImageView(this);
tips[i] = img;
if (i == 0) {
tips[i].setImageResource(R.drawable.yuanquan_up2);
} else {
tips[i].setImageResource(R.drawable.yuanquan_down2);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
params.width = 20;//小点宽度
params.height = 20;//小点高度
params.leftMargin = 5;//小点的左边margin距离
params.rightMargin = 5;//小点的右边margin距离
group.addView(img, params);//将小点添加到线性布局中
}
//处理了,当图片小于等于4张的时候报的错
imageViews = imgIds.length <= 3 ? new ImageView[imgIds.length * 4] : new ImageView[imgIds.length];
for (int i = 0; i < imageViews.length; i++) {
imageViews[i] = new ImageView(this);
imageViews[i].setImageResource(imgIds[i % imgIds.length]);
//按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
//ImageView.ScaleType参考:
imageViews[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
}
// 设置Adapter
viewPager.setAdapter(new MyAdapter());
// 设置监听,主要是设置滑动时点点的图片的改变
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
setImageBack(arg0 % imgIds.length);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
// 设置ViewPager的默认项, 设置为长度的100倍,这样子开始就能往左滑动
viewPager.setCurrentItem((imageViews.length) * 100);
添加的部分:调用这个方法,启动服务并且添加广播
startServiceAndBroadcast();//启动服务来发送广播让ViewPager滑动
}
/**
* 启动服务创建广播
*/
private void startServiceAndBroadcast()
{
ViewPagerSlideService.isRun = true;//线程可以运行
receiver = new ChangePagerReceiver();//实例化广播接收器
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.jz.viewpager.CHANGPAGERRECEIVER");
registerReceiver(receiver, intentFilter);//添加广播的代码注册
intent = new Intent(MainActivity.this, ViewPagerSlideService.class);
startService(intent);//启动服务
}
/**
*
* 设置小点的背景
*
*/
private void setImageBack(int selectItem) {
for (int i = 0; i < tips.length; i++) {
if (i == selectItem) {
tips[i].setImageResource(R.drawable.yuanquan_up2);
} else {
tips[i].setImageResource(R.drawable.yuanquan_down2);
}
}
}
/**
*
* ViewPager适配器
*
*/
public class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(imageViews[position % imageViews.length]);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(imageViews[position % imageViews.length], 0);
return imageViews[position % imageViews.length];
}
}
添加的部分:添加的广播接收器,用于切换到下一张图片
/**
*
* 接收广播让ViewPager动态改变
*
*/
public class ChangePagerReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
int position = viewPager.getCurrentItem();
viewPager.setCurrentItem(position + 1, true);
}
}
添加的部分:当页面被销毁时
@Override
protected void onDestroy() {
super.onDestroy();
ViewPagerSlideService.isRun = false;
stopService(intent);
unregisterReceiver(receiver);
}
}
我的感受
- 常常有自动滑动的ViewPager,自己就想着添加一个
- 也可以直接在Activity中添加一个线程来控制滑动,这样来的还挺方便快捷的(@ ̄ー ̄@),感觉我好像想多了给弄麻烦了。。。
想说的话
- 博客坚持写,今后学习了新的东西就在这里记录一下,以便今后回顾,也希望小小笔记能帮助你们
- 若内容有什么地方不对、不清楚,还望吐槽,希望大家能一起成长
- 来一个:枯燥无味中寻找成功的快感