Android學習—補間動畫(漸變更畫)

使用xml的方式設置動畫屬性java

 1   <?xml version="1.0" encoding="utf-8"?>
 2    <set xmlns:android="http://schemas.android.com/apk/res/android">
 3        <!--
 4        fromAlpha:動畫的初始狀態
 5        toAlpha:動畫的最終狀態
 6        duration:動畫從初始狀態到最終狀態的持續時間
 7        repeatCount:不包括第一次顯示的,顯示次數
 8         -->
 9        <alpha
10         android:fromAlpha="0.1"
11         android:toAlpha="1.0"
12         android:duration="2000"
13         android:repeatCount="3"
14          />
15  </set>
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3      android:layout_height="match_parent"
 4      tools:context="com.test2_23.test2_23.AlphaAnimation_Activity">
 5   
 6       <ImageView
 7          android:id="@+id/img_Alpha"
 8          android:src="@drawable/img_bird"
 9          android:layout_width="wrap_content"
10          android:layout_height="wrap_content"
11          android:layout_centerInParent="true"
12         />
13  
14 </RelativeLayout>
 1 public class AlphaAnimation_Activity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_alpha_animation_);
 7         //獲取到要設置動畫的控件
 8         ImageView iv = (ImageView) findViewById(R.id.img_Alpha);
 9         //獲取動畫文件
10         Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation);
11         //清除以前的動畫
12         iv.clearAnimation();
13         //開始動畫
14         iv.startAnimation(animation);
15     }
16 }

使用java的方式來設置動畫屬性android

 1 public class javaAlpha_Activity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_java_alpha_);
 7         ImageView img = (ImageView) findViewById(R.id.img_Alpha_java);
 8         //使用java代碼實現也很是簡單,直接設置一個Animation類的對象就能夠了
 9         //AlphaAnimation 是Animation的子類
10         //在構造函數中設置他的初始狀態fromAlpha和最終狀態toAlpha,是float類型
11         Animation animation = new AlphaAnimation(0.1f,1.0f);
12         //設置持續時間
13         animation.setDuration(2000);
14         //設置重複次數
15         animation.setRepeatCount(3);
16         //清除原有的動畫,避免屢次點擊出現重複的效果
17         img.clearAnimation();
18         //開始執行動畫
19         img.startAnimation(animation);
20     }
21 }

 

補間動畫(TweenAnimation):補間動畫就是肯定了動畫的啓始和終點兩個端點狀態以後,由系統自動計算中間的各個狀態,並補充到兩個端點之間,造成一個連續的動畫效果。ide

補間動畫能夠分爲4種類型:函數

漸變更畫(AlphaAnimation)動畫

縮放動畫 (scaleAnimation)this

平移動畫(TranslateAnimation)spa

旋轉動畫(RotateAnimation)code

 

這幾種動畫均可以用xml文件和java代碼的方式實現,xml

漸變更畫xml實現方式:對象

1.在res目錄下建立文件夾anim

2.在anim文件夾下建立animation.xml動畫文件

3.在animation.xml中描述某種動畫的屬性

4.在java代碼中用AnimationUtils.loadAnimation方法加載動畫

5.使用View的startAnimation方法啓動動畫

 

漸變更畫效果:能夠演示一個視圖在透明度上的漸變效果。

主要屬性:

FromAlpha:動畫啓始透明度,取值在0.0-1.0之間

toAlpha:動畫結束時透明度,取值在0.0-1.0之間

duration:動畫從開始到結束的持續時間以毫秒作單位

repeatCount :動畫的重複次數,不包括第一次播放

相關文章
相關標籤/搜索