From f934535d05de3943c2935090034833f6ebba4732 Mon Sep 17 00:00:00 2001 From: ram <623910786@qq.com> Date: Tue, 30 Aug 2016 08:22:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/stone/shop/scroll/MyScrollLayout.class | Bin 0 -> 5012 bytes .../shop/scroll/OnViewChangeListener.class | Bin 0 -> 173 bytes .../com/stone/shop/scroll/MyScrollLayout.java | 200 ++++++++++++++++++ .../stone/shop/scroll/OnViewChangeListener.java | 5 + 4 files changed, 205 insertions(+) create mode 100644 代码/Shop/bin/classes/com/stone/shop/scroll/MyScrollLayout.class create mode 100644 代码/Shop/bin/classes/com/stone/shop/scroll/OnViewChangeListener.class create mode 100644 代码/Shop/src/com/stone/shop/scroll/MyScrollLayout.java create mode 100644 代码/Shop/src/com/stone/shop/scroll/OnViewChangeListener.java diff --git a/代码/Shop/bin/classes/com/stone/shop/scroll/MyScrollLayout.class b/代码/Shop/bin/classes/com/stone/shop/scroll/MyScrollLayout.class new file mode 100644 index 0000000000000000000000000000000000000000..2e64139be3f01504f964cc860f982f5781376c45 GIT binary patch literal 5012 zcma)933wc38Gir0vzeA8uxZnzG*H?k+t5&sQ0QUPw4tF%10*JG5!%V_q&u*?6Lx3Q z7ErFrVG&WWQsim{3s^yvq=9M?m4cv%x1x9-c%kBj9Ifv+vzu&spn0C1`R6~rb5gxdcy*RNv&2oYwKydS5M}Q z5K0uZV!;Y!sa_*##Y}s1PezYjYGf3S@0h4*;<`xfUe$8my58j-U9IgsYZO$w7x1iB zC`q}(MM~9}l#OP5gJCxnmM9>cYRzT%Z5U~V$*E<=fSya*g$XUyp=a$*%Qmevg{n4% zNUAF>9=9gwp3z}uZ6hs88as{*-;s7xZ(yOBHtodsHa6J{8hRCc ztybI!!;5MSQ!upz3ez=IU{aW)85(Lam77GC!Z96Y+E|rK^&6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t0hN9a2FTv z-OudjOCWt5J3SajG|Di7fuDqniNZQ&`g$U{j>zbwqR=ajMI_R>B+iN4=_c*UF;rJ# zmt%Sr$@_p~x{$&T^(K!fJU9U}0sjOsb_5S?N0o$_!-w71ql`4|eQew|7rqoNOfxPP zbzkP#piwXdhmE~t3A;+au=t&jFR{|qXkqxi-!o|iA4{f=wp5AHzN9laEtHH?>cVIMzVo;9$w z*-swt!Oym1w;b7zQh7NiWg{2KEmBpS4Kv0z@Uz_e$_RcdGb8x@*5*JYQ0`fM zA7oGC-I!Vts`!&6kCPU@IBB*E^SI~j2X>+fEeDR&m&R55Gu6K3@x%yX;y&uQ8 zDqf2!`sUkBzugM7jYSdI$k1&f;G4tSq z)!E8AaWiu4yRXD8T)7oDk;}K@HuCs(Y{Pckg*$LRJLE?=dK~xSS?t7%Y|e8+btZ@SqCfAyvxGrUDPEDmwSGch(kWSU&pZFIe?N`JIo z_+I{(U55W~a@@DjpPs-)Y-pOScefIJjs3Wvn??OrSo$z~i%ZD2YK5tCy;QZY(Uj%d|4Fdbq zQtYKk`{>k@RQ{CXsC(nq6&%ewpd7P#{Y{!e{o;)L0{!CTqWqj3egC|$%+zw{jKsY3 zRG-Y^lGMD^B6bEAMh1bb#Ii*F9H6+qe^ORzatR{?53*uL1{MuZ%`ip=uHgLAqU2P! q%$!t421#TkF!S_4MzJw6FfuR$9mK%E2sECRfeplBU*Lj>@BVU literal 0 HcmV?d00001 diff --git a/代码/Shop/src/com/stone/shop/scroll/MyScrollLayout.java b/代码/Shop/src/com/stone/shop/scroll/MyScrollLayout.java new file mode 100644 index 0000000..2b15767 --- /dev/null +++ b/代码/Shop/src/com/stone/shop/scroll/MyScrollLayout.java @@ -0,0 +1,200 @@ +package com.stone.shop.scroll; + +import android.R.bool; +import android.content.Context; +import android.util.AttributeSet; +import android.util.Log; +import android.view.MotionEvent; +import android.view.VelocityTracker; +import android.view.View; +import android.view.ViewConfiguration; +import android.view.ViewGroup; +import android.view.View.MeasureSpec; +import android.widget.Scroller; + +public class MyScrollLayout extends ViewGroup { + + private static final String TAG = "ScrollLayout"; + private VelocityTracker mVelocityTracker; // 用于判断甩动手势 + private static final int SNAP_VELOCITY = 600; //X轴速度基值,大于该值时进行切换 + private Scroller mScroller; // 滑动控制�? + private int mCurScreen; //当前页面为第几屏 + private int mDefaultScreen = 0; + private float mLastMotionX; + + + private OnViewChangeListener mOnViewChangeListener; + + public MyScrollLayout(Context context) { + super(context); + // TODO Auto-generated constructor stub + init(context); + } + + public MyScrollLayout(Context context, AttributeSet attrs) { + super(context, attrs); + // TODO Auto-generated constructor stub + init(context); + } + + private void init(Context context) { + mCurScreen = mDefaultScreen; + mScroller = new Scroller(context); + } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + // TODO Auto-generated method stub + + Log.e("a", "a"); + if (changed) { + int childLeft = 0; + final int childCount = getChildCount(); + Log.e("count", childCount+""); + for (int i = 0; i < childCount; i++) { + final View childView = getChildAt(i); + if (childView.getVisibility() != View.GONE) { + final int childWidth = childView.getMeasuredWidth(); + childView.layout(childLeft, 0, childLeft + childWidth, + childView.getMeasuredHeight()); + childLeft += childWidth; + } + } + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + // TODO Auto-generated method stub + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + Log.e("b", "b"); + final int width = MeasureSpec.getSize(widthMeasureSpec); + + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); + } + + scrollTo(mCurScreen * width, 0); + + } + + public void snapToDestination() { + final int screenWidth = getWidth(); + + final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth; + snapToScreen(destScreen); + } + + //使屏幕移动到第whichScreen+1屏 + public void snapToScreen(int whichScreen) { + + if (getScrollX() != (whichScreen * getWidth())) { + final int delta = whichScreen * getWidth() - getScrollX(); + mScroller.startScroll(getScrollX(), 0, delta, 0, + Math.abs(delta) * 2); + mCurScreen = whichScreen; + invalidate(); + + if (mOnViewChangeListener != null) { + mOnViewChangeListener.OnViewChange(mCurScreen); + } + } + } + + @Override + public void computeScroll() { + // TODO Auto-generated method stub + if (mScroller.computeScrollOffset()) { + scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); + postInvalidate(); + } + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + // TODO Auto-generated method stub + + final int action = event.getAction(); + final float x = event.getX(); + final float y = event.getY(); + + switch (action) { + case MotionEvent.ACTION_DOWN: + + Log.i("", "onTouchEvent ACTION_DOWN"); + if (mVelocityTracker == null) { + mVelocityTracker = VelocityTracker.obtain(); + mVelocityTracker.addMovement(event); + } + if (!mScroller.isFinished()) { + mScroller.abortAnimation(); + } + mLastMotionX = x; + break; + + case MotionEvent.ACTION_MOVE: + + int deltaX = (int) (mLastMotionX - x); + if (IsCanMove(deltaX)) { + if (mVelocityTracker != null) { + mVelocityTracker.addMovement(event); + } + mLastMotionX = x; + //正向或者负向移动,屏幕跟随手指移动 + scrollBy(deltaX, 0); + } + break; + + case MotionEvent.ACTION_UP: + + int velocityX = 0; + if (mVelocityTracker != null) { + mVelocityTracker.addMovement(event); + mVelocityTracker.computeCurrentVelocity(1000); + //得到X轴方向手指移动速度 + velocityX = (int) mVelocityTracker.getXVelocity(); + } + //velocityX为正值说明手指向右滑动,为负值说明手指向左滑动 + if (velocityX > SNAP_VELOCITY && mCurScreen > 0) { + // Fling enough to move left + Log.e(TAG, "snap left"); + snapToScreen(mCurScreen - 1); + } else if (velocityX < -SNAP_VELOCITY + && mCurScreen < getChildCount() - 1) { + // Fling enough to move right + Log.e(TAG, "snap right"); + snapToScreen(mCurScreen + 1); + } else { + snapToDestination(); + } + + if (mVelocityTracker != null) { + mVelocityTracker.recycle(); + mVelocityTracker = null; + } + + break; + } + return true; + } + + + private boolean IsCanMove(int deltaX) { + //deltaX<0说明手指向右划 + if (getScrollX() <= 0 && deltaX < 0) { + return false; + } + //deltaX>0说明手指向左划 + if (getScrollX() >= (getChildCount() - 1) * getWidth() && deltaX > 0) { + return false; + } + return true; + } + + public void SetOnViewChangeListener(OnViewChangeListener listener) { + mOnViewChangeListener = listener; + } + +} diff --git a/代码/Shop/src/com/stone/shop/scroll/OnViewChangeListener.java b/代码/Shop/src/com/stone/shop/scroll/OnViewChangeListener.java new file mode 100644 index 0000000..c3517c1 --- /dev/null +++ b/代码/Shop/src/com/stone/shop/scroll/OnViewChangeListener.java @@ -0,0 +1,5 @@ +package com.stone.shop.scroll; + +public interface OnViewChangeListener { + public void OnViewChange(int view); +}