android实现八大行星绕太阳3D旋转效果
仿上面效果,采用kotlin实现,逻辑要简单些,注释在源码中,一看就懂

<com.example.androidxdemo.star.StarGroupViewandroid:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" ><!-- 增加太阳View --><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher_background"android:tag="center" /><TextViewandroid:id="@+id/tv1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/colorAccent"android:text="1" /><TextViewandroid:id="@+id/tv2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/darker_gray"android:gravity="center"android:text="2" /><TextViewandroid:id="@+id/tv3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_green_dark"android:gravity="center"android:text="3" /><TextViewandroid:id="@+id/tv4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_blue_dark"android:gravity="center"android:text="4" /><TextViewandroid:id="@+id/tv5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_green_light"android:gravity="center"android:text="5" /><TextViewandroid:id="@+id/tv6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:color/holo_orange_light"android:gravity="center"android:text="6" /><TextViewandroid:id="@+id/tv7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ff3311"android:gravity="center"android:text="7" /><TextViewandroid:id="@+id/tv8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#11aa44"android:gravity="center"android:text="8" /><TextViewandroid:id="@+id/tv9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#ff99cc"android:gravity="center"android:text="9" /></com.example.androidxdemo.star.StarGroupView>
class StarGroupView @JvmOverloads constructor(context: Context,attrs: AttributeSet? = null,defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {val TAG_CENTER = "center"//控件宽高var mWidth = 0var mHeight = 0//中心点var centerWidth = 0var centerHeight = 0private var mRadius = 0//子View的大小,默认是1:1正方形private var childWidth = 0private var childHeight = 0//初始角度,扫过的角度var sweepAngle = 90//值越大,转动越快val changedAngle = 2//延时delayTime ms,进行一次值的改变val delayTime = 100L//手指按下时x和角度private var downX = 0fprivate var downAngle = 0//延时改变view位置和绘制顺序private val autoScrollRunnable = object : Runnable {override fun run() {sweepAngle = (sweepAngle + changedAngle) % 360layoutChildren()postDelayed(this, delayTime)}}init {postDelayed(autoScrollRunnable, delayTime)}override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {super.onSizeChanged(w, h, oldw, oldh)
//        "w: $w h: $h oldw: $oldw oldh: $oldh".log()//如果宽度大于两倍高度,则高度决定宽度,宽度 = 2 * 高度//否则,宽度小于等于两倍高度,则宽度决定高度,高度 = 1/2 * 宽度if (w > h * 2) {mHeight = hmWidth = mHeight * 2} else {mWidth = wmHeight = mWidth / 2}//确定中心点的位置,childWidth = heightWidth = mWidth / 6centerWidth = mWidth / 2centerHeight = mHeight / 2childWidth = mWidth / 6childHeight = childWidth//半径,mRadius = mHeight - childWidth"w: $w h: $h oldw: $oldw oldh: $oldh mWidth: $mWidth mHeight: $mHeight mRadius: $mRadius childWidth: $childWidth".log()}override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {super.onLayout(changed, left, top, right, bottom)
//        "changed $changed left: $left top: $top right: $right bottom: $bottom".log()layoutChildren()}/*** 计算view的位置* z值范围(0-1)* 改变子View的z值以改变子View的绘制优先级,z越大优先级越低(最后绘制)*/private fun layoutChildren() {val centerView = findViewWithTag<View>(TAG_CENTER)val degree: Floatif (centerView == null) {degree = 360f / childCount} else {//中心点view的宽高为 2 * childWithdegree = 360f / (childCount - 1)centerView.layout(centerWidth - childWidth, centerHeight - childHeight,centerWidth + childWidth, centerHeight + childHeight)}for (index in 0 until childCount) {val child = getChildAt(index)if (child.tag == TAG_CENTER) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {child.z = 0.5f}child.rotation = sweepAngle.toFloat()
//                "中心旋转 rotation: $sweepAngle".log()continue}//设置child的大小尺寸和布局, index为0的是centerViewval radius = (degree * index + sweepAngle) * Math.PI / 180val childCenterX = (mRadius * cos(radius)).toInt()val childCenterY = (mRadius * sin(radius) / 2).toInt()
//            "index:$index degree:${degree * (index - 1)} radius:$radius childCenterX: $childCenterX childCenterY: $childCenterY".log()val left = childCenterX - childWidth / 2 + centerWidthval top = childCenterY - childHeight / 2 + centerHeightval right = childCenterX + childWidth / 2 + centerWidthval bottom = childCenterY + childHeight / 2 + centerHeightchild.layout(left, top, right, bottom)val scale = ((sin(radius) + 2) / 3f).toFloat()child.scaleX = scalechild.scaleY = scaleif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {child.z = scale}
//            "index:$index child left: $left top: $top right: $right bottom: $bottom child.scaleX: ${child.scaleX}".logW()}}override fun onTouchEvent(event: MotionEvent): Boolean {when (event.action) {MotionEvent.ACTION_DOWN -> {downX = event.xdownAngle = sweepAngleremoveCallbacks(autoScrollRunnable)"actionDown downX $downX downAngle: $downAngle ".logW()return true}MotionEvent.ACTION_MOVE -> {val dx = event.x - downXif (dx != 0f) {sweepAngle = (dx * 0.2 + downAngle).toInt()"actionMove dx $dx sweepAngle: $sweepAngle ".log()layoutChildren()}}MotionEvent.ACTION_UP -> {"actionUp ".logW()postDelayed(autoScrollRunnable, 16)}}return super.onTouchEvent(event)}}

仿八大行星绕太阳3D旋转效果相关推荐

  1. android立体3D效果_Android实现八大行星绕太阳3D旋转效果

    code小生 一个专注大前端领域的技术平台公众号回复Android加入安卓技术群 作者:史蒂芬诺夫斯基链接:https://www.jianshu.com/p/2954f2ef8ea5声明:本文已获史 ...

  2. android fragment中引入自定义view_厉害了,用Android自定义View实现八大行星绕太阳3D旋转效果...

    作者:史蒂芬诺夫斯基 链接:https://www.jianshu.com/p/2954f2ef8ea5 好久没写View了,最近恰巧遇到一个八大行星绕太阳旋转的假3D效果,写完之后感觉效果还不错.能 ...

  3. 实现八大行星绕太阳3D旋转效果,这波操作不来喊个666?

    /   今日科技快讯   / 针对媒体报道的关于法拉第未来进行重组以及贾跃亭或辞去CEO职务的内容,FF发布声明称,已正式进入公司顶层治理架构变革的执行阶段,近期会公布相关细节.对于贾跃亭的债务问题, ...

  4. Android实现八大行星绕太阳3D旋转效果

    效果图: 本文目的: 巩固/练习 自定义View 分析解决问题的思路 需要解决的问题: 1.行星的整体布局,3D的视觉效果 2.行星转到太阳后面时,会被太阳挡住,转到太阳前面时,会挡住太阳 3.行星自 ...

  5. 2021高考成绩查询镇远一中,离太阳由近到远的八大行星排序及记忆方法

    八大行星按照离太阳的距离从近到远,它们依次为水星.金星.地球.火星.木星.土星.天王星.海王星. 更多高考资讯尽在30高考网https://www.30gk.com/ 八大行星的排序 1.水星:在太阳 ...

  6. QT实现太阳系系统八大行星

    QT实现太阳系系统八大行星 项目简介 项目技术 项目展示 主要源码片段解析 获取完整项目源码传送门 项目简介 演示结合了Qt 3D渲染和Qt Quick 2元素. Planets演示了如何实现将Qt ...

  7. css3动画实现八大行星

    css3动画实现八大行星 话不多说 上代码 看效果 <!DOCTYPE html> <html lang="en"><head><meta ...

  8. 学完javaee基础,编的一个小游戏—太阳系的八大行星

    项目完整代码已放到码云上 自行下载: https://gitee.com/tutu_57893_7590/planets 学完javaee基础,编的一个小游戏-太阳系的八大行星 要使太阳系的八大行星, ...

  9. 太阳系八大行星碰撞的视频_高中地理——太阳系与地球

    知识点 (1)太阳系八大恒星,及其排列顺序. (2)行星公转的特点 (3)为什么,地球上可以存在生命? 考点详解 1.太阳系八大行星的顺序 金木水火土?No!!! 根据距离太阳由近至远的顺序,分别为: ...

最新文章

  1. 一场“正宗”的开发者大会,为什么说微软更像是“AII in AI”了?
  2. MySQL/ACCESS导出一句话拿WebShell后门命令
  3. android trace文件分析ANR
  4. requireJS对文件合并与压缩(二)
  5. android smart home,Android smart home system based on ATmega16
  6. Unity3D shader Blending
  7. linux下GPIO的用户层操作(sysfs)
  8. 合成分红游戏源码_养成合成分红游戏源码,广告分红游戏开发
  9. mysql:Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)解决方法
  10. Latex 大括号错位显示
  11. JVM架构、JVM垃圾回收机制、垃圾回收算法、垃圾回收器、JMM(内存模型)
  12. uefi装完系统后无法引导_uefi装win7启动不了怎么解决?
  13. 黑苹果驱动神器Hackintool 3.4.4中文版
  14. FreeRTOS-时间片与任务阻塞的实现
  15. 数据结构——树和二叉树
  16. 济南铺设全球首条高速光伏公路
  17. 《让子弹飞》系列——小六子之死
  18. 89个windows常用API调用
  19. 【雕爷学编程】Arduino动手做(110)---JDY-31 蓝牙模块
  20. 用HTML CSS 实现简洁美观的时间线(历史年表)

热门文章

  1. 系统集成项目管理工程师05《项目进度管理》
  2. model.compile中metrics的参数accuracy
  3. 【IoT】14.Identify Customer Need 拿捏住客户的想法
  4. 看涨期权计算函数实现(Python)
  5. 赖大师新文章 :Xilinx 开箱-KV260相机,两个小时轻松搞定,文章不能用我坐飞机过去帮你调哈。
  6. WRT之Crosswalk简介
  7. 全国降雨量数据、气温数据、风速数据
  8. 工具-UML【ROSE】关系图
  9. threejs 使用陀螺仪实现手机端全景
  10. 1万+字原创读书笔记,机器学习的知识点全在这篇文章里了