Android屬性動畫小練習(簡單實現旋轉、平移、淡入淡出、縮放動畫效果)


 

Android屬性動畫小練習(簡單實現旋轉、平移、淡入淡出、縮放動畫效果)

 

                  ——安德風QQ1652102745

 

 

一、效果演示

 

二、布局設計activity_main.xml

 

 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     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <Button
10         android:id="@+id/btn2"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="使圖片圍繞X軸旋轉 "
14         android:onClick="myClick"
15         android:textSize="24sp"
16         app:layout_constraintEnd_toEndOf="parent"
17         app:layout_constraintHorizontal_bias="0.497"
18         app:layout_constraintStart_toStartOf="parent"
19         app:layout_constraintTop_toBottomOf="@+id/btn1" />
20 
21     <Button
22         android:id="@+id/btn4"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:text="使文字可以飛入 "
26         android:onClick="myClick"
27         android:textSize="24sp"
28         app:layout_constraintEnd_toEndOf="parent"
29         app:layout_constraintStart_toStartOf="parent"
30         app:layout_constraintTop_toBottomOf="@+id/btn3" />
31 
32     <Button
33         android:id="@+id/btn1"
34         android:layout_width="380dp"
35         android:layout_height="48dp"
36         android:onClick="myClick"
37         android:text="圖片先順時針旋轉再逆時針旋轉"
38         android:textSize="24sp"
39         app:layout_constraintBottom_toBottomOf="parent"
40         app:layout_constraintEnd_toEndOf="parent"
41         app:layout_constraintHorizontal_bias="0.516"
42         app:layout_constraintStart_toStartOf="parent"
43         app:layout_constraintTop_toTopOf="parent"
44         app:layout_constraintVertical_bias="0.734" />
45 
46     <Button
47         android:id="@+id/btn3"
48         android:layout_width="wrap_content"
49         android:layout_height="wrap_content"
50         android:onClick="myClick"
51         android:text="使圖片可以橫向放大2倍再還原 "
52         android:textSize="24sp"
53         app:layout_constraintEnd_toEndOf="parent"
54         app:layout_constraintHorizontal_bias="0.28"
55         app:layout_constraintStart_toStartOf="parent"
56         app:layout_constraintTop_toBottomOf="@+id/btn2" />
57 
58     <ImageView
59         android:id="@+id/img"
60         android:layout_width="161dp"
61         android:layout_height="123dp"
62         android:layout_marginTop="96dp"
63         app:layout_constraintEnd_toEndOf="parent"
64         app:layout_constraintHorizontal_bias="0.486"
65         app:layout_constraintStart_toStartOf="parent"
66         app:layout_constraintTop_toTopOf="parent"
67         app:srcCompat="@drawable/p" />
68 
69     <TextView
70         android:id="@+id/tv"
71         android:layout_width="wrap_content"
72         android:layout_height="wrap_content"
73         android:layout_marginBottom="64dp"
74         android:text="草莓"
75         android:textSize="30sp"
76         android:textStyle="bold"
77         app:layout_constraintBottom_toTopOf="@+id/btn1"
78         app:layout_constraintEnd_toEndOf="parent"
79         app:layout_constraintHorizontal_bias="0.498"
80         app:layout_constraintStart_toStartOf="parent"
81         app:layout_constraintTop_toBottomOf="@+id/img"
82         app:layout_constraintVertical_bias="0.837" />
83 
84 </androidx.constraintlayout.widget.ConstraintLayout>

 

三、功能實現MainActivity.java

 1 package com.example.myapplication;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.animation.AnimatorSet;
 6 import android.animation.ObjectAnimator;
 7 import android.app.Activity;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.view.animation.AnimationSet;
