Android 動畫之ScaleAnimation應用詳解
阿新 • • 發佈:2019-01-01
android中提供了4中動畫: AlphaAnimation 透明度動畫效果 ScaleAnimation 縮放動畫效果 TranslateAnimation 位移動畫效果 RotateAnimation 旋轉動畫效果 本節講解ScaleAnimation 動畫, ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 引數說明: 複製程式碼 程式碼如下: float fromX 動畫起始時 X座標上的伸縮尺寸 float toX 動畫結束時 X座標上的伸縮尺寸 float fromY 動畫起始時Y座標上的伸縮尺寸 float toY 動畫結束時Y座標上的伸縮尺寸 int pivotXType 動畫在X軸相對於物件位置型別 float pivotXValue 動畫相對於物件的X座標的開始位置 int pivotYType 動畫在Y軸相對於物件位置型別 float pivotYValue 動畫相對於物件的Y座標的開始位置 程式碼: 複製程式碼 程式碼如下: public class MainActivity extends Activity { ImageView image; Button start; Button cancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.main_img); start = (Button) findViewById(R.id.main_start); cancel = (Button) findViewById(R.id.main_cancel); /** 設定縮放動畫 */ final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(2000);//設定動畫持續時間 /** 常用方法 */ //animation.setRepeatCount(int repeatCount);//設定重複次數 //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態 //animation.setStartOffset(long startOffset);//執行前的等待時間 start.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { image.setAnimation(animation); /** 開始動畫 */ animation.startNow(); } }); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { /** 結束動畫 */ animation.cancel(); } }); } }
本節講解ScaleAnimation 動畫, ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 引數說明: 複製程式碼 程式碼如下: float fromX 動畫起始時 X座標上的伸縮尺寸 float toX 動畫結束時 X座標上的伸縮尺寸 float fromY 動畫起始時Y座標上的伸縮尺寸 float toY 動畫結束時Y座標上的伸縮尺寸 int pivotXType 動畫在X軸相對於物件位置型別 float pivotXValue 動畫相對於物件的X座標的開始位置 int pivotYType 動畫在Y軸相對於物件位置型別 float pivotYValue 動畫相對於物件的Y座標的開始位置 程式碼: 複製程式碼 程式碼如下: public class MainActivity extends Activity { ImageView image; Button start; Button cancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.main_img); start = (Button) findViewById(R.id.main_start); cancel = (Button) findViewById(R.id.main_cancel); /** 設定縮放動畫 */ final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(2000);//設定動畫持續時間 /** 常用方法 */ //animation.setRepeatCount(int repeatCount);//設定重複次數 //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態 //animation.setStartOffset(long startOffset);//執行前的等待時間 start.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { image.setAnimation(animation); /** 開始動畫 */ animation.startNow(); } }); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { /** 結束動畫 */ animation.cancel(); } }); } }
複製程式碼 程式碼如下: public class MainActivity extends Activity { ImageView image; Button start; Button cancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.main_img); start = (Button) findViewById(R.id.main_start); cancel = (Button) findViewById(R.id.main_cancel); /** 設定旋轉動畫 */ final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f); animation.setDuration(3000);//設定動畫持續時間 /** 常用方法 */ //animation.setRepeatCount(int repeatCount);//設定重複次數 //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態 //animation.setStartOffset(long startOffset);//執行前的等待時間 start.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { image.setAnimation(animation); /** 開始動畫 */ animation.startNow(); } }); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { /** 結束動畫 */ animation.cancel(); } }); } }本節講解RotateAnimation 動畫, RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 引數說明: float fromDegrees:旋轉的開始角度。 float toDegrees:旋轉的結束角度。 int pivotXType:X軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 float pivotXValue:X座標的伸縮值。 int pivotYType:Y軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 float pivotYValue:Y座標的伸縮值。 程式碼:
本節講解TranslateAnimation動畫,TranslateAnimation比較常用,比如QQ,網易新聞選單條的動畫,就可以用TranslateAnimation實現,
通過TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 來定義動畫
引數說明:
複製程式碼 程式碼如下:
float fromXDelta 動畫開始的點離當前View X座標上的差值
float toXDelta 動畫結束的點離當前View X座標上的差值
float fromYDelta 動畫開始的點離當前View Y座標上的差值
float toYDelta 動畫開始的點離當前View Y座標上的差值
常用方法:
複製程式碼 程式碼如下:
animation.setDuration(long durationMillis);//設定動畫持續時間
animation.setRepeatCount(int i);//設定重複次數
animation.setRepeatMode(Animation.REVERSE);//設定反方向執行
Xml屬性:
複製程式碼 程式碼如下:
android:duration:執行動畫的時間
android:repeatCount:定義動畫重複的時間
程式碼:
複製程式碼 程式碼如下:
public class MainActivity extends Activity {
ImageView image;
Button start;
Button cancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.main_img);
start = (Button) findViewById(R.id.main_start);
cancel = (Button) findViewById(R.id.main_cancel);
/** 設定位移動畫 向右位移150 */
final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0);
animation.setDuration(2000);//設定動畫持續時間
animation.setRepeatCount(2);//設定重複次數
animation.setRepeatMode(Animation.REVERSE);//設定反方向執行
start.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
image.setAnimation(animation);
/** 開始動畫 */
animation.startNow();
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/** 結束動畫 */
animation.cancel();
}
});
}
} 本節講解AlphaAnimation 動畫,視窗的動畫效果,淡入淡出什麼的,有些遊戲的歡迎動畫,logo的淡入淡出效果就使用AlphaAnimation。 直接看程式碼: 複製程式碼 程式碼如下: public class MainActivity extends Activity { ImageView image; Button start; Button cancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.main_img); start = (Button) findViewById(R.id.main_start); cancel = (Button) findViewById(R.id.main_cancel); /** 設定透明度漸變動畫 */ final AlphaAnimation animation = new AlphaAnimation(1, 0); animation.setDuration(2000);//設定動畫持續時間 /** 常用方法 */ //animation.setRepeatCount(int repeatCount);//設定重複次數 //animation.setFillAfter(boolean);//動畫執行完後是否停留在執行完的狀態 //animation.setStartOffset(long startOffset);//執行前的等待時間 start.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { image.setAnimation(animation); /** 開始動畫 */ animation.startNow(); } }); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { /** 結束動畫 */ animation.cancel(); } }); } }