動畫分類:Animation 單一動畫android
AnimationSet 複合動畫app
AnimationSet是Animation的實現子類,Animation是一個抽象類,他的實現子類主要有以下幾種:ide
主要有縮放 ScaleAnimation ,平移TranslateAnimation,透明(不清楚)AlphaAnimation,旋轉 RotateAnimation。佈局
Animation做爲父類的公用方法:測試
SetDuration(..);設置的是動畫持續的時間,單位是毫秒:1000=1秒動畫
SetFillAfter(..);設置動畫結束後是否保持狀態,false則返回最初狀態;this
SetFillBefore(..);spa
SetFillEnable(..); //這兩個搞不懂code
SetStartOffSet(..);這是動畫開始延時時間;xml
還有幾個通用屬性:
pivotXType: 指定x軸中心的類型有三種參數:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT
pivotYtype:指定Y軸中心的類型同x軸
動畫移動選擇或者縮放都有一箇中心點,這個中心點默認原點是視圖左上角的點
pivotXTyp的做用就是指定中心點。若是設置成絕對路徑Animation.ABSOLUTE,那麼就須要計算距離這個原點的距離,把x軸中心設置到x的中間,那麼就是view.getWidth()/2
若是設置成相對自身Animation.RELATIVE_TO_SELF,設置成0.5f就能夠了,這個要注意理解。
最後一個是相對父視圖的絕對距離這個計算相對麻煩。
如下是主程序:
package com.skymaster.hs.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class AnimationTest extends AppCompatActivity { private ImageView iv_animation; private Button btn_start1,btn_start2,btn_start3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_animation_test); iv_animation = (ImageView) findViewById(R.id.iv_animation); btn_start1 = (Button) findViewById(R.id.btn_start1); btn_start2 = (Button) findViewById(R.id.btn_start2); btn_start3 = (Button) findViewById(R.id.btn_start3); btn_start1.setOnClickListener(new KeyPress()); btn_start2.setOnClickListener(new KeyPress()); btn_start3.setOnClickListener(new KeyPress()); iv_animation.setOnClickListener(new View.OnClickListener() { //這裏設置ImageView點擊監聽是爲了測試當動畫移走以後,點擊ImageView //原來的位置是否有效果,實測是有的,也就是說即便ImageView移走了可是它的 //按鍵有效位置仍是xml佈局的那個位置。 @Override public void onClick(View v) { Toast.makeText(AnimationTest.this,"ImageClick",Toast.LENGTH_SHORT) .show(); } }); } private class KeyPress implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_start1: //設定x軸座標中心點(0,2f),2f是移動自身寬度的兩倍 //設定y軸座標中心點(0,1.5f),同理x軸 TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.RELATIVE_TO_SELF,2.0f ,Animation.ABSOLUTE,0,Animation.RELATIVE_TO_SELF,1.5f); animation.setDuration(2000);//持續時間 animation.setStartOffset(1000); animation.setFillAfter(true);//動畫完成保持不會原位 //設定動畫監聽 animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Toast.makeText(AnimationTest.this,"start!",Toast.LENGTH_SHORT) .show(); } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(AnimationTest.this,"End!",Toast.LENGTH_SHORT) .show(); } @Override public void onAnimationRepeat(Animation animation) { Toast.makeText(AnimationTest.this,"Repeat!",Toast.LENGTH_SHORT) .show(); } }); iv_animation.startAnimation(animation); break; case R.id.btn_start2: //設定縮放中心點與縮放比例x軸是0.2f放大到2f,這裏都是相對距離 //y軸縮放比例同理,從0.2到3f是放大若是反過來就是縮小 //後面4個參數是設置縮放中心點,用的是絕對距離 ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f,2f,0.2f,3f,Animation.ABSOLUTE ,iv_animation.getWidth()/2,Animation.ABSOLUTE,0); scaleAnimation.setDuration(2000); scaleAnimation.setFillAfter(false); iv_animation.startAnimation(scaleAnimation); break; case R.id.btn_start3: //旋轉動畫 RotateAnimation rotateAnimation = new RotateAnimation(0f,180f,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); //rotateAnimation.setFillAfter(true); rotateAnimation.setFillEnabled(true); rotateAnimation.setFillBefore(true); rotateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Toast.makeText(AnimationTest.this, "rotateStart!", Toast.LENGTH_SHORT) .show(); } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(AnimationTest.this, "rotateEnd!", Toast.LENGTH_SHORT) .show(); //iv_animation.startAnimation(animation); } @Override public void onAnimationRepeat(Animation animation) { Toast.makeText(AnimationTest.this, "rotateRepeat!", Toast.LENGTH_SHORT) .show(); } }); rotateAnimation.setDuration(2000); rotateAnimation.setStartOffset(1000); iv_animation.startAnimation(rotateAnimation); break; default:break; } } } }
xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.skymaster.hs.myapplication.AnimationTest"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:background="@drawable/ic_launcher" android:id="@+id/iv_animation"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start1" android:layout_alignParentBottom="true" android:id="@+id/btn_start1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start2" android:layout_alignParentBottom="true" android:layout_toRightOf="@id/btn_start1" android:id="@+id/btn_start2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start3" android:layout_toRightOf="@id/btn_start2" android:layout_alignParentBottom="true" android:id="@+id/btn_start3"/> </RelativeLayout>
AnimationSet能夠配置多個動畫
除了Animation以外還有xml利用多幅圖片來實現動畫。
利用Animation實現動畫,屬性並並無隨着動畫移動。上面例子中ImageView的點擊事件仍是在最初的位置,要實現屬性動畫就要利用ObjectAnimation類