RotateAnimation类:旋转变化动画类

来源:互联网 发布:python中的sleep函数 编辑:程序博客网 时间:2024/05/22 05:23
 

RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点。x轴为0度,顺时针方向旋转一定的角度。


1.RotateAnimation(fromDegrees, toDegrees) [默认以View左上角顶点为旋转点]。


X轴顺时针转动到fromDegrees为旋转的起始点,
X轴顺时针转动到toDegrees为旋转的起始点。
如fromDegrees=0,toDegrees=90;为左上角顶点为旋转点。0度为起始点,90度为终点。进行旋转,旋转了90度
如fromDegrees=60,toDegrees=90;为左上角顶点为旋转点。60度为起始点,90度为终点。进行旋转,旋转了90-60=30度

2.RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)


(pivotX,pivotY)为旋转点。pivotX为距离左侧的偏移量,pivotY为距离顶部的偏移量。即为相对于View左上角(0,0)的坐标点。
如View width=100px,height=100px
RotateAnimation(0,10,100,100);则以右下角顶点为旋转点,从原始位置顺时针旋转10度
RotateAnimation(0,90,50,50);则以View的中心点为旋转点,旋转90度

3.RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)


pivotXType, pivotXValue, pivotYType, pivotYValue  旋转点类型及其值。
Animation.ABSOLUTE为绝对值 其他为百分比。这个和平移动画的一样,不了解可以去那看
如RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 按中心点旋转90度
效果和2例中的RotateAnimation(0,90,50,50);则以View的中心点为旋转点,旋转90度 。效果一样


new RotateAnimation(0, 180, centerX,centerY);

第一个参数表示动画的起始角度,第二个参数表示动画的结束角度,第三个表示动画的旋转中心x轴,第四个表示动画旋转中心y轴。

rotateAnimation.setDuration(1000 * 20);

表动画持续20s。

rotateAnimation.setFillAfter(true);

ture表示动画结束后停留在动画的最后位置,false表示动画结束后回到初始位置,默认为false。

mView.startAnimation(rotateAnimation);

表示在mView中启动动画。 

 

RotateAnimation类是Android系统中的旋转变化动画类,用于控制View对象的旋转动作,该类继承于Animation类。RotateAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是RotateAnimation构造方法。

【基本语法】public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明

fromDegrees:旋转的开始角度。

toDegrees:旋转的结束角度。

pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:X坐标的伸缩值。

pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:Y坐标的伸缩值。

【实例演示】下面通过代码来演示如何设置一个简单的旋转变化动画效果。

  1. public class firstActivity extends Activity {  
  2. /** Called when the activity is first created. */  
  3. @Override  
  4. public void onCreate(Bundle savedInstanceState) {           //重载onCreate方法  
  5.     super.onCreate(savedInstanceState);  
  6.     setContentView(R.layout.main);  
  7.  
  8.     final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象  
  9.     Button btn1=(Button)findViewById(R.id.button1);         //按钮对象  
  10.     Button btn2=(Button)findViewById(R.id.button2);  
  11.     final Animation rotateAnimation = new       
  12.      RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  13.                                                         //设置旋转变化动画对象  
  14.     btn1.setOnClickListener(new View.OnClickListener() {        //设置监听器  
  15.           
  16.         @Override  
  17.         public void onClick(View v) {  
  18.             // TODO Auto-generated method stub  
  19.             rotateAnimation.setDuration(3000);              //持续时间  
  20.             image.setAnimation(rotateAnimation);            //设置动画  
  21.            imagev.startAnimation(rotateAnimation);//启动动画  
  22.         }  
  23.     });  
  24.     btn2.setOnClickListener(new View.OnClickListener() {        //设置监听器  
  25.           
  26.         @Override  
  27.         public void onClick(View v) {  
  28.             // TODO Auto-generated method stub  
  29.             rotateAnimation.cancel();                       //取消动画执行  
  30.         }  
  31.     });  
  32. }  
  33. }  
在这段代码中,首先通过RotateAnimation构造方法创建了一个旋转变化的动画对象。然后,在第一个按钮监听器中设置了动画的持续时间,之后启动该动画。在第二个按钮监听器中取消该动画
0 0