屬性動畫:html
動畫定義在xml文件中修改目標對象的某一個屬性,如background,alpha etc.java
文件位置:res/animator/xxx.xmlandroid
資源類型數據:ValueAnimator、ObjectAnimator、AnimatorSetapi
資源引用:in java---R.animator.xxx in xml------@[package:]animator/xxxapp
定義動畫的語法:框架
<set android:ordering=["together" | "sequentially"]> <objectAnimator android:propertyName="string" android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <animator android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <set> ... </set></set>
在該文件中只有一個根元素能夠是set、objectAnimator、valueAnimator固然你也能夠將它們組合在一個set中,set之間容許嵌套。ide
<set>:持有其餘動畫元素的容器,對應的java類AnimatorSet
.佈局
android:orderpost
指定<set>中動畫的播放順序測試
sequentially:按照
順序播放動畫
together
(default):同時播放因此動畫
<objectAnimator>:在一段時間內爲指定對象的某個屬性賦予動畫,對應java類
ObjectAnimator
.
android:propertyName
String Required,要實現動畫的屬性,你能夠指定諸如alpha,backgroundColor等,固然在objectAnimator中不能指定目標對象在xml配置文件中,你能夠經過loadAnimator()方法加載動畫並經過setTarget()方法爲動畫指定目標。
android:valueTo
float, int, or color. Required.表明屬性在動畫最後的值.
android:valueFrom
float, int, or color.屬性在動畫開始的值,若是沒有指定將經過屬性的get方法獲取默認值。
android:duration
int. 動畫的時間(毫秒)
android:startOffset
int . 動畫開始的延遲執行時間(毫秒)即調用start()後動畫延遲執行的時間。
android:repeatCount
int . 動畫重複執行的次數
-1和負數:無限循環執行
正數:執行相應的次數便可
0:執行1次後不循環
android:repeatMode
int . 動畫結束後如何重複(重複模式),須要repeatCount爲負數纔有效
reverse:動畫翻轉,有結束向開始動畫
repeat:重複動畫
android:valueType
Keyword.不能爲該屬性指定是顏色的值,animator框架會自動處理顏色值。
intType:指定動畫中值是整數
floatType
(default):指定動畫中的值是浮點數
<animator>:在指定的時間裏執行動畫,對應java類
ValueAnimator
屬性參考<ObjectAnimator>
Tween動畫是對視圖對象中的內容進行一系列簡單的轉換,好比位置的移動,大小的縮放,旋轉,透明度得變化等等。Tween動畫能夠寫到一個xml文件中,就像定義佈局文件同樣,固然,也能夠寫到android代碼中,不過推薦寫到xml文件中,由於它具有的閱讀性,可重用性大大超過了硬編碼。xml文件放在工程的res/anim目錄中,這個目錄中要包含一個根元素,能夠是<scale>,<translate>,<alpha>或者<rotate>,固然,這些元素均可以放到一個動畫集合<set>中,默認狀況下,全部的動畫指令都是同時發生的,爲了讓他們按順序發生,須要設置一個特殊的屬性startOffset。下面定義了一個動畫的集合:
1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android"
3 android:shareInterpolator="false">
4 <scale android:interpolator="@android :anim/accelerate_decelerate_interpolator"
5 android:fromXScale="1.0"
6 android:toXScale="1.4"
7 android:fromYScale="1.0"
8 android:toYScale="0.6"
9 android:pivotX="50%"
10 android:pivotY="50%"
11 android:fillAfter="false"
12 android:duration="700"/>
13
14 <set android:interpolator="@android :anim/decelerate_interpolator">
15 <scale
16 android:fromXScale="1.4"
17 android:toXScale="0.0"
18 android:fromYScale="0.6"
19 android:toYScale="0.0"
20 android:pivotX="50%"
21 android:pivotY="50%"
22 android:startOffset="700"
23 android:duration="400"
24 android:fillBefore="false"/>
25 <rotate
26 android:fromDegrees="0"
27 android:toDegrees="-45"
28 android:toYScale="0.0"
29 android:pivotX="50%"
30 android:pivotY="50%"
31 android:startOffset="700"
32 android:duration="400"/>
33 </set>
34 </set>
這裏解釋一下這段代碼的做用:
首先是一個動畫的集合set,在這個set中有一個屬性shareInterpolater,若是設置爲true,則這個集合下面全部的子元素都享有一樣的interpolater,api上面是這樣說的:
android:shareInterpolator
Boolean. "true" if you want to share the same interpolator among all child elements.
緊跟在這個集合後面的是一個縮放動畫,裏面有一些個屬性,下面一一介紹:
android:interpolator屬性:這是值定一個動畫的插入器,有一些經常使用的插入器:accelerate_decelerate_interpolator加速-減速動畫插入器,顧名思義,就是先加速後減速,accelerate_interpolator加速動畫插入器,decelerate_interpolator減速動畫插入器
android:fromXScale屬性爲動畫起始時,x座標上的伸縮尺寸
android:toXScal屬性爲動畫結束時,x座標上的伸縮尺寸
android:fromYScale屬性爲動畫起始時,y座標上的伸縮尺寸
android:toYScale屬性爲動畫結束時,y座標上的伸縮尺寸
關於伸縮尺寸這裏還要羅嗦一下:
也就是上面的四個屬性的值:0.0表示收縮到沒有,1.0表示正常無收縮,值小於1.0表示收縮,值大於1.0表示放大。
android:fillAfter屬性當設置爲true時,該動畫轉化在動畫結束後被應用,同理還有android:fillBefore屬性,當設置爲true時,該動畫轉化在動畫開始前被應用
android:duration屬性表示動畫持續的時間,單爲時毫秒
android:pivotX屬性爲動畫相對於x座標的起始位置
android:pivotY屬性爲動畫相對於y座標的起始位置
這2個屬性有不一樣的格式表示,如值爲50,表示是相對於父類的50%,如值爲50%,表示是相對於本身的50%
這裏的50%表示相對於自身x,y座標上的中點位置
緊跟着是一個動畫集合,裏面有縮放和旋轉,這個集合的interpolater爲減速動畫插入器
這裏的縮放裏面還有一個屬性,android:startOffset屬性是設置動畫開始的時間,這裏設置700是表示700毫秒以後開始,也就是第一個縮放完成以後開始。
旋轉裏面的屬性跟scale裏面的都差很少,只是旋轉講究的角度。
android:fromDegrees屬性表示動畫起始時的角度
android:toDegrees屬性表示動畫結束時旋轉的角度,能夠大於360度
動畫文件寫好了以後,咱們就能夠在代碼中調用這個動畫了,先寫一個佈局文件,佈局文件裏面有一個ImageView,而後咱們讓這個ImageView作動畫。
佈局文件以下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent">
6 <ImageView
7 android:id="@+id/imageView1"
8 android:src="@drawable/duola"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"></ImageView>
11 </LinearLayout>
而後咱們讓這個圖片按照咱們xml中指定的動畫運動:
代碼:
package com.test.shang;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class TestStyle extends Activity {
AnimationDrawable animationDrawable;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Animation animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set);
iv.setAnimation(animation);
animation.start();
}
}
由於這裏是動畫,很差截圖,因此我就不截圖了,具體效果你們能夠試試看。
下面接着說說Frame Animation吧:也就是幀動畫,可使用AndroidDrawable來負責幀動畫,一樣它能夠在xml文件中很方便的列出全部的幀,按照週期去執行每幀動畫,下面是一個定義幀動畫的例子:
1 <?xml version="1.0" encoding="utf-8"?>
2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
3 android:oneshot="true">
4 <item android:drawable="@drawable/register" android:duration="500"/>
5 <item android:drawable="@drawable/duola" android:duration="500"/>
6 <item android:drawable="@drawable/icon" android:duration="500"/>
7 </animation-list>
這裏定義了幀的名字和每幀的持續時間.
這裏有3幀,經過設置android:oneshot屬性爲true,它將會在最後一幀停下來,若是設置爲false,它將會循環播放,能夠把它添加到一個背景中,讓他播放,具體代碼以下:
佈局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent">
6 <ImageView
7 android:id="@+id/imageView1"
8 android:layout_width="match_parent"
9 android:layout_height="match_parent"></ImageView>
10 </LinearLayout>
在代碼裏面設置imageview的背景圖片,而後作動畫:
1 package com.test.shang;
2
3 import android.app.Activity;
4 import android.graphics.drawable.AnimationDrawable;
5 import android.graphics.drawable.TransitionDrawable;
6 import android.os.Bundle;
7 import android.view.MotionEvent;
8 import android.view.animation.Animation;
9 import android.view.animation.AnimationSet;
10 import android.view.animation.AnimationUtils;
11 import android.widget.ImageView;
12
13 public class TestStyle extends Activity {
14 AnimationDrawable animationDrawable;
15
16 @Override
17 protected void onCreate (Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.test);
20 ImageView iv = (ImageView) findViewById(R.id.imageView1);
21 iv.setBackgroundResource(R.anim.anim_list);
22 animationDrawable = (AnimationDrawable) iv.getBackground();
23 }
24 @Override
25 public boolean onTouchEvent (MotionEvent event) {
26 if(event.getAction() == MotionEvent.ACTION_DOWN) {
27 animationDrawable.start();
28 return true;
29 }
30 return super.onTouchEvent(event);
31 }
32 }
這裏須要注意的是:AnimationDrawable在調用OnCreate的過程當中不能調用start(),這是由於AnimationDrawable不能在不徹底的窗口上運行,須要一個操做來觸發,若是你想當即播放動畫,沒有必要的交互,你能夠在onWindowFocusChanged()方法中調用它,這樣它將會成爲窗口焦點。
下面說說圖片的縮放和旋轉:
這裏我就寫的比較簡單了,代碼裏面的註釋很詳細,能夠慢慢看。
1 package com.test.shang;
2
3 import android.app.Activity;
4 import android.graphics.Bitmap;
5 import android.graphics.BitmapFactory;
6 import android.graphics.Matrix;
7 import android.graphics.drawable.BitmapDrawable;
8 import android.os.Bundle;
9 import android.widget.ImageView;
10 import android.widget.ImageView.ScaleType;
11 import android.widget.LinearLayout;
12 import android.widget.LinearLayout.LayoutParams;
13
14 public class BitmapTest extends Activity {
15
16 @Override
17 protected void onCreate (Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setTitle("測試圖片的縮放和旋轉");
20 LinearLayout layout = new LinearLayout(this);
21
22 //加載須要操做的圖片,這裏是機器貓的圖片
23 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.duola);
24
25 //獲取這個圖片的寬和高
26 int width = bitmapOrg.getWidth();
27 int height = bitmapOrg.getHeight();
28
29 //定義預轉換成的圖片的寬和高
30 int newWidth = 200;
31 int newHight = 200;
32
33 //計算縮放率,新尺寸除原尺寸
34 float scaleWidth = (float)newWidth/width;
35 float scaleHeight = (float)newHight/height;
36
37 //建立操做圖片用的matrix對象
38 Matrix matrix = new Matrix();
39
40 //縮放圖片動做
41 matrix.postScale(scaleWidth, scaleHeight);
42
43 //旋轉圖片動做
44 matrix.postRotate(45);
45
46 //建立新的圖片
47 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
48
49 //將上面建立的Bitmap轉換成Drawable對象,使得其可使用在imageView,imageButton上。
50 BitmapDrawable bitmapDrawable = new BitmapDrawable(resizedBitmap);
51
52 //建立一個ImageView
53 ImageView iv = new ImageView(this);
54
55 //將imageView的圖片設置爲上面轉換的圖片
56 iv.setImageDrawable(bitmapDrawable);
57
58 //將圖片居中顯示
59 iv.setScaleType(ScaleType.CENTER);
60
61 //將imageView添加到佈局模板中
62 layout.addView(iv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
63
64 //設置爲本activity的模板
65 setContentView(layout);
66 }
67 }