11 import android.widget.Button;
12 import android.widget.ImageView;
13 import android.widget.TextView;
14 
15 public class MainActivity extends AppCompatActivity {
16 ImageView img;//圖片控件聲明
17 TextView tv;//文本標簽聲明
18 Button btn1,btn2,btn3,btn4;//聲明按鈕1/2/3/4
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         img=findViewById(R.id.img);//綁定圖片控件ID
25         tv=findViewById(R.id.tv);//綁定文本控件ID
26         btn1=findViewById(R.id.btn1);//綁定按鈕1控件ID
27         btn2=findViewById(R.id.btn2);//綁定按鈕2控件ID
28         btn3=findViewById(R.id.btn3);//綁定按鈕3控件ID
29         btn4=findViewById(R.id.btn4);//綁定按鈕4控件ID
30     }
31 //對應按鈕實現功能
32     public void myClick(View view) {
33         switch (view.getId()){
34 
35             //實現使圖片先順時針旋轉再逆時針旋轉按鈕功能
36             case R.id.btn1:
37                 ObjectrotationAnim();//先順時針360度旋轉然后逆時針360度旋轉動畫的函數
38                 break;
39             //實現使圖片圍繞X軸旋轉按鈕功能
40             case R.id.btn2:
41                 ObjectrotationXAnim(); //實現使圖片圍繞X軸旋轉按鈕的函數
42                 break;
43             //實現使圖片可以橫向放大2倍再還原按鈕功能
44             case R.id.btn3:
45                 ObjectscaleAnim();
46                 break;
47             //實現使文字可以飛入按鈕功能
48             case R.id.btn4:
49                 ObjectfeiruAnimator();
50                 break;
51 
52         }
53 
54     }
55     //實現使文字可以飛入按鈕功能
56     private void ObjectfeiruAnimator() {
57        ObjectAnimator animator=ObjectAnimator.ofFloat(tv,"translationX",300,0);//文字標簽X軸平移
58         ObjectAnimator animator2=ObjectAnimator.ofFloat(tv,"translationY",200,0);//文字標簽Y軸平移
59         ObjectAnimator animator3=ObjectAnimator.ofFloat(tv,"alpha",0.0F,1.0F);//設置文字標簽淡入淡出效果(由透明到不透明)
60         animator2.setDuration(3000);//設置文字標簽X軸平移持續時長3000毫秒
61         animator.setDuration(3000);//設置文字標簽Y軸平移持續時長3000毫秒
62         animator3.setDuration(4000);//設置文字標簽淡入淡出持續時長4000毫秒
63         AnimatorSet animatorSet=new AnimatorSet();//創建動畫集
64         animatorSet.playTogether(animator,animator2,animator3);//組合動畫效果
65 //        animatorSet.setDuration(5000);
66         animatorSet.start();//開始執行動畫(文本飛入效果)
67     }
68 
69     //實現使圖片可以橫向放大2倍再還原按鈕功能
70     private void ObjectscaleAnim() {
71         ObjectAnimator animator=ObjectAnimator.ofFloat(img,"ScaleX",1.0F,2.0F,1.0F);//圖片默認X軸不變然后放大2倍然后恢復原樣
72         animator.setDuration(4000);//縮放動畫持續時間為4000毫秒
73         animator.start();//開始執行動畫(圖片橫向放大2倍動畫)
74     }
75 
76     //實現使圖片圍繞X軸旋轉按鈕功能
77     private void ObjectrotationXAnim() {
78       ObjectAnimator animator=ObjectAnimator.ofFloat(img,"rotationX",0.0F,180.0F,00.0F);//圖片默認X軸旋轉
79       animator.setDuration(2000);//旋轉動畫持續時間為2000毫秒
80       animator.start();//開始執行動畫(圖片X軸旋轉)
81     }
82 
83     //實現先順時針360度旋轉然后逆時針360度旋轉動畫功能
84     private void ObjectrotationAnim() {
85 
86         //構造ObjectAnimator對象的方法
87         ObjectAnimator animator = ObjectAnimator.ofFloat(img, "rotation", 0.0F, 360.0F,0.0F,-360.0F);//設置先順時針360度旋轉然后逆時針360度旋轉動畫
88         animator.setDuration(5000);//設置旋轉時間
89         animator.start();//開始執行動畫(順時針旋轉動畫)
90     }
91 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM