最近看了hyman的博客:http://blog.csdn.net/lmj623565791/article/details/23692439 ,由于个人技术还比较薄弱,就在这个自定义控件的基础上做了个拓展,支持水平和垂直的
下面是主要代码;
GuideView:
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;
import android.widget.Toast;
@SuppressLint("ClickableViewAccessibility")
public class GuideView extends ViewGroup {
/**
*
<attr name="orientation"> <enum name="horizontal" value="1" /> <enum
* name="vertical" value="2" /> </attr>
**/
/** 滑动方向 */
private int mOrientation = 0;
/** 水平方向 */
private int mHorientation = 0;
/** 垂直方向 */
private int mVertical = 1;
/** 屏幕宽度 */
private int mScreenWidth;
/** 屏幕高度 */
private int mScreenHeight;
/** 滑动状态 */
private boolean isScrolling;
/** 滑动辅助类 */
private Scroller mScroller;
/** 记录当前的x/y的值 */
private PointF mPointF;
/** 记录上一次的x、y值 */
private PointF mLastPointF;
/** Scroller 对应的开始坐标 */
private Point mScrollStartPoint;
/** Scroller 对应的结束坐标 */
private Point mScrollStopPoint;
/** 记录滑动的距离 */
private PointF mDistancePointF;
/**ScrollXY 的差值*/
private Point mDistanceScrollPoint;
/** 加速度检测 */
private VelocityTracker mVelocityTracker;
/**切换屏幕时的回调函数*/
private OnPageChangeListener mOnPageChangeListener;
/**
* 记录当前页
*/
private int currentPage = 0;
public GuideView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
// 获取自定义属性
TypedArray mTypeArray = context.obtainStyledAttributes(
attrs, R.styleable.GuideView_orientation);
mOrientation = mTypeArray.getInteger(
R.styleable.GuideView_orientation_orientation, mOrientation);
mTypeArray.recycle();
// 获取屏幕宽高
initialScreen(context);
mScroller = new Scroller(context);
mPointF = new PointF();
mLastPointF = new PointF();
mScrollStartPoint = new Point();
mScrollStopPoint = new Point();
mDistancePointF = new PointF();
mDistanceScrollPoint=new Point();
}
public GuideView(Context context, AttributeSet attrs) {
this(context, attrs, 1);
// TODO Auto-generated constructor stub
}
public GuideView(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);