Android学习—补间动画(旋转动画)

用XML文件来设置旋转动画属性

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android">
 3 
 4     <!--
5 pivotX:动画旋围绕点的X轴位置 pivotY:动画旋转围绕点的Y轴位置 50%是中心
6 fromDegress:动画开始的初始角度 7 toDegrees:动画结束的角度 8 --> 9 <rotate 10 android:fromDegrees="180" 11 android:toDegrees="360" 12 android:duration="2000" 13 android:repeatCount="2" 14 /> 15 </set>
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     tools:context="com.test2_23.test2_23.xml_RotateActivity">
 5     <ImageView
 6         android:id="@+id/rotate_xml_img"
 7         android:src="@drawable/img_bird"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content" />
10 </RelativeLayout>
 1 public class xml_RotateActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_xml__rotate);
 7         ImageView img = (ImageView) findViewById(R.id.rotate_xml_img);
 8         Animation animation = AnimationUtils.loadAnimation(this,R.anim.animtation_rotate);
 9         img.clearAnimation();
10         img.startAnimation(animation);
11     }
12 }

 使用java代码实现旋转动画

 1 public class Java_RotateActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_java__rotate);
 7         ImageView img = (ImageView) findViewById(R.id.rotate_java_img);
 8         //使用RotateAnimation来创建一个旋转动画
 9         //构造方法的参数
10         //fromDegrees:旋转动画开始的度数
11         //toDegrees:旋转动画结束的度数
12         //在使用pivotX和pivotY的时候都需要设置一下这个中心点是根据谁来决定的
13         //Animation.RELATIVE_SELF是相对于自身的,
14         //pivotX:旋转围绕的中心点在X轴的相对位置
15         //pivotY:玄幻围绕的中心点在Y轴的相对位置
16         Animation animation = new RotateAnimation(0.0f,360.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
17         animation.setDuration(2000);
18         animation.setRepeatCount(2);
19         img.setAnimation(animation);
20     }
21 }

 

posted on 2016-02-24 18:44  Become_stronger  阅读(533)  评论(0)    收藏  举报

导航