在Drawable目录下创建资源文件 使用shape标签画出背景圆点与当前圆点
背景圆点:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="@android:color/darker_gray" /> </shape>当前圆点:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="#f00" /> </shape>布局界面:
LinearLayout:用来在代码中添加背景点
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="20dp" > <LinearLayout android:id="@+id/ll_point_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > </LinearLayout> <View android:id="@+id/view_red_point" android:layout_width="10dp" android:layout_height="10dp" android:background="@drawable/shape_point_red" /></RelativeLayout> 代码中:先画背景点:
for (int i = 0; i < list_photo.size(); i++) { View point = new View(this); point.setBackgroundResource(R.drawable.shape_point_gray);// 设置引导页默认圆点 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10)); if (i > 0) { params.leftMargin = DensityUtils.dp2px(this, 10);// 设置圆点间隔 } point.setLayoutParams(params);// 设置圆点的大小 ll_point_group.addView(point);// 将圆点添加给线性布局} // 获取视图树, 对layout结束事件进行监听ll_point_group.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { // 当layout执行结束后回调此方法 @Override public void onGlobalLayout() { System.out.println("layout 结束"); ll_point_group.getViewTreeObserver() .removeGlobalOnLayoutListener(this); mPointWidth = ll_point_group.getChildAt(1).getLeft() - ll_point_group.getChildAt(0).getLeft(); System.out.println("圆点距离:" + mPointWidth); } });对图片使用点击事件的监听避免点击后不会循环播放
class MyTouchListener implements View.OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println("按下"); mHandler.removeCallbacksAndMessages(null);// 删除Handler中的所有消息 break; case MotionEvent.ACTION_CANCEL: System.out.println("事件取消"); mHandler.sendEmptyMessageDelayed(0, 3000); break; case MotionEvent.ACTION_UP: System.out.println("抬起"); mHandler.sendEmptyMessageDelayed(0, 3000); break; default: break; } return true; }}重写viewpager的滑动事件实时改变红点的位置
class MyPageListener implements ViewPager.OnPageChangeListener { // 滑动事件 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // System.out.println("当前位置:" + position + ";百分比:" + positionOffset // + ";移动距离:" + positionOffsetPixels); int len = (int) (mPointWidth * positionOffset) + position * mPointWidth; RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRedPoint .getLayoutParams();// 获取当前红点的布局参数 params.leftMargin = len;// 设置左边距 viewRedPoint.setLayoutParams(params);// 重新给小红点设置布局参数 }定时循环播放viewpager的图片
// 自动轮播条显示if (mHandler == null) { mHandler = new Handler() { public void handleMessage(android.os.Message msg) { int currentItem = viewPager.getCurrentItem(); if (currentItem < list_photo.size() - 1) { currentItem++; } else { currentItem = 0; } viewPager.setCurrentItem(currentItem);// 切换到下一个页面 mHandler.sendEmptyMessageDelayed(0, 3000);// 继续延时3秒发消息, // 形成循环 }; }; mHandler.sendEmptyMessageDelayed(0, 3000);// 延时3秒后发消息}
时间: 2024-12-13 19:58:16