android 旋转动画设置旋转中心点_安卓实现旋转风车动画(仿华为天气预报)

本文介绍了如何在Android上创建旋转风车动画,通过固定背景图和一张旋转图叠加实现,利用属性动画控制旋转,强调调整图片相对位置的重要性,并提供了主要代码及库的集成方法。
摘要由CSDN通过智能技术生成

旋转风车实现效果

21808f96d8933c02ce91199c4add3cc9.gif

实现方案

对于风车的立柱部分,采用一张固定背景图;对于风车上面旋转部分,采用一张图,通过安卓属性动画进行旋转。注意点: 因为是采用两张图叠加而形成的效果,所以需要注意调整两张图的相对位置,使之刚好形成风车旋转效果。

主要代码

  1. BaseWindmillView.kt
abstract class BaseWindmillView constructor(context: Context, attrs: AttributeSet? = null) :    LinearLayout(context, attrs) {    private fun getRotateAnimation(@IntRange(from = 1, to = 10) speed: Int): RotateAnimation {        val animateTime = getAnimateTimeFromSpeed(speed)        val animation = RotateAnimation(            0f, 360f,            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f        )        animation.fillAfter = true        animation.repeatCount = Animation.INFINITE        animation.duration = animateTime        animation.interpolator = LinearInterpolator()        return animation    }    private fun getAnimateTimeFromSpeed(speed: Int): Long {        return (10 * 1000 / speed).toLong()    }    fun startRotate(@IntRange(from = 1, to = 10) speed: Int) {        getToRotateViews().forEach {            it.clearAnimation()            it.startAnimation(getRotateAnimation(speed)) }    }    abstract fun getToRotateViews(): List}
  1. DoubleWindmillView.kt
class DoubleWindmillView constructor(context: Context, attrs: AttributeSet? = null) :    BaseWindmillView(context, attrs) {    init {        LayoutInflater.from(context).inflate(R.layout.view_double_windmill, this, true)    }    override fun getToRotateViews()= listOf(windmillBladeIv,smallWindmillBladeIv)}
  1. view_double_windmill.xml
<?xml version="1.0" encoding="utf-8"?>

lib库集成方法

  1. 在工程根目录下build.gradle文件中增加jitpack仓库地址:
allprojects {    repositories {        ...        maven { url 'https://jitpack.io' }    }}
  1. 在app工程下build.gradle文件中增加依赖:
dependencies {    implementation 'com.github.cxyzy1:windmill:1.0.0'}

完整源代码

https://github.com/cxyzy1/windmill

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值