软件编程
位置:首页>> 软件编程>> Android编程>> Android之FanLayout制作圆弧滑动效果

Android之FanLayout制作圆弧滑动效果

作者:陈小缘  发布时间:2023-01-14 16:58:29 

标签:Android,弧形,滑动,圆弧,FanLayout

前言

在上篇文章(Android实现圆弧滑动效果之ArcSlidingHelper篇)中,我们把圆弧滑动手势处理好了,那么这篇文章我们就来自定义一个ViewGroup,名字叫就风扇布局吧,接地气。 在开始之前,我们先来看2张效果图 (表情包来自百度贴吧):

Android之FanLayout制作圆弧滑动效果Android之FanLayout制作圆弧滑动效果 

哈哈,其实还有以下特性的,就先不发那么多图了:

Android之FanLayout制作圆弧滑动效果

简单分析

圆弧手势滑动我们现在可以跳过了(因为在上一篇文章中做好了),先从最基本的开始,想一下该怎么layout? 其实也很简单:从上面几张效果图中我们可以看出来,那一串串小表情是围着大表情旋转的,即小表情的旋转点(mPivotX,mPivotY) = 大表情的中心点(宽高 ÷ 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:


   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       measureChildren(widthMeasureSpec, heightMeasureSpec);
       setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
   }

来源:https://blog.csdn.net/u011387817/article/details/80788704

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com