设计一个简单的android动画库

设计一个通用的Android View动画库。这个库将会提供一组简单易用的API来创建和管理各种类型的动画。我们可以使用Kotlin来编写这个库,并利用Android的动画框架(如ObjectAnimatorValueAnimator等)来实现。

功能需求

  1. 基本动画支持:支持透明度、平移、旋转和缩放动画。
  2. 组合动画:支持顺序和并行动画。
  3. 自定义动画:允许用户自定义动画属性和时间。
  4. 回调:支持动画开始、结束和取消的回调。
  5. 便捷的API:提供链式调用的API,简化动画创建和管理。

目录结构

markdown 复制代码
markdown
复制代码
/src/main/java/com/example/animationlibrary
    - AnimationLibrary.kt
    - ViewExtensions.kt
    - AnimListener.kt
    - AnimationType.kt

代码实现

AnimationLibrary.kt

kotlin 复制代码
kotlin
复制代码
package com.example.animationlibrary

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View

class AnimationLibrary {
    private val animatorSet = AnimatorSet()
    private val animators = mutableListOf<ObjectAnimator>()

    fun addAlphaAnimation(view: View, fromAlpha: Float, toAlpha: Float, duration: Long): AnimationLibrary {
        val animator = ObjectAnimator.ofFloat(view, "alpha", fromAlpha, toAlpha).apply {
            this.duration = duration
        }
        animators.add(animator)
        return this
    }

    fun addTranslationAnimation(view: View, fromX: Float, toX: Float, fromY: Float, toY: Float, duration: Long): AnimationLibrary {
        val animatorX = ObjectAnimator.ofFloat(view, "translationX", fromX, toX).apply {
            this.duration = duration
        }
        val animatorY = ObjectAnimator.ofFloat(view, "translationY", fromY, toY).apply {
            this.duration = duration
        }
        animators.add(animatorX)
        animators.add(animatorY)
        return this
    }

    fun addRotationAnimation(view: View, fromDegrees: Float, toDegrees: Float, duration: Long): AnimationLibrary {
        val animator = ObjectAnimator.ofFloat(view, "rotation", fromDegrees, toDegrees).apply {
            this.duration = duration
        }
        animators.add(animator)
        return this
    }

    fun addScaleAnimation(view: View, fromScaleX: Float, toScaleX: Float, fromScaleY: Float, toScaleY: Float, duration: Long): AnimationLibrary {
        val animatorX = ObjectAnimator.ofFloat(view, "scaleX", fromScaleX, toScaleX).apply {
            this.duration = duration
        }
        val animatorY = ObjectAnimator.ofFloat(view, "scaleY", fromScaleY, toScaleY).apply {
            this.duration = duration
        }
        animators.add(animatorX)
        animators.add(animatorY)
        return this
    }

    fun setDuration(duration: Long): AnimationLibrary {
        animators.forEach { it.duration = duration }
        return this
    }

    fun setListener(listener: AnimListener): AnimationLibrary {
        animatorSet.addListener(listener)
        return this
    }

    fun start() {
        animatorSet.playTogether(animators)
        animatorSet.start()
    }
}

ViewExtensions.kt

kotlin 复制代码
kotlin
复制代码
package com.example.animationlibrary

import android.view.View

fun View.animateAlpha(fromAlpha: Float, toAlpha: Float, duration: Long): AnimationLibrary {
    return AnimationLibrary().addAlphaAnimation(this, fromAlpha, toAlpha, duration)
}

fun View.animateTranslation(fromX: Float, toX: Float, fromY: Float, toY: Float, duration: Long): AnimationLibrary {
    return AnimationLibrary().addTranslationAnimation(this, fromX, toX, fromY, toY, duration)
}

fun View.animateRotation(fromDegrees: Float, toDegrees: Float, duration: Long): AnimationLibrary {
    return AnimationLibrary().addRotationAnimation(this, fromDegrees, toDegrees, duration)
}

fun View.animateScale(fromScaleX: Float, toScaleX: Float, fromScaleY: Float, toScaleY: Float, duration: Long): AnimationLibrary {
    return AnimationLibrary().addScaleAnimation(this, fromScaleX, toScaleX, fromScaleY, toScaleY, duration)
}

AnimListener.kt

kotlin 复制代码
kotlin
复制代码
package com.example.animationlibrary

import android.animation.Animator

open class AnimListener : Animator.AnimatorListener {
    override fun onAnimationStart(animation: Animator) {}
    override fun onAnimationEnd(animation: Animator) {}
    override fun onAnimationCancel(animation: Animator) {}
    override fun onAnimationRepeat(animation: Animator) {}
}

AnimationType.kt

kotlin 复制代码
kotlin
复制代码
package com.example.animationlibrary

enum class AnimationType {
    ALPHA,
    TRANSLATION,
    ROTATION,
    SCALE
}

使用示例

在你的应用中,可以通过以下方式使用这个动画库:

kotlin 复制代码
kotlin
复制代码
import com.example.animationlibrary.animateAlpha
import com.example.animationlibrary.AnimListener

val myView: View = findViewById(R.id.my_view)

myView.animateAlpha(0f, 1f, 1000)
    .setListener(object : AnimListener() {
        override fun onAnimationEnd(animation: Animator) {
            // 动画结束后的操作
        }
    })
    .start()

通过这种方式,你可以非常方便地创建和管理动画,并且代码清晰简洁。这个库可以根据具体需求进一步扩展,例如添加更多的动画类型、支持更多的回调等。

上面的动画库支持曲线路径效果。通过使用 Path 类,你可以定义各种曲线路径,然后通过 ObjectAnimator 来沿着这些路径移动视图。例如,使用二次贝塞尔曲线或三次贝塞尔曲线来定义复杂的曲线路径。

要在现有的动画库基础上添加更多的动画效果,如烟花绽放和水滴效果,我们可以定义一些自定义的动画。可以利用 ObjectAnimatorValueAnimatorAnimatorSet 等工具来实现这些效果。

demo地址

相关推荐
雮尘1 小时前
Android性能优化之枚举替代
android
2501_915909063 小时前
苹果上架App软件全流程指南:iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核技巧详解
android·ios·小程序·https·uni-app·iphone·webview
2501_915921433 小时前
iOS 文件管理与能耗调试结合实战 如何查看缓存文件、优化电池消耗、分析App使用记录(uni-app开发与性能优化必备指南)
android·ios·缓存·小程序·uni-app·iphone·webview
2501_915918414 小时前
App 苹果 上架全流程解析 iOS 应用发布步骤、App Store 上架流程
android·ios·小程序·https·uni-app·iphone·webview
2501_916007474 小时前
苹果上架全流程详解,iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核要点完整指南
android·ios·小程序·https·uni-app·iphone·webview
PuddingSama5 小时前
Android 高级绘制技巧: BlendMode
android·前端·面试
2501_915921435 小时前
iOS App 性能监控与优化实战 如何监控CPU、GPU、内存、帧率、耗电情况并提升用户体验(uni-app iOS开发调试必备指南)
android·ios·小程序·uni-app·iphone·webview·ux
Digitally6 小时前
如何将视频从安卓手机传输到电脑?
android·智能手机·电脑
CV资深专家6 小时前
Android 相机框架的跨进程通信架构
android
前行的小黑炭6 小时前
Android :如何提升代码的扩展性,方便复制到其他项目不会粘合太多逻辑,增强你的实战经验。
android·java·kotlin