package com.drw.myapplication.custom;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.Scroller;
/**
* Created by Administrator on 2018/2/22.
*/
public class ForeverLayout extends ViewGroup{
private int front=0;//代表前面还有几个页面的变量
private int mWidth;//存储屏幕宽度
private float mXDownPos;//存储点击时的x坐标
private float mXLastMovePos;//存储上一次滑动时保存的x坐标
private float mXMovePos;//存储滑动所到位置的x坐标
private int mTotalNum;//存储子元素个数
private Scroller mScroller;//操控滚动效果的实例
public ForeverLayout(Context context) {
super(context);
Init(context);
}
public ForeverLayout(Context context, AttributeSet attrs) {
super(context, attrs);
Init(context);
}
public ForeverLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Init(context);
}
/**
* 初始化函数
* @param context 上下文对象
*/
private void Init(Context context){
if(mScroller==null)
mScroller = new Scroller(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec,heightMeasureSpec);
}
mTotalNum=getChildCount();
mWidth = getMeasuredWidth();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
int left=0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
child.layout(left, 0, left+mWidth, child.getMeasuredHeight());
left=left+mWidth;
}
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN://点击时获得x,y坐标
mXDownPos = ev.getRawX();
mXLastMovePos = mXDownPos;
break;
case MotionEvent.ACTION_MOVE://判断手指移动的距离有没有达到定性为滑动的最小距离
float distance;
mXMovePos = ev.getRawX();
distance= Math.abs(mXMovePos - mXDownPos);
mXLastMovePos = mXMovePos;
if (distance > 100) {
return true;
}
break;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
mXMovePos = event.getRawX();
int scrolledX = (int) (mXLastMovePos - mXMovePos);
scrollBy(scrolledX, 0);
mXLastMovePos = mXMovePos;
break;
case MotionEvent.ACTION_UP:
mXMovePos = event.getRawX();
if((mXMovePos-mXDownPos)>mWidth/3){
if(front==0) {
int temp2=Math.abs(getScrollX());
temp2=mWidth-temp2;
View temp=getChildAt(mTotalNum-1);
removeViewAt(mTotalNum-1);
addView(temp,0);
invalidate();
setScrollX(temp2);
mScroller.startScroll(getScrollX(), 0, -temp2, 0);
}
else{
front--;
mScroller.startScroll(getScrollX(), 0, -getScrollX() + front * mWidth, 0);
}
invalidate();
}else if((mXMovePos-mXDownPos)<-mWidth/3){
if(front==mTotalNum-1) {
int temp2=Math.abs(getScrollX());
int temp3=temp2/mWidth;
temp2=(temp3+1)*mWidth-temp2;
View temp = getChildAt(0);
removeViewAt(0);
addView(temp, mTotalNum - 1);
invalidate();
setScrollX(-temp2);
mScroller.startScroll(getScrollX(), 0, temp2, 0);
}else{
front++;
mScroller.startScroll(getScrollX(), 0, -getScrollX() + front * mWidth, 0);
}
invalidate();
}else{
mScroller.startScroll(getScrollX(),0,-getScrollX()+front*mWidth,0);
invalidate();
}
}
return super.onTouchEvent(event);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.save();
canvas.translate(mWidth * getChildCount(), 0);
super.dispatchDraw(canvas);
canvas.restore();
canvas.save();
canvas.translate(-mWidth * getChildCount(), 0);
super.dispatchDraw(canvas);
canvas.restore();
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
invalidate();
}
}
}