先看看效果图:
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 {
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中用RxJava和ViewPager实现轮播图Android开发基于ViewPager+GridView实现仿大众点评横向滑动功能Android ViewPager实现图片轮播效果Android使用ViewPager实现无限滑动效果Android开发实战之漂亮的ViewPager引导页Android 使用ViewPager实现图片左右循环滑动自动播放android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)Android利用ViewPager实现滑动广告板实例源码Android ViewPager相册横向移动的实现方法android配合viewpager实现可滑动的标签栏示例分享Android viewpage实现禁止滑动的功能