自定义view实现一个d性滑动的效果,供大家参考,具体内容如下
实现原理
onMeasure()中测量所有子VIEw
@OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { // 测量所有子VIEw int count = getChildCount(); for (int i = 0; i < count; i++) { VIEw childVIEw = getChildAt(i); measureChild(childVIEw,wIDthMeasureSpec,heightmeasureSpec); } setMeasuredDimension(wIDthMeasureSpec,heightmeasureSpec); }
onLayout()中,将所有的子VIEw按照位置依次往下排列
@OverrIDe protected voID onLayout(boolean changed,int l,int t,int r,int b) { // 设置VIEwGroup的高度,对所有子VIEw进行排列 int childCount = getChildCount(); marginLayoutParams params = (marginLayoutParams) getLayoutParams(); params.height = mScreenHeight * childCount; for (int i = 0; i < childCount; i++) { VIEw childVIEw = getChildAt(i); if (childVIEw.getVisibility() != VIEw.GONE) { // 给每个ChildVIEw放置在指定位置 childVIEw.layout(l,i * mScreenHeight,r,(i + 1) * mScreenHeight); } } }
ontouchEvent()中处理滑动
@OverrIDe public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastY = (int) event.getY(); mStart = getScrollY(); return true; case MotionEvent.ACTION_MOVE: if (!mScroller.isFinished()) { // 终止滑动 mScroller.abortAnimation(); } int offsetY = (int) (mLastY - event.getY()); Log.d(TAG,"ontouchEvent: getScrollY: " + getScrollY()); Log.d(TAG,"ontouchEvent: offsetY " + offsetY); // 到达顶部,使用offset判断方向 if (getScrollY() + offsetY < 0) { // 当前已经滑动的 Y 位置 offsetY = 0; } // 到达底部 if (getScrollY() > getHeight() - mScreenHeight && offsetY > 0) { offsetY = 0; } scrollBy(0,offsetY); // 滑动完成后,重新设置LastY位置 mLastY = (int) event.getY(); break; case MotionEvent.ACTION_UP: mEnd = getScrollY(); int distance = mEnd - mStart; if (distance > 0) { // 向上滑动 if (distance < mScreenHeight / 3) { Log.d(TAG,"ontouchEvent: distance < screen/3"); // 回到原来位置 mScroller.startScroll(0,getScrollY(),-distance); } else { // 滚到屏幕的剩余位置 mScroller.startScroll(0,mScreenHeight - distance); } } else { // 向下滑动 if (-distance < mScreenHeight / 3) { mScroller.startScroll(0,-distance); } else { mScroller.startScroll(0,-mScreenHeight - distance); } } postInvalIDate(); } return super.ontouchEvent(event); }
其中ACTION_UP这段代码是处理d性滑动的
case MotionEvent.ACTION_UP: mEnd = getScrollY(); int distance = mEnd - mStart; if (distance > 0) { // 向上滑动 if (distance < mScreenHeight / 3) { Log.d(TAG,-mScreenHeight - distance); } } postInvalIDate();
完整代码
public class ScrollVIEwGroup extends VIEwGroup { private static final String TAG = "ScrollVIEw"; private Scroller mScroller; private int mScreenHeight; // 窗口高度 private int mLastY; private int mStart; private int mEnd; public ScrollVIEwGroup(Context context) { this(context,null); } public ScrollVIEwGroup(Context context,AttributeSet attrs) { this(context,attrs,0); } public ScrollVIEwGroup(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); mScroller = new Scroller(context); // 获取屏幕高度 WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); displayMetrics metrics = new displayMetrics(); windowManager.getDefaultdisplay().getMetrics(metrics); mScreenHeight = metrics.heightPixels; Log.d(TAG,"ScrollVIEwGroup: ScreenHeight " + mScreenHeight); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,heightmeasureSpec); } @OverrIDe protected voID onLayout(boolean changed,(i + 1) * mScreenHeight); } } } @OverrIDe public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastY = (int) event.getY(); mStart = getScrollY(); return true; case MotionEvent.ACTION_MOVE: if (!mScroller.isFinished()) { // 终止滑动 mScroller.abortAnimation(); } int offsetY = (int) (mLastY - event.getY()); Log.d(TAG,-mScreenHeight - distance); } } postInvalIDate(); } return super.ontouchEvent(event); } @OverrIDe public voID computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(),mScroller.getCurrY()); postInvalIDate(); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android自定义ViewGroup实现d性滑动效果全部内容,希望文章能够帮你解决Android自定义ViewGroup实现d性滑动效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)