ScaleAnimation类是Android系统中的尺寸变化动画类,用于控制View对象的尺寸变化,该类继承于Animation类。 ScaleAnimation类中的不少方法都与Animation类一致,该类中最经常使用的方法即是ScaleAnimation构造方法。ide
【基本语法】public ScaleAnimation (float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)动画
参数说明code
fromX:起始X坐标上的伸缩尺寸。对象
toX:结束X坐标上的伸缩尺寸。继承
fromY:起始Y坐标上的伸缩尺寸。图片
toY:结束Y坐标上的伸缩尺寸。get
pivotXType:X轴的伸缩模式,能够取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。it
pivotXValue:X坐标的伸缩值。io
pivotYType:Y轴的伸缩模式,能够取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。class
pivotYValue:Y坐标的伸缩值。
【实例演示】下面经过代码来演示如何设置一个简单的尺寸变化动画效果。
public class firstActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { //重载onCreate方法 super.onCreate(savedInstanceState); setContentView(R.layout.main); final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象 Button btn1=(Button)findViewById(R.id.button1); //按钮对象 Button btn2=(Button)findViewById(R.id.button2); final Animation scaleAnimation= new ScaleAnimation(0f,1f,0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); //设置尺寸变化动画对象 btn1.setOnClickListener(new View.OnClickListener() { //设置监听器 @Override public void onClick(View v) { // TODO Auto-generated method stub scaleAnimation.setDuration(2000); //动画持续时间 image.setAnimation(scaleAnimation); //设置动画 scaleAnimation.startNow(); //启动动画 } }); btn2.setOnClickListener(new View.OnClickListener() { //设置监听器 @Override public void onClick(View v) { // TODO Auto-generated method stub scaleAnimation.cancel(); //取消动画执行 } }); } }
在这段代码中,首先经过ScaleAnimation构造方法建立了一个尺寸变化的动画对象。而后,在第一个按钮监听器中设置了动画的持续 时间,以后启动该动画。在第二个按钮监听器中取消该动画。读者运行这段代码,将看到图片从小到大逐渐变化,如图9.9所示。最后,图片增大到原始尺寸的时 候中止,如图9.10所示。