使用 Kotlin 的永久旋转动画
Permanent RotationAnimation with Kotlin
对于我的应用程序项目,我计划了一个欢迎 activity,允许用户选择一组国家/地区。
为此,我有两个按钮:第一个将允许包含 4 个 TextView 的 CardView 旋转 90°,直到国家组的 TexView 可读(其他 Textview 的方向不同)。
下面的代码适用于 Texview,但我的问题是旋转的永久效果。
我添加了以下行:rotateanimation.setFillAfter(true)
效果很好,但只有一半!
如果我们启动一个新的旋转,动画从头开始,而我的目标是将这个旋转延长 90°,完成一个完整的旋转。
主要activity:
package training.geography.rotation
import android.content.Intent
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity() : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.group1)
val buttonrotate = findViewById<Button>(R.id.rotatebutton)
val rotateanimation = RotateAnimation(0f, 90f, Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f)
buttonrotate.setOnClickListener {
rotateanimation.setDuration(1000)
rotateanimation.setFillAfter(true)
textView.setAnimation(rotateanimation)
textView.startAnimation(rotateanimation)
}
val buttonchoose= findViewById<Button>(R.id.choosebutton)
buttonchoose.setOnClickListener {
val intent = Intent(this, TripleRecyclerView::class.java)
startActivity(intent)
}
}
}
activity_main xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/group1"
android:layout_width="220dp"
android:layout_height="220dp"
android:text="@string/countriesgroup1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
android:layout_marginTop="128dp"
app:layout_constraintHorizontal_bias="0.497"/>
<Button android:id="@+id/rotatebutton"
android:text="Rotate to choose Group of Countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/group1"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp" />
<Button android:id="@+id/choosebutton"
android:text="Choose Group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/rotatebutton"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result after rotation 90°
旋转效果很好,但如何使其永久旋转并在 4 次点击中执行完整旋转?
那是因为您总是告诉视图从 0 度旋转到 90 度,而不是从 90 度旋转到 180 度,等等。
你可以做的是像这样在视图上调用动画:
view.animate().apply{
rotationBy(90f)
duration = 1000L
start()
}
对于我的应用程序项目,我计划了一个欢迎 activity,允许用户选择一组国家/地区。 为此,我有两个按钮:第一个将允许包含 4 个 TextView 的 CardView 旋转 90°,直到国家组的 TexView 可读(其他 Textview 的方向不同)。 下面的代码适用于 Texview,但我的问题是旋转的永久效果。
我添加了以下行:rotateanimation.setFillAfter(true)
效果很好,但只有一半!
如果我们启动一个新的旋转,动画从头开始,而我的目标是将这个旋转延长 90°,完成一个完整的旋转。 主要activity:
package training.geography.rotation
import android.content.Intent
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity() : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.group1)
val buttonrotate = findViewById<Button>(R.id.rotatebutton)
val rotateanimation = RotateAnimation(0f, 90f, Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f)
buttonrotate.setOnClickListener {
rotateanimation.setDuration(1000)
rotateanimation.setFillAfter(true)
textView.setAnimation(rotateanimation)
textView.startAnimation(rotateanimation)
}
val buttonchoose= findViewById<Button>(R.id.choosebutton)
buttonchoose.setOnClickListener {
val intent = Intent(this, TripleRecyclerView::class.java)
startActivity(intent)
}
}
}
activity_main xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/group1"
android:layout_width="220dp"
android:layout_height="220dp"
android:text="@string/countriesgroup1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
android:layout_marginTop="128dp"
app:layout_constraintHorizontal_bias="0.497"/>
<Button android:id="@+id/rotatebutton"
android:text="Rotate to choose Group of Countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/group1"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp" />
<Button android:id="@+id/choosebutton"
android:text="Choose Group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/rotatebutton"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result after rotation 90°
旋转效果很好,但如何使其永久旋转并在 4 次点击中执行完整旋转?
那是因为您总是告诉视图从 0 度旋转到 90 度,而不是从 90 度旋转到 180 度,等等。 你可以做的是像这样在视图上调用动画:
view.animate().apply{
rotationBy(90f)
duration = 1000L
start()
}