Android 动画之ScaleAnimation应用具体解释

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(); 
} 
}); 
} 
}
 
本节解说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坐标的伸缩值。 
代码: 
复制代码 代码例如以下:
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(); } }); } }
本节解说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(); 
} 
}); 
} 
}
 
 
 
 
 
 
 
 
 
 




原文地址:https://www.cnblogs.com/lcchuguo/p/4081295.html