Android属性动画旋转缩放叠加
2020-04-28 本文已影响0人
kevinsEegets
![](https://img.haomeiwen.com/i18406403/f27f1ed45a49171d.gif)
![](https://img.haomeiwen.com/i18406403/1827af8fe16c2d36.png)
![](https://img.haomeiwen.com/i18406403/7c203ef061cbcdf3.png)
话不多说直接上代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingDefaultResource">
<FrameLayout
android:id="@+id/gift_frame"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
tools:ignore="MissingDefaultResource">
<ImageView
android:id="@+id/gift_rotation_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/donate_gift_fastclick_bg"
tools:ignore="MissingConstraints" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/donate_gift_fastclick_icon"
tools:ignore="MissingConstraints" />
</FrameLayout>
<TextView
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:textSize="22sp"
android:text="开始" />
<TextView
android:id="@+id/pause"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:textSize="22sp"
android:text="暂停" />
<TextView
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:textSize="22sp"
android:text="终止"/>
</LinearLayout>
package com.eegets.test.api
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.eegets.R
/**
* @package com.eegets.test.api
* @fileName DonateGiftPressActivity
* @data on 上午11:42
* @author Kevin
* @describe TODO
* @org https://www.jianshu.com/u/ed346078c1d3
* @email wangk8181@gmail.com
**/
class DonateGiftPressActivity : AppCompatActivity() {
private val animatorSet = AnimatorSet()
private var objectAnimator: ObjectAnimator? = ObjectAnimator()
private var actionDownTime: Long = 0
private val framelayout by lazy {
findViewById<FrameLayout>(R.id.gift_frame)
}
private val rotationBg by lazy {
findViewById<ImageView>(R.id.gift_rotation_bg)
}
private val start by lazy {
findViewById<TextView>(R.id.start)
}
private val pause by lazy {
findViewById<TextView>(R.id.pause)
}
private val stop by lazy {
findViewById<TextView>(R.id.stop)
}
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_donate_gift_press_test)
start.setOnClickListener {
animationRotation(rotationBg)
}
pause.setOnClickListener {
animatorSet.pause()
}
stop.setOnClickListener {
animatorSet.cancel()
}
framelayout.setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
actionDownTime = System.currentTimeMillis()
animationScale(framelayout, 0.9f, 1.0f)
}
MotionEvent.ACTION_UP ->{
animationScale(framelayout, 1.0f, 0.9f)
}
}
animatorSet?.start()
true
}
}
/**
* 旋转动画
*/
@SuppressLint("WrongConstant", "ObjectAnimatorBinding")
private fun animationRotation(view: View?) {
val objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 600f, 1200f, 1800f)
objectAnimator?.interpolator = LinearInterpolator()
objectAnimator.repeatCount = Animation.INFINITE
animatorSet?.duration = 10000L
animatorSet?.play(objectAnimator)
animatorSet?.start()
}
/**
* 缩放动画
*/
@SuppressLint("WrongConstant")
private fun animationScale(view: View?, scale1: Float, scale2: Float) {
objectAnimator?.setFloatValues(scale1, scale2)
objectAnimator?.addUpdateListener { animation ->
view?.scaleX = animation.animatedValue as Float
view?.scaleY = animation.animatedValue as Float
}
objectAnimator?.duration = System.currentTimeMillis() - actionDownTime
animatorSet?.play(objectAnimator)
}
}