自定义动画的实现-实例

自定义动画的实现-实例,第1张

概述一:效果上面是一个大嘴在吃一个球,下面是三个球分别一大一小的变化     GitHub地址:https://github.com/luofangli/Custom_Animation代码: <?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://sche

一:效果

上面是一个大嘴在吃一个球,下面是三个球分别一大一小的变化

         

GitHub地址:https://github.com/luofangli/Custom_Animation

代码:

 

<?xml version="1.0" enCoding="utf-8"?>
<androIDx.constraintlayout.Widget.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
xmlns:app="http://schemas.androID.com/apk/res-auto"
xmlns:tools="http://schemas.androID.com/tools"
androID:layout_wIDth="match_parent"
androID:layout_height="match_parent"
tools:context=".MainActivity">

<com.example.custom_animation.BigEatSmallCircle
androID:ID="@+ID/myvIEw"
androID:layout_wIDth="0dp"
androID:layout_height="200dp"
androID:layout_marginStart="10dp"
androID:layout_margintop="10dp"
androID:layout_marginEnd="10dp"
androID:background="@@R_301_6004@/design_default_@R_301_6004@_primary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constrainttop_totopOf="parent" />

<com.example.custom_animation.CircleFollowJump
androID:ID="@+ID/circleAnima"
androID:layout_wIDth="0dp"
androID:layout_height="200dp"
androID:layout_marginStart="10dp"
androID:layout_marginEnd="10dp"
androID:background="@@R_301_6004@/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constrainttop_toBottomOf="@+ID/myvIEw" />
</androIDx.constraintlayout.Widget.ConstraintLayout>

package com.example.custom_animation

import androID.animation.ValueAnimator
import androID.animation.ValueAnimator.INFINITE
import androID.animation.ValueAnimator.offloat
import androID.content.Context
import androID.graphics.*
import androID.util.AttributeSet
import androID.util.Log
import androID.vIEw.VIEw
import androIDx.core.graphics.toRectF

class BigEatSmallCircle:VIEw {
//小圆的半径
private var smallArcRadius = 0f
//大圆的动画因子
private var sweepAngle = 0f
private var startAngle = 0f
//小圆的x,y坐标
private var smallCircleX = 0f
private var smallCircleY = 0f
//小圆x的最大距离
private var smallCircleBigX = 0f
//大圆的动画
private var valueAnimatorBigArc = ValueAnimator()
//小圆的动画
private var valueAnimatorSmallCircle = ValueAnimator()
//画大圆的paint
private val paintBigCircle: Paint by lazy {
Paint().apply {
style = Paint.Style.FILL
@R_301_6004@ = @R_301_6004@.RED
}
}
constructor(context: Context):super(context){}
constructor(context: Context,attributeSet: AttributeSet):super(context,attributeSet){}

overrIDe fun onSizeChanged(w: Int, h: Int, olDW: Int, oldh: Int) {
super.onSizeChanged(w, h, olDW, oldh)
Log.v("lfl","在OnSizeChanged")
ArcRect()
}

overrIDe fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
//画大圆
canvas?.drawArc(ArcRect(),startAngle,sweepAngle,true,paintBigCircle)
//画小圆
canvas?.drawCircle(smallCircleX,smallCircleY,smallArcRadius,paintBigCircle)
}
//确定圆弧的矩形区域 并且确定小圆的x,y坐标
private fun ArcRect():RectF{
//距离画布左右边缘的距离为
var xdistance = 0f
//距离画布上下边缘的距离
var ydistance = 0f
//当画布的高度小于宽度时
if (measuredHeight<measureDWIDth){
smallArcRadius = measuredHeight/6f
xdistance = (measureDWIDth-8.5f*smallArcRadius)/2
}else{
smallArcRadius = measureDWIDth/8.5f
ydistance = (measuredHeight-6*smallArcRadius)/2
}
//确定小圆的x,y坐标
smallCircleBigX = xdistance+7.5f*smallArcRadius
smallCircleY = ydistance+3*smallArcRadius
//返回大圆的矩形区域
return RectF(xdistance,ydistance,
(xdistance+6*smallArcRadius).tofloat(),
(ydistance+6*smallArcRadius).tofloat())
}
//大圆的动画因子的改变
private fun changBigArcAngle(){
valueAnimatorBigArc = ValueAnimator.offloat(0f,45f).apply {
duration = 1000L
repeatCount = INFINITE
addUpdateListener {
val value = it.animatedValue as float
startAngle = value
sweepAngle = 360f-value*2
invalIDate()
}
}
//小球移动的动画
valueAnimatorSmallCircle = ValueAnimator.offloat(0f,5.5f*87.5f).apply {
duration = 1000L
repeatCount = INFINITE
addUpdateListener {
val value = it.animatedValue as float
smallCircleX = smallCircleBigX - value
invalIDate()
}
}
}
//启动动画
fun startAnima(){
changBigArcAngle()
valueAnimatorBigArc.start()
valueAnimatorSmallCircle.start()
}
}

package com.example.custom_animation

import androID.animation.ValueAnimator
import androID.content.Context
import androID.graphics.Canvas
import androID.graphics.@R_301_6004@
import androID.graphics.Paint
import androID.util.AttributeSet
import androID.util.Log
import androID.vIEw.CollapsibleActionVIEw
import androID.vIEw.ContextMenu
import androID.vIEw.VIEw

class CircleFollowJump:VIEw {
//圆的半径
private var raduis = 0f
//圆在y轴上的位置
private var cy = 0f
//圆的x的位置
private val cxArray = mutablelistof<float>()
//存圆的半径
private val raduisArray = arrayOf(0f,0f,0f)
//存入动画
private val valueAnimatorArray = mutablelistof<ValueAnimator>()
//准备画笔
private val paint_circle:Paint by lazy {
Paint().apply {
style = Paint.Style.FILL
@R_301_6004@ = @R_301_6004@.RED
}
}
constructor(context: Context):super(context){}
constructor(context: Context,attributeSet: AttributeSet):super(context,attributeSet){}

overrIDe fun onSizeChanged(w: Int, h: Int, olDW: Int, oldh: Int) {
super.onSizeChanged(w, h, olDW, oldh)
sure_centerandradius()
}

overrIDe fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
//画三个圆
draw3circle(canvas)
}
//确定中心点,以及圆的最大半径
private fun sure_centerandradius(){
//临时判断值,
val temp = measuredHeight/2f
if (7*temp>measureDWIDth){
//圆的半径大了,
raduis = measureDWIDth/7f
}else{
//圆的半径刚刚好
raduis = temp
}
//在x轴上圆离两边的距离
val xdistance = (measureDWIDth-7*raduis)/2
//在y轴上圆离两边的距离
val ydistance = (measuredHeight-2*raduis)/2
//圆中心点y
cy = ydistance+raduis
//将半径存入数组中
//各圆中心点x
for (i in 0..2){
raduisArray[i] = raduis
val cx = (xdistance+raduis+i*2.5*raduis).tofloat()
cxArray.add(cx)
}
}
//画三个圆
private fun draw3circle(canvas: Canvas?){
for (i in 0..2){
canvas?.drawCircle(cxArray[i],cy,raduisArray[i],paint_circle)
}
}
//动画因子的改变
private fun changeRadius(){
Log.v("lfl","在改变因子方法中")
for (i in 0..2){
ValueAnimator.offloat(1f,0.1f,1f).apply {
duration = 1000
repeatCount = ValueAnimator.INFINITE
startDelay = i*155L
addUpdateListener {
val value = it.animatedValue as float
//各圆半径为
raduisArray[i] = raduis*value
invalIDate()
}
valueAnimatorArray.add(this)
}
}
}
//启动动画
fun startAnima(){
changeRadius()
for (i in 0..2){
valueAnimatorArray[i].start()
}
}

}

package com.example.custom_animation

import androID.animation.ValueAnimator
import androIDx.appcompat.app.AppCompatActivity
import androID.os.Bundle
import androID.util.Log
import kotlinx.androID.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
overrIDe fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentVIEw(R.layout.activity_main)
myvIEw.startAnima()
circleAnima.startAnima()


}
}
          总结       

以上是内存溢出为你收集整理的自定义动画的实现-实例全部内容,希望文章能够帮你解决自定义动画的实现-实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存