先看看效果图:
activity_main.xml
<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="com.example.welcome.MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/mViewPager" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" > <com.example.welcome.PagerCursor android:id="@+id/pagerCursor" android:layout_width="fill_parent" android:layout_height="5dp" /> </FrameLayout> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" > <com.example.welcome.CustomeDotGroup android:id="@+id/ll_point_group" android:layout_width="fill_parent" android:layout_height="10dp" android:orientation="horizontal" > </com.example.welcome.CustomeDotGroup> <View android:id="@+id/red_point" android:layout_width="10dp" android:layout_height="10dp" android:background="@drawable/point_red" /> </FrameLayout> </RelativeLayout>
MainActivity
package com.example.welcome; import java.util.ArrayList; import android.app.Activity; 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.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { private ViewPager mViewPager; MyAdapter mAdapter; private ArrayList<ImageView> imageViewList; View red_point; private int lastDis; PagerCursor pagerCursor; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pagerCursor = (PagerCursor) findViewById(R.id.pagerCursor); red_point = findViewById(R.id.red_point); imageViewList = new ArrayList<ImageView>(); int[] imagesInt = {R.drawable.p1,R.drawable.p2,R.drawable.p3}; for (int i = 0; i < imagesInt.length; i++) { ImageView imageView = new ImageView(MainActivity.this); imageView.setBackgroundResource(imagesInt[i]); imageViewList.add(imageView); } mViewPager = (ViewPager) findViewById(R.id.mViewPager); mAdapter = new MyAdapter(); mViewPager.setAdapter(mAdapter); mViewPager.setOnPageChangeListener(new OnPageChangeListener() { private boolean isDragging; @Override public void onPageSelected(int arg0) { } @Override public void onPageScrolled(int position, float screenOffSet, int arg2) { TranslateAnimation animation = null; if (isDragging) { int dp2px = PhoneUtils.dp2px(MainActivity.this, 20f); int offSet = (int) (position*dp2px + dp2px*screenOffSet); animation = new TranslateAnimation(lastDis, offSet, 0f, 0f); animation.setDuration(200); animation.setFillAfter(true); red_point.startAnimation(animation); lastDis = offSet; } float[] screenSize = MeasureUtil.getScreenSize(MainActivity.this); float itemWidth = screenSize[0]/3; pagerCursor.SetOffSet(position,screenOffSet,itemWidth); } @Override public void onPageScrollStateChanged(int arg0) { switch (arg0) { case ViewPager.SCROLL_STATE_DRAGGING://滑动 isDragging = true; break; case ViewPager.SCROLL_STATE_IDLE://空闲 isDragging = false; break; default: break; } } }); } class MyAdapter extends PagerAdapter{ @Override public int getCount() { return imageViewList.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(imageViewList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imageViewList.get(position)); return imageViewList.get(position); } } }
CustomeDotGroup
package com.example.welcome; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; public class CustomeDotGroup extends LinearLayout { private Context context; public CustomeDotGroup(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; initRes(); } public CustomeDotGroup(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomeDotGroup(Context context) { this(context, null); } private void initRes() { int dotWidthOrHeight = PhoneUtils.dp2px(context, 10); for (int i = 0; i < 3; i++) { ImageView dotImageView = new ImageView(context); dotImageView.setBackgroundResource(R.drawable.point_normal); LayoutParams dotImageViewParams = new LayoutParams(dotWidthOrHeight, dotWidthOrHeight); if (i != 0) { dotImageViewParams.leftMargin = dotWidthOrHeight; } dotImageView.setLayoutParams(dotImageViewParams); this.addView(dotImageView); } } }
PagerCursor
package com.example.welcome; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class PagerCursor extends View { Context context; private Paint mPaint; private int screenWidth; private int desRight; private float itemWidth; public PagerCursor(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public PagerCursor(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; initRes(); // TODO Auto-generated constructor stub } public PagerCursor(Context context) { super(context); // TODO Auto-generated constructor stub } private void initRes() { float[] screenSize = MeasureUtil.getScreenSize((Activity) context); screenWidth = (int) screenSize[0]; Log.d("TAG", ",screenWidth" + screenWidth); mPaint = new Paint(); mPaint.setColor(getResources().getColor(R.color.contentPressColor)); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); } public void SetOffSet(int position, float screenOffSet, float itemWidth) { int offSet = (int) (position * itemWidth + itemWidth * screenOffSet); this.itemWidth = itemWidth; desRight = offSet; Log.d("TAG", "screenOffSet:" + screenOffSet + ",position" + position + ",desRigh-->t" + desRight + ",itemWidth" + itemWidth); invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Log.d("TAG", "onDraw,desRigh-->t" + desRight + ",itemWidth" + itemWidth); canvas.drawRect(desRight, 0f, desRight + itemWidth, PhoneUtils.dp2px(context, 5), mPaint); } }
MeasureUtil
package com.example.welcome; import android.app.Activity; import android.util.DisplayMetrics; /** * 测绘工具�? */ public final class MeasureUtil { /** * 获取屏幕尺寸 * * @param activity * Activity * @return 屏幕尺寸像素值,下标�?的�?为宽,下标为1的�?为高 */ public static float[] getScreenSize(Activity activity) { DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); return new float[] { metrics.widthPixels, metrics.heightPixels }; } }
PhoneUtils
package com.example.welcome; import android.content.Context; public class PhoneUtils { public static int dp2px(Context context,float dpValue){ float scale = context.getResources().getDisplayMetrics().density; return (int)(dpValue * scale +0.5f); } }
源码下载:http://xiazai.jb51.net/201610/yuanma/androidviewpaper(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索Android滑动ViewPager
android滑动viewpager、android 滑动解锁实现、android实现左右滑动、android实现上下滑动、android实现页面滑动,以便于您获取更多的相关知识。
时间: 2024-10-26 15:05:10