diff --git a/.idea/.name b/.idea/.name index d19c35e..afa36b7 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -AutoDismiss \ No newline at end of file +AutoMissingMessage \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 7713b04..508b3d9 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,16 +3,20 @@ diff --git a/.idea/modules.xml b/.idea/modules.xml index 7f8440d..656795b 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 6564d52..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/java/dev/journey/auotodismiss/view/MessagQuenView.java b/app/src/main/java/dev/journey/auotodismiss/view/MessagQuenView.java index 301328e..5ee469c 100644 --- a/app/src/main/java/dev/journey/auotodismiss/view/MessagQuenView.java +++ b/app/src/main/java/dev/journey/auotodismiss/view/MessagQuenView.java @@ -1,21 +1,25 @@ package dev.journey.auotodismiss.view; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Stack; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; +import android.util.Log; +import android.view.MotionEvent; +import android.view.VelocityTracker; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; +import android.widget.Scroller; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Stack; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import dev.journey.autodismiss.R; @@ -26,6 +30,12 @@ public class MessagQuenView extends ViewGroup { private Context context; + private Scroller mScroller; + private VelocityTracker mVelocityTracker; + private int viewHeight = 0; + private int childViewHeight = 0; + private float lastDownY; + private String TAG = "tag"; public MessagQuenView(Context context, AttributeSet attrs) { super(context, attrs); @@ -61,19 +71,24 @@ public void shutDownSchedulSeervice() { private void init() { setSchedule(); + //初始化滑动的类 + mScroller = new Scroller(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int count = getChildCount(); + int measureTempHeight = 0; for (int i = 0; i < count; i++) { measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec); + measureTempHeight = measureTempHeight + getChildAt(i).getMeasuredHeight(); } - // setMeasuredDimension(measureSize(widthMeasureSpec), - // measureSize(heightMeasureSpec)); - + childViewHeight = measureTempHeight; setMeasuredDimension(measureWidthSize(widthMeasureSpec), measureSize(heightMeasureSpec)); + viewHeight = getHeight(); + Log.e(TAG, "childViewHeight=" + childViewHeight); + Log.e(TAG, "viewHeight == " + viewHeight); } private int measureWidthSize(int widthMeasureSpec) { @@ -160,8 +175,6 @@ public void handleMessage(Message msg) { childQueue.remove((View) msg.obj); } - - ; }; /* @@ -194,11 +207,9 @@ public void onAnimationEnd(Animation animation) { } - ; }; public void schedualRemove() { - for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child != null) { @@ -221,12 +232,10 @@ public void schedualRemove() { } } - } // 添加View 控件 public void addNewView(View childv) { - ItemTag tag = new ItemTag(); tag.time = System.currentTimeMillis(); childv.setTag(tag); @@ -236,6 +245,76 @@ public void addNewView(View childv) { this.addView(childv); } + + @Override + public boolean onTouchEvent(MotionEvent event) { + + if (mVelocityTracker == null) { + mVelocityTracker = VelocityTracker.obtain(); + } + mVelocityTracker.addMovement(event); + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + lastDownY = event.getY(); + mVelocityTracker.clear(); + mVelocityTracker.addMovement(event); + break; + case MotionEvent.ACTION_MOVE: + float currentY = event.getY(); + float dy; + dy = lastDownY - currentY; + lastDownY = event.getY(); + mVelocityTracker.addMovement(event); + //dy>0向上滑动 超过水平线 getScrollY <0 低于最低水平 +// if (dy >= 0 && getScrollY() > 0) { +// Log.e(TAG, "dy >===0"); +// return true; +// } +// //dy<0向下滑动 低于水平线 getScrollY >0 超过最高水平 +// if (dy <= 0 && getScrollY() < viewHeight - childViewHeight) { +// Log.e(TAG, "dy <====0"); +// return true; +// } + + if (childViewHeight > viewHeight) { + scrollBy(0, (int) dy); + } + Log.e(TAG, "scrollY =" + getScrollY()); + break; + case MotionEvent.ACTION_UP: +// if (childViewHeight > viewHeight) { +// if (getScrollY() > 0) { +// mScroller.startScroll(0, getScrollY(), 0, -getScrollY()); +// } else if (getScrollY() < viewHeight - childViewHeight) { +// int temp = (viewHeight - childViewHeight); +// mScroller.startScroll(0, getScrollY(), 0, temp - getScrollY()); +// } +// } + if (childViewHeight > viewHeight) { + mVelocityTracker.computeCurrentVelocity(1000); + float velocityY = mVelocityTracker.getYVelocity(); + mScroller.fling(0, getScrollY(), 0, (int) -velocityY, 0, 0, viewHeight - childViewHeight, 0); + postInvalidate(); + } else { +// mScroller.startScroll(); + mScroller.startScroll(0, getScrollY(), 0, -getScrollY()); + + } + + break; + } + return true; + } + + + @Override + public void computeScroll() { + if (mScroller.computeScrollOffset()) {//是否已经滚动完成 + scrollTo(0, mScroller.getCurrY());//获取当前值,startScroll()初始化后,调用就能获取区间值 + postInvalidate(); + } + } + /* * View的tag标记 */