Skip to content

Commit 9a29833

Browse files
add rotationY, rotationX, and card flip animations
1 parent ba49f6e commit 9a29833

File tree

6 files changed

+172
-6
lines changed

6 files changed

+172
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.iml
22
.gradle
33
/local.properties
4+
/.idea/*
45
/.idea/caches
56
/.idea/libraries
67
/.idea/modules.xml
@@ -12,4 +13,4 @@
1213
/captures
1314
.externalNativeBuild
1415
.cxx
15-
local.properties
16+
local.properties

Tutorial1-1Basics/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<activity android:name=".chapter2_animate_views.Activity2_1PulseView" />
2222
<activity android:name=".chapter2_animate_views.Activity2_2RevealHideCrossFade" />
2323
<activity android:name=".chapter2_animate_views.Activity2_3CircularReveal" />
24+
<activity android:name=".chapter2_animate_views.Activity2_4CardFlipAnimation" />
2425
</application>
2526

2627
</manifest>

Tutorial1-1Basics/src/main/java/com/example/tutorial1_1basics/MainActivity.kt

+13-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.example.tutorial1_1basics.chapter1_basics.Activity1_2AnimatorInflater
1212
import com.example.tutorial1_1basics.chapter2_animate_views.Activity2_1PulseView
1313
import com.example.tutorial1_1basics.chapter2_animate_views.Activity2_2RevealHideCrossFade
1414
import com.example.tutorial1_1basics.chapter2_animate_views.Activity2_3CircularReveal
15+
import com.example.tutorial1_1basics.chapter2_animate_views.Activity2_4CardFlipAnimation
1516

1617
import com.example.tutorial1_1basics.chapter_adapter.BaseAdapter
1718
import com.example.tutorial1_1basics.chapter_adapter.ChapterSelectionAdapter
@@ -64,35 +65,42 @@ class MainActivity : AppCompatActivity(), BaseAdapter.OnRecyclerViewItemClickLis
6465
activityClassModels.add(
6566
ActivityClassModel(
6667
Activity1_1Basics::class.java,
67-
"Animation with ObjectAnimator"
68+
"1-1 Animation with ObjectAnimator"
6869
)
6970
)
7071

7172
activityClassModels.add(
7273
ActivityClassModel(
7374
Activity1_2AnimatorInflater::class.java,
74-
"Animation with AnimatorInflater"
75+
"1-2 Animation with AnimatorInflater"
7576
)
7677
)
7778

7879
activityClassModels.add(
7980
ActivityClassModel(
8081
Activity2_1PulseView::class.java,
81-
"Animation with Custom View"
82+
"2-1 Animation with Custom View"
8283
)
8384
)
8485

8586
activityClassModels.add(
8687
ActivityClassModel(
8788
Activity2_2RevealHideCrossFade::class.java,
88-
"Animation Hide/Reveal crossFade"
89+
"2-2 Animation Hide/Reveal crossFade"
8990
)
9091
)
9192

9293
activityClassModels.add(
9394
ActivityClassModel(
9495
Activity2_3CircularReveal::class.java,
95-
"Animation Circular Reveal"
96+
"2-3 Animation Circular Reveal"
97+
)
98+
)
99+
100+
activityClassModels.add(
101+
ActivityClassModel(
102+
Activity2_4CardFlipAnimation::class.java,
103+
"2-4 Rotation X/Y and Flip"
96104
)
97105
)
98106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.example.tutorial1_1basics.chapter2_animate_views
2+
3+
import android.animation.Animator
4+
import android.animation.AnimatorListenerAdapter
5+
import android.animation.ObjectAnimator
6+
import android.os.Bundle
7+
import android.view.View
8+
import android.widget.Button
9+
import android.widget.ImageView
10+
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.core.content.ContextCompat
12+
import com.example.tutorial1_1basics.R
13+
14+
class Activity2_4CardFlipAnimation : AppCompatActivity() {
15+
16+
override fun onCreate(savedInstanceState: Bundle?) {
17+
super.onCreate(savedInstanceState)
18+
setContentView(R.layout.activity2_4card_flip)
19+
20+
val imageView = findViewById<ImageView>(R.id.imageView)
21+
22+
val buttonRotateY = findViewById<Button>(R.id.buttonRotateY)
23+
val buttonRotateX = findViewById<Button>(R.id.buttonRotateX)
24+
val buttonFlip = findViewById<Button>(R.id.buttonFlip)
25+
26+
buttonRotateY.setOnClickListener {
27+
val objectAnimator =
28+
ObjectAnimator.ofFloat(
29+
imageView,
30+
View.ROTATION_Y,
31+
imageView.rotationY,
32+
imageView.rotationY + 180f
33+
)
34+
objectAnimator.duration = 1000
35+
objectAnimator.start()
36+
}
37+
38+
buttonRotateX.setOnClickListener {
39+
val objectAnimator =
40+
ObjectAnimator.ofFloat(
41+
imageView, View.ROTATION_X,
42+
imageView.rotationX,
43+
imageView.rotationX + 180f
44+
)
45+
objectAnimator.duration = 1000
46+
objectAnimator.start()
47+
}
48+
49+
var isFirstImage = true
50+
51+
val scale = applicationContext.resources.displayMetrics.density
52+
imageView.cameraDistance = 4000 * scale
53+
54+
buttonFlip.setOnClickListener {
55+
56+
val objectAnimatorFirst =
57+
ObjectAnimator.ofFloat(imageView, View.ROTATION_Y, 0f, 90f)
58+
objectAnimatorFirst.duration = 500
59+
objectAnimatorFirst.start()
60+
61+
objectAnimatorFirst.addListener(object : AnimatorListenerAdapter() {
62+
63+
override fun onAnimationEnd(animation: Animator?) {
64+
super.onAnimationEnd(animation)
65+
66+
// Set image in halfway
67+
isFirstImage = !isFirstImage
68+
imageView.swapImage(isFirstImage)
69+
70+
val objectAnimatorSecond =
71+
ObjectAnimator.ofFloat(imageView, View.ROTATION_Y, -90f, 0f)
72+
objectAnimatorSecond.duration = 500
73+
objectAnimatorSecond.start()
74+
}
75+
})
76+
}
77+
}
78+
79+
private fun ImageView.swapImage(isFirstImage: Boolean) {
80+
81+
if (isFirstImage) {
82+
setImageDrawable(
83+
ContextCompat.getDrawable(
84+
applicationContext,
85+
R.drawable.mountains
86+
)
87+
)
88+
} else {
89+
setImageDrawable(
90+
ContextCompat.getDrawable(
91+
applicationContext,
92+
R.drawable.landscape
93+
)
94+
)
95+
}
96+
97+
}
98+
99+
100+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<androidx.appcompat.widget.AppCompatImageView
8+
android:id="@+id/imageView"
9+
android:layout_width="match_parent"
10+
android:layout_height="0dp"
11+
android:scaleType="fitXY"
12+
android:src="@drawable/mountains"
13+
app:layout_constraintBottom_toBottomOf="parent"
14+
app:layout_constraintDimensionRatio="4:3"
15+
app:layout_constraintEnd_toEndOf="parent"
16+
app:layout_constraintHorizontal_bias="0.0"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
20+
<com.google.android.material.button.MaterialButton
21+
android:id="@+id/buttonRotateY"
22+
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:layout_marginBottom="48dp"
26+
android:text="Rotate Y"
27+
app:layout_constraintBottom_toBottomOf="parent"
28+
app:layout_constraintEnd_toStartOf="@id/buttonRotateX"
29+
app:layout_constraintStart_toStartOf="parent"
30+
app:strokeColor="@color/design_default_color_primary" />
31+
32+
<com.google.android.material.button.MaterialButton
33+
android:id="@+id/buttonRotateX"
34+
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:layout_marginBottom="48dp"
38+
android:text="Rotate X"
39+
app:layout_constraintBottom_toBottomOf="parent"
40+
app:layout_constraintEnd_toStartOf="@id/buttonFlip"
41+
app:layout_constraintStart_toEndOf="@id/buttonRotateY"
42+
app:strokeColor="@color/design_default_color_primary" />
43+
44+
<com.google.android.material.button.MaterialButton
45+
android:id="@+id/buttonFlip"
46+
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:layout_marginBottom="48dp"
50+
android:text="Flip"
51+
app:layout_constraintBottom_toBottomOf="parent"
52+
app:layout_constraintEnd_toEndOf="parent"
53+
app:layout_constraintStart_toEndOf="@id/buttonRotateX"
54+
app:strokeColor="@color/design_default_color_primary" />
55+
56+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)