Android动画RotateAnimation(fromDegrees, toDegrees, pivotX,pivotY)参数

Android动画RotateAnimation(fromDegrees, toDegrees, pivotX,pivotY)参数

博客撰写人:It一zhai男 
转载请标明地址:http://blog.csdn.net/u013293125/article/details/52637189

本文内容知识点包括: 
【1】ImageView的getHeight()和getWidth()为0的解决方法及原因 
【2】RotateAnimation(fromDegrees, toDegrees, pivotX,pivotY)参数解析

先上截图: 

一、ImageView的getHeight()和getWidth()为0的解决方法及原因 
原因:因为onCreate方法执行完了,我们定义的控件才会被度量(measure),所以我们在onCreate方法里面通过view.getHeight()获取控件的高度或者宽度肯定是0,因为它自己还没有被度量,也就是说他自己都不知道自己有多高,而你这时候去获取它的尺寸,肯定是不行的. 
网上有大神写的很详细: 
http://www.cnblogs.com/kissazi2/p/4133927.html 
http://blog.csdn.net/xiayu98020214/article/details/46714015

解决方法:将一个runnable添加到Layout队列中:View.post()。简单地说,只要用View.post()一个runnable就可以了。runnable对象中的方法会在View的measure、layout等事件后触发,具体的参考Romain Guy:

UI事件队列会按顺序处理事件。在setContentView()被调用后,事件队列中会包含一个要求重新layout的message,所以任何你post到队列中的东西都会在Layout发生变化后执行。

1 final View view=//smth;
2 ...
3 view.post(new Runnable() {
4             @Override
5             public void run() {
6                 view.getHeight(); //height is ready
7             }
8         });

二、RotateAnimation(fromDegrees, toDegrees, pivotX,pivotY)参数解析 
fromDegrees:旋转的起始角度 
toDegrees:旋转的终止角度 
pivotX:该对象被旋转的点的X坐标,指定为一个绝对数字,其中0个是左边缘。 
pivotY:对象被旋转的点的Y坐标,指定为一个绝对数字,其中0个是顶部边缘。

pivotX和pivotY怎么理解呢?其实就是你所要旋转的View的左顶点为坐标原点,pivotX=0时是View的左边缘,pivotY=0时是View的顶部边缘。点(pivotX=0,pivotY=0)就是View的左顶点。

三、activity_imagerotate.xml文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6      <ImageView
 7          android:id="@+id/iv"
 8             android:layout_gravity="center_vertical|center_horizontal"
 9             android:layout_width="7dp"
10             android:layout_height="200dp"
11             android:background="@drawable/diaobi_green"/>
12 
13 </LinearLayout>

四、ImageRotate.java文件

 1 package com.example.progresstest;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.text.style.LineHeightSpan.WithDensity;
 6 import android.util.Log;
 7 import android.view.animation.Animation;
 8 import android.view.animation.RotateAnimation;
 9 import android.widget.ImageView;
10 
11 public class ImageRotate extends Activity{
12     private ImageView iv;
13     private int width,height;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         // TODO Auto-generated method stub
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_imagerotate);
19 
20         iv = (ImageView) findViewById(R.id.iv);
21         //这里用post是为了得到iv的高度和宽度,如果不用post,
22         //得到的高度和宽度的值为0,因为在iv被绘制之前就调用了getHeight和getWidth;
23         iv.post(new Runnable() {
24 
25             @Override
26             public void run() {
27                 // TODO Auto-generated method stub
28                 /**
29                  *RotateAnimation(fromDegrees, toDegrees, pivotX,pivotY)
30                  *pivotX:该对象被旋转的点的X坐标,指定为一个绝对数字,其中0个是左边缘。
31                  *pivotY:对象被旋转的点的Y坐标,指定为一个绝对数字,其中0个是顶部边缘。
32                  */
33                 height = iv.getHeight();
34                 width = iv.getWidth()/2;
35                 RotateAnimation animation = new RotateAnimation(0, 90, width,height);
36                 animation.setDuration(10000);//设定转一圈的时间
37 //              animation.setRepeatCount(Animation.INFINITE);//设定无限循环
38 //              animation.setRepeatMode(Animation.RESTART);//设定重复模式
39                 iv.startAnimation(animation);
40                 //ture表示动画结束后停留在动画的最后位置,
41                 //false表示动画结束后回到初始位置,默认为false。
42                 animation.setFillAfter(true);
43             }
44         });
45     }
46 
47 }
原文地址:https://www.cnblogs.com/ityizhainan/p/ityizhainan.html