Skip to content

Commit

Permalink
手势滑动int改为float
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Jul 26, 2019
1 parent e3588be commit eaeb041
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 46 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion 15
targetSdkVersion 27
versionCode 2
versionName "4.3.1"
versionName "4.3.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, Lis

miui9Calendar.setOnCalendarScrollingListener(new OnCalendarScrollingListener() {
@Override
public void onCalendarScrolling(int dy) {
public void onCalendarScrolling(float dy) {
Log.d(TAG, "onCalendarScrolling::" + dy);
}
});
Expand Down
4 changes: 2 additions & 2 deletions ncalendar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "4.3.1"
versionName "4.3.2"
}
}

Expand All @@ -23,7 +23,7 @@ publish {
repoName = 'maven'//bintray仓库
groupId = 'com.necer.ncalendar'//jcenter上的路径
artifactId = 'ncalendar'//项目名称
publishVersion = '4.3.1'//版本号
publishVersion = '4.3.2'//版本号
desc = 'NCalendar'
website = 'https://github.com/yannecer/NCalendar'
}
8 changes: 4 additions & 4 deletions ncalendar/src/main/java/com/necer/calendar/EmuiCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ protected float getMonthYOnWeekState(LocalDate localDate) {
}

@Override
protected float getGestureMonthUpOffset(int dy) {
protected float getGestureMonthUpOffset(float dy) {
return getGestureChildUpOffset(dy);
}

@Override
protected float getGestureMonthDownOffset(int dy) {
protected float getGestureMonthDownOffset(float dy) {
return getGestureChildDownOffset(dy);
}

@Override
protected float getGestureChildDownOffset(int dy) {
protected float getGestureChildDownOffset(float dy) {
float maxOffset = monthHeight - childView.getY();
return getOffset(Math.abs(dy), maxOffset);
}

@Override
protected float getGestureChildUpOffset(int dy) {
protected float getGestureChildUpOffset(float dy) {
float maxOffset = childView.getY() - weekHeight;
return getOffset(dy, maxOffset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Miui10Calendar(@NonNull Context context, @Nullable AttributeSet attrs) {
* @return
*/
@Override
protected float getGestureMonthUpOffset(int dy) {
protected float getGestureMonthUpOffset(float dy) {
float maxOffset;
float monthCalendarOffset;
if (calendarState == CalendarState.MONTH) {
Expand All @@ -44,7 +44,7 @@ protected float getGestureMonthUpOffset(int dy) {
* @return
*/
@Override
protected float getGestureMonthDownOffset(int dy) {
protected float getGestureMonthDownOffset(float dy) {
float maxOffset = Math.abs(monthCalendar.getY());
float monthCalendarOffset;
if (calendarState == CalendarState.MONTH) {
Expand All @@ -58,13 +58,13 @@ protected float getGestureMonthDownOffset(int dy) {
}

@Override
protected float getGestureChildDownOffset(int dy) {
protected float getGestureChildDownOffset(float dy) {
float maxOffset = monthHeight - childView.getY();
return getOffset(Math.abs(dy), maxOffset);
}

@Override
protected float getGestureChildUpOffset(int dy) {
protected float getGestureChildUpOffset(float dy) {
float maxOffset = childView.getY() - weekHeight;
return getOffset(dy, maxOffset);
}
Expand Down
8 changes: 4 additions & 4 deletions ncalendar/src/main/java/com/necer/calendar/Miui9Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Miui9Calendar(@NonNull Context context, @Nullable AttributeSet attrs) {
}

@Override
protected float getGestureMonthUpOffset(int dy) {
protected float getGestureMonthUpOffset(float dy) {
float maxOffset;
if (calendarState == CalendarState.MONTH) {
//月 月日历有选中则选中为 中心点,如果没有选中则第一行
Expand All @@ -32,20 +32,20 @@ protected float getGestureMonthUpOffset(int dy) {
}

@Override
protected float getGestureMonthDownOffset(int dy) {
protected float getGestureMonthDownOffset(float dy) {
float maxOffset = Math.abs(monthCalendar.getY());
return getOffset(Math.abs(dy), maxOffset);
}

@Override
protected float getGestureChildDownOffset(int dy) {
protected float getGestureChildDownOffset(float dy) {
float maxOffset = monthHeight - childView.getY();
return getOffset(Math.abs(dy), maxOffset);
}


@Override
protected float getGestureChildUpOffset(int dy) {
protected float getGestureChildUpOffset(float dy) {
float maxOffset = childView.getY() - weekHeight;
return getOffset(dy, maxOffset);
}
Expand Down
59 changes: 30 additions & 29 deletions ncalendar/src/main/java/com/necer/calendar/NCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -56,9 +57,9 @@ public abstract class NCalendar extends FrameLayout implements IICalendar, Neste
protected View childView;//NCalendar内部包含的直接子view,直接子view并不一定是NestScrillChild
private View targetView;//实际滑动的view 当targetView==null时,子view没有NestScrillChild或者NestScrillChild不可见,此时滑动交给onTochEvent处理

protected Rect monthRect;//月日历大小的矩形
protected Rect weekRect;//周日历大小的矩形 ,用于判断点击事件是否在日历的范围内
protected Rect stretchMonthRect;
protected RectF monthRect;//月日历大小的矩形
protected RectF weekRect;//周日历大小的矩形 ,用于判断点击事件是否在日历的范围内
protected RectF stretchMonthRect;

private boolean isWeekHoldEnable;//是否需要周状态定
private boolean isMonthStretchEnable;//月日历是否可拉伸
Expand Down Expand Up @@ -324,7 +325,7 @@ public void onStopNestedScroll(View target) {
* 1、起始位置 根据滑动的方向判断是月周切换还是月、拉伸切换
* 2、起始条件触发后,根据childView的位置执行不同的逻辑 setY 和 拉伸高度
*/
protected void gestureMove(int dy, int[] consumed) {
protected void gestureMove(float dy, int[] consumed) {
float monthCalendarY = monthCalendar.getY();
float childViewY = childView.getY();

Expand All @@ -341,7 +342,7 @@ protected void gestureMove(int dy, int[] consumed) {
monthCalendar.setY(-getGestureMonthUpOffset(dy) + monthCalendarY);
childView.setY(-getGestureChildUpOffset(dy) + childViewY);

if (consumed != null) consumed[1] = dy;
if (consumed != null) consumed[1] = (int) dy;
scrolling(dy);

} else if (dy < 0 && childViewY == monthHeight && monthCalendarY == 0 && isMonthStretchEnable) {
Expand All @@ -353,7 +354,7 @@ protected void gestureMove(int dy, int[] consumed) {
float childOffset = getOffset(-dy, stretchMonthHeight - childViewY);
childView.setY(childViewY + childOffset);

if (consumed != null) consumed[1] = dy;
if (consumed != null) consumed[1] = (int) dy;
scrolling(dy);

} else if (dy > 0 && childViewY <= monthHeight && childViewY != weekHeight) {
Expand All @@ -367,7 +368,7 @@ protected void gestureMove(int dy, int[] consumed) {
monthCalendar.setY(-getGestureMonthUpOffset(dy) + monthCalendarY);
childView.setY(-getGestureChildUpOffset(dy) + childViewY);

if (consumed != null) consumed[1] = dy;
if (consumed != null) consumed[1] = (int) dy;
scrolling(dy);

} else if (dy < 0 && childViewY <= monthHeight && childViewY >= weekHeight && (isWeekHoldEnable ? consumed == null : true) && (targetView == null ? true : !targetView.canScrollVertically(-1))) {
Expand All @@ -380,7 +381,7 @@ protected void gestureMove(int dy, int[] consumed) {
monthCalendar.setY(getGestureMonthDownOffset(dy) + monthCalendarY);
childView.setY(getGestureChildDownOffset(dy) + childViewY);

if (consumed != null) consumed[1] = dy;
if (consumed != null) consumed[1] = (int) dy;
scrolling(dy);

} else if (dy < 0 && childViewY >= monthHeight && childViewY <= stretchMonthHeight && monthCalendarY == 0 && isMonthStretchEnable) {
Expand All @@ -392,7 +393,7 @@ protected void gestureMove(int dy, int[] consumed) {
float childOffset = getOffset(-dy, stretchMonthHeight - childViewY);
childView.setY(childViewY + childOffset);

if (consumed != null) consumed[1] = dy;
if (consumed != null) consumed[1] = (int) dy;
scrolling(dy);

} else if (dy > 0 && childViewY >= monthHeight && childViewY <= stretchMonthHeight && monthCalendarY == 0 && isMonthStretchEnable) {
Expand All @@ -404,17 +405,17 @@ protected void gestureMove(int dy, int[] consumed) {
float childOffset = getOffset(-dy, stretchMonthHeight - childViewY);
childView.setY(childViewY + childOffset);

if (consumed != null) consumed[1] = dy;
if (consumed != null) consumed[1] = (int) dy;
scrolling(dy);
}

}


private int downY;
private int downX;
private int lastY;//上次的y
private int verticalY = 50;//竖直方向上滑动的临界值,大于这个值认为是竖直滑动
private float downY;
private float downX;
private float lastY;//上次的y
private float verticalY = 50.f;//竖直方向上滑动的临界值,大于这个值认为是竖直滑动
private boolean isFirstScroll = true; //第一次手势滑动,因为第一次滑动的偏移量大于verticalY,会出现猛的一划,这里只对第一次滑动做处理

@Override
Expand All @@ -424,14 +425,14 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
}
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = (int) ev.getY();
downX = (int) ev.getX();
downY = ev.getY();
downX = ev.getX();
lastY = downY;
targetView = ViewUtil.getTargetView(getContext(), childView);
break;
case MotionEvent.ACTION_MOVE:
int y = (int) ev.getY();
int absY = Math.abs(downY - y);
float y = ev.getY();
float absY = Math.abs(downY - y);
boolean inCalendar = isInCalendar(downX, downY);
if ((absY > verticalY && inCalendar) || targetView == null && absY > verticalY) {
//onInterceptTouchEvent返回true,触摸事件交给当前的onTouchEvent处理
Expand All @@ -447,8 +448,8 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int y = (int) event.getY();
int dy = lastY - y;
float y = event.getY();
float dy = lastY - y;
if (isFirstScroll) {
// 防止第一次的偏移量过大
if (dy > verticalY) {
Expand All @@ -473,7 +474,7 @@ public boolean onTouchEvent(MotionEvent event) {


//点击事件是否在日历的范围内
private boolean isInCalendar(int x, int y) {
private boolean isInCalendar(float x, float y) {
if (calendarState == CalendarState.MONTH) {
return monthRect.contains(x, y);
} else if (calendarState == CalendarState.WEEK) {
Expand Down Expand Up @@ -767,7 +768,7 @@ protected boolean isMonthCalendarWeekState() {
}

//滑动中 包含跟随手势和自动滑动
protected void scrolling(int dy) {
protected void scrolling(float dy) {
setWeekVisible(dy > 0);

if (onCalendarScrollingListener != null) {
Expand All @@ -781,9 +782,9 @@ protected void dispatchDraw(Canvas canvas) {
if (!isInflateFinish) {
monthCalendar.setVisibility(calendarState == CalendarState.MONTH ? VISIBLE : INVISIBLE);
weekCalendar.setVisibility(calendarState == CalendarState.MONTH ? INVISIBLE : VISIBLE);
monthRect = new Rect(0, 0, monthCalendar.getMeasuredWidth(), monthCalendar.getMeasuredHeight());
weekRect = new Rect(0, 0, weekCalendar.getMeasuredWidth(), weekCalendar.getMeasuredHeight());
stretchMonthRect = new Rect(0, 0, monthCalendar.getMeasuredWidth(), stretchMonthHeight);
monthRect = new RectF(0, 0, monthCalendar.getMeasuredWidth(), monthCalendar.getMeasuredHeight());
weekRect = new RectF(0, 0, weekCalendar.getMeasuredWidth(), weekCalendar.getMeasuredHeight());
stretchMonthRect = new RectF(0, 0, monthCalendar.getMeasuredWidth(), stretchMonthHeight);
monthCalendar.setY(calendarState == CalendarState.MONTH ? 0 : getMonthYOnWeekState(weekCalendar.getFirstDate()));
childView.setY(calendarState == CalendarState.MONTH ? monthHeight : weekHeight);
isInflateFinish = true;
Expand Down Expand Up @@ -816,31 +817,31 @@ protected float getOffset(float offset, float maxOffset) {
* @param dy 当前滑动的距离 dy>0向上滑动,dy<0向下滑动
* @return 根据不同日历的交互,计算不同的滑动值
*/
protected abstract float getGestureMonthUpOffset(int dy);
protected abstract float getGestureMonthUpOffset(float dy);

/**
* Child根据手势向上移动的距离
*
* @param dy 当前滑动的距离 dy>0向上滑动,dy<0向下滑动
* @return 根据不同日历的交互,计算不同的滑动值
*/
protected abstract float getGestureChildUpOffset(int dy);
protected abstract float getGestureChildUpOffset(float dy);

/**
* 月日历根据手势向下移动的距离
*
* @param dy 当前滑动的距离 dy>0向上滑动,dy<0向下滑动
* @return 根据不同日历的交互,计算不同的滑动值
*/
protected abstract float getGestureMonthDownOffset(int dy);
protected abstract float getGestureMonthDownOffset(float dy);

/**
* Child根据手势向下移动的距离
*
* @param dy 当前滑动的距离 dy>0向上滑动,dy<0向下滑动
* @return 根据不同日历的交互,计算不同的滑动值
*/
protected abstract float getGestureChildDownOffset(int dy);
protected abstract float getGestureChildDownOffset(float dy);


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

//日历 月 周 拉伸 状态滑动监听
public interface OnCalendarScrollingListener {
void onCalendarScrolling(int dy);
void onCalendarScrolling(float dy);
}

0 comments on commit eaeb041

Please sign in to comment.