摘要:本文将带你了解Android应用开发之Android实现圆弧滑动效果之FanLayout篇,希望本文对大家学Android有所帮助。
本文将带你了解Android应用开发之Android实现圆弧滑动效果之FanLayout篇,希望本文对大家学Android有所帮助。
Android实现圆弧滑动效果之FanLayout篇
先从最基本的开始,想一下该怎么layout? 其实也很简单:从上面几张效果图中我们可以看出来,那一串串小表情是围着大表情旋转的,即小表情的旋转点(mPivotX,mPivotY) = 大表情的中心点(宽高 &pide; 2),至于旋转,肯定是用setRotation方法啦,不过在setRotation之前,还要先set一下PivotX和PivotY。
创建FanLayout
首先是onLayout方法:
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); //每个item要旋转的角度 float angle = 360F / childCount; //旋转基点,现在也就是这个ViewGroup的中心点 mPivotX = getWidth() / 2; mPivotY = getHeight() / 2; for (int i = 0; i < childCount; i++) {View view = getChildAt(i);int layoutHeight = view.getMeasuredHeight() / 2;int layoutWidth = view.getMeasuredWidth(); //在圆心的右边,并且垂直居中view.layout(mPivotX, mPivotY - layoutHeight, mPivotX + layoutWidth, mPivotY + layoutHeight);//更新旋转的中心点view.setPivotX(0);view.setPivotY(layoutHeight);//设置旋转的角度view.setRotation(i * angle); } }
onMeasure我们先不考虑那么细,measureChildren后直接setMeasuredDimension:
?12345@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { measureChildren(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);}
好了,就这么简单,我们来看看效果怎么样:
我们的布局:
<com.test.fanlayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="https://schemas.android.com/apk/res/android"> <include layout="@layout/item"> <include layout="@layout/item"> <include layout="@layout/item"> <include layout="@layout/item"> <include layout="@layout/item"> <include layout="@layout/item"> </include></include></include></include></include></include></com.test.fanlayout>
效果:
哈哈,现在最基本的效果是出来了,但是还未支持手势,这时候我们上篇做的ArcSlidingHelper要登场了,我们把ArcSlidingHelper添加进来:
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (mArcSlidingHelper == null) {mArcSlidingHelper = ArcSlidingHelper.create(this, this);//开始惯性滚动mArcSlidingHelper.enableInertialSliding(true); } }
我们把ArcSlidingHelper放到onSizeChanged里面初始化,为什么呢,因为这个方法回调时,getWidth和getHeight已经能获取到正确的值了
@Override public boolean onTouchEvent(MotionEvent event) { //把触摸事件交给Helper去处理 mArcSlidingHelper.handleMovement(event); return true; } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); //释放资源 if (mArcSlidingHelper != null) {mArcSlidingHelper.release();mArcSlidingHelper = null; } } @Override public void onSliding(float angle) { for (int i = 0; i < getChildCount(); i++) {View child = getChildAt(i);//更新角度child.setRotation(child.getRotation() + angle); } }
onSliding方法就是ArcSlidingHelper的OnSlidingListener接口,当ArcSlidingHelper计算出角度之后,就会回调onSliding方法,我们在这里面直接更新了子view的角度,并且在onDetachedFromWindow释放了ArcSlidingHelper
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号