??Android中有四大動畫,它們分別是:變換動畫(Tween Animation),幀動畫(frame Animation),布局動畫(layout Animation),屬性動畫(Property Animation )。今天我在這里簡單的說下變換動畫。
Animation的通用屬性
xml屬性 | java方法 | 解釋 |
---|---|---|
android:detachWallpaper | setDetachWallpaper(boolean) | 是否在在壁紙上運行 |
android:duration | setDuration(long) | 動畫持續時間,單位為毫秒 |
android:fillAfter | setFillAfter(boolean) | 控件結束后是否保持動畫最后的狀態 |
android:fillBefore | setFillBefore(boolean) | 控件結束后是否還原到開始動畫的狀態 |
android:fillEnabled | setFillEnabled(boolean) | 與android:fillBefore效果相同 |
android:interpolator | setInterpolator(Interpolator) | 設定插值器(指定的動畫效果,譬如回彈等) |
android:repeatCount | setRepeatCount(int) | 動畫重復次數 |
android:repeatMode | setRepeatMode(int) | 重復類型有兩個值,reverse表示倒序回放,restart表示從頭播放 |
android:startOffset | setStartOffset(long) | 調用start函數之后等待開始運行的時間,單位為毫秒 |
android:zAdjustment | setZAdjustment(int) | 表示被設置動畫的內容運行時在Z軸上的位置默認為normal |
??變換動畫中分為4種:透明度動畫,縮放動畫,旋轉動畫,位移動畫。今天我在這里會一一說明變換動畫的四種動畫的使用方法.
1.透明度動畫
(1). 屬性
xml屬性 | java方法 | 解釋 |
---|---|---|
android:fromAlpha | AplphaAnimation(float fromAlpah,...) | 動畫開始的透明度(0.0,到1.0,0.0是全透明,1.0是不透明) |
android:toAlppha | AlphaAnimation(..., float toAlpha) | 動畫結束的透明度 |
(2). 用法
??我們現在res文件下新建一個anim文件夾,用來存放動畫的xml文件。
anim下的xml代碼
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1.0"
/>
</set>
布局文件xml代碼
<LinearLayout 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:orientation="vertical"
tools:context="com.example.Tween_Animation.Alpha_MainActivity" >
<Button
android:id="@+id/button_scale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_stringScaleAnimation" />
<LinearLayout
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
activity中的JAVA代碼
package com.example.Tween_Animation;
import com.example.androidanimation.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class Alpha_MainActivity extends Activity implements OnClickListener{
private Button button = null;
private ImageView imageview = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView) findViewById(R.id.imageview_scale);
button = (Button) findViewById(R.id.button_scale);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//通過AnimationUtils里面的一個靜態方法--loadAnimation用來綁定這個動畫變換的xml文件
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
//imageview加載animation對象
imageview.startAnimation(animation);
}
}
2.縮放動畫
(1). 屬性
xml屬性 | JAVA方法 | 解釋 |
---|---|---|
android:fromXScale | ScaleAnimation(float fromX, …) | 初始X軸縮放比例,1.0表示無變化 |
android:toXScale | ScaleAnimation(…, float toX, …) | 結束X軸縮放比例 |
android:fromYScale | ScaleAnimation(…, float fromY, …) | 初始Y軸縮放比例 |
android:toYScale | ScaleAnimation(…, float toY, …) | 結束Y軸縮放比例 |
android:pivotX | ScaleAnimation(…, float pivotX, …) | 縮放起點X軸坐標(數值、百分數、百分數p,譬如50表示以當前View左上角坐標加50px為初始點、50%表示以當前View的左上角加上當前View寬高的50%做為初始點、50%p表示以當前View的左上角加上父控件寬高的50%做為初始點) |
ScaleAnimation(…, float pivotX, …) | ScaleAnimation(…, float pivotY) | 縮放七點Y軸坐標,同上規律 |
(2). 用法
A.anim下的xml代碼
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="5000"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:toXScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
/>
</set>
B.布局文件xml代碼
??xml代碼:同上的布局代碼
C.activity的代碼
package com.example.Tween_Animation;
import com.example.androidanimation.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class Scale_MainActivity extends Activity implements OnClickListener{
private Button button = null;
private ImageView imageview = null;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button_scale);
imageview = (ImageView) findViewById(R.id.imageview_scale);
button.setOnClickListener(this);
}
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
imageview.startAnimation(animation);
}
}
3.旋轉動畫
(1). 屬性
xml屬性 | JAVA方法 | 解釋 |
---|---|---|
android:fromDegrees | RotateAnimation(float fromDegrees, …) | 旋轉開始角度,正代表順時針度數,負代表逆時針度數 |
android:toDegrees | RotateAnimation(…, float toDegrees, …) | 旋轉結束角度,正代表順時針度數,負代表逆時針度數 |
android:pivotX | RotateAnimation(…, float pivotX, …) | 縮放起點X坐標(數值、百分數、百分數p,譬如50表示以當前View左上角坐標加50px為初始點、50%表示以當前View的左上角加上當前View寬高的50%做為初始點、50%p表示以當前View的左上角加上父控件寬高的50%做為初始點) |
android:pivotY | RotateAnimation(…, float pivotY) | 縮放起點Y坐標,同上規律 |
(2). 用法
A. anim下的xml代碼
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:repeatCount="5"
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+360" />
</set>
B.布局文件xml代碼
??xml代碼:同上的布局代碼
C. activity的代碼
package com.example.Tween_Animation;
import com.example.androidanimation.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class Rotate_MainActivity extends Activity implements OnClickListener{
private Button button = null;
private ImageView imageview = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView) findViewById(R.id.imageview_scale);
button = (Button) findViewById(R.id.button_scale);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//通過AnimationUtils里面的一個靜態方法--loadAnimation用來綁定這個動畫變換的xml文件
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//imageview加載animation對象
imageview.startAnimation(animation);
}
}
4.位移動畫
(1). 屬性
xml屬性 | JAVA方法 | 解釋 |
---|---|---|
android:fromXDelta | TranslateAnimation(float fromXDelta, …) | 起始點X軸坐標(數值、百分數、百分數p,譬如50表示以當前View左上角坐標加50px為初始點、50%表示以當前View的左上角加上當前View寬高的50%做為初始點、50%p表示以當前View的左上角加上父控件寬高的50%做為初始點) |
android:fromYDelta | TranslateAnimation(…, float fromYDelta, …) | 起始點Y軸從標,同上規律 |
android:toXDelta | TranslateAnimation(…, float toXDelta, …) | 結束點X軸坐標,同上規律 |
android:toYDelta | TranslateAnimation(…, float toYDelta) | 結束點Y軸坐標,同上規律 |
(2). 用法
A.anim下的xml代碼
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromXDelta="10"
android:fromYDelta="10"
android:toXDelta="1000"
android:toYDelta="1000" />
</set>
B.布局文件xml代碼
??xml代碼:同上的布局代碼
C. activity的代碼
package com.example.Tween_Animation;
import com.example.androidanimation.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class Translate_MainActivity extends Activity implements OnClickListener{
private Button button = null;
private ImageView imageview = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView) findViewById(R.id.imageview_scale);
button = (Button) findViewById(R.id.button_scale);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//通過AnimationUtils里面的一個靜態方法--loadAnimation用來綁定這個動畫變換的xml文件
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
//imageview加載animation對象
imageview.startAnimation(animation);
}
}
??以上就是變換動畫中的四種情況。