行业资讯
📅 2026/8/27 13:18:01
Android中View跟随手指滑动效果的实例代码
本文讲述了Android中View跟随手指滑动效果的实例代码。分享给大家供大家参考具体如下1、android View 主要6种滑动方法分别是layout()offsetLeftAndRight()和offsetTopAndBottom()LayoutParamsscrollBy()和 scrollTo()Scroller动画2、实现效果图3、自定义中使用layout()方法实习view的滑动代码语言javascriptpublicclassMoveViewextendsView{privateint lastX,lastY;publicMoveView(Context context,Nullable AttributeSet attrs){super(context,attrs);}publicMoveView(Context context,Nullable AttributeSet attrs,int defStyleAttr){super(context,attrs,defStyleAttr);}publicMoveView(Context context){super(context);}publicbooleanonTouchEvent(MotionEvent event){int x(int)event.getX();int y(int)event.getY();switch(event.getAction()){caseMotionEvent.ACTION_DOWN:lastXx;lastYy;break;caseMotionEvent.ACTION_MOVE:int offsetXx-lastX;//计算滑动的距离int offsetYy-lastY;//重新放置新的位置layout(getLeft()offsetX,getTop()offsetY,getRight()offsetX,getBottom()offsetY);}returntrue;}}2、offsetLeftAndRight()和offsetTopAndBottom()代码语言javascriptpublicbooleanonTouchEvent(MotionEvent event){int x(int)event.getX();int y(int)event.getY();switch(event.getAction()){caseMotionEvent.ACTION_DOWN:lastXx;lastYy;break;caseMotionEvent.ACTION_MOVE:int offsetXx-lastX;//计算滑动的距离int offsetYy-lastY;//重新放置新的位置// layout(getLeft() offsetX, getTop() offsetY, getRight() offsetX, getBottom() offsetY);offsetLeftAndRight(offsetX);offsetTopAndBottom(offsetY);}returntrue;}3、LayoutParams 改变布局参数的方法代码语言javascriptpublicbooleanonTouchEvent(MotionEvent event){int x(int)event.getX();int y(int)event.getY();switch(event.getAction()){caseMotionEvent.ACTION_DOWN:lastXx;lastYy;break;caseMotionEvent.ACTION_MOVE:int offsetXx-lastX;//计算滑动的距离int offsetYy-lastY;//重新放置新的位置// layout(getLeft() offsetX, getTop() offsetY, getRight() offsetX, getBottom() offsetY);// offsetLeftAndRight(offsetX);// offsetTopAndBottom(offsetY);LinearLayout.LayoutParams layoutParams(LinearLayout.LayoutParams)getLayoutParams();layoutParams.leftMargingetLeft()offsetX;layoutParams.topMargingetTop()offsetY;setLayoutParams(layoutParams);}returntrue;}4、当然使用动画 scrollBy()和 scrollTo()也可以使view滑动不足的是使用scrollBy()和 scrollTo()滑动时是瞬间完成的用户体验不太好。5、Scroller和 View的computeScroll() 结合使用实现view平滑的移动代码语言javascriptpublicclassMoveViewextendsView{privateScroller mScroller;publicMoveView(Context context,Nullable AttributeSet attrs){super(context,attrs);mScrollernewScroller(context);}publicMoveView(Context context,Nullable AttributeSet attrs,int defStyleAttr){super(context,attrs,defStyleAttr);}publicMoveView(Context context){super(context);}//重写computeScroll方法OverridepublicvoidcomputeScroll(){//view在onDraw的时候会调用此方法super.computeScroll();if(mScroller.computeScrollOffset()){((View)getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());invalidate();}}//在外部调用这个方法即可publicvoidsmoothScrollTo(int destX,int destY){int scrollXgetScrollX();int deltadestX-scrollX;mScroller.startScroll(scrollX,0,delta,0,6000);invalidate();}}以上就是这篇文章的全部内容了希望本文的内容对大家的学习或者工作具有一定的参考学习价值更多Android进阶指南 可以扫码 解锁《Android十大板块文档》1.Android车载应用开发系统学习指南附项目实战2.Android Framework学习指南助力成为系统级开发高手3.2024最新Android中高级面试题汇总解析告别零offer4.企业级Android音视频开发学习路线项目实战附源码5.Android Jetpack从入门到精通构建高质量UI界面6.Flutter技术解析与实战跨平台首要之选7.Kotlin从入门到实战全方面提升架构基础8.高级Android插件化与组件化含实战教程和源码9.Android 性能优化实战360°全方面性能调优10.Android零基础入门到精通高手进阶之路敲代码不易关注一下吧。ღ( ´ᴗ )