android学习之动画(一)旋转动画demo

来源:互联网 发布:日版苹果6破解网络锁 编辑:程序博客网 时间:2024/05/22 04:24

android提供了两种动画,tween动画和Frame帧动画,本文介绍一个仿优酷菜单的demo,对旋转动画的实现做一个简要的介绍

一、layout文件如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <RelativeLayout         android:layout_width="100dip"        android:layout_height="50dip"        android:id="@+id/level1"        android:background="@drawable/level1"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true">        <ImageButton             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/icon_home"            android:id="@+id/home"            android:layout_centerInParent="true"/>    </RelativeLayout> <RelativeLayout         android:layout_width="180dip"        android:layout_height="90dip"        android:id="@+id/level2"        android:background="@drawable/level2"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true">      <ImageButton          android:id="@+id/search"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_marginLeft="12dip"android:layout_marginBottom="6dip"          android:background="@drawable/icon_search" />      <ImageButton           android:id="@+id/menu"android:layout_margin="6dip"          android:layout_height="wrap_content"          android:layout_width="wrap_content"          android:background="@drawable/icon_menu"         android:layout_centerHorizontal="true"/>       <ImageButton          android:id="@+id/search"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginRight="12dip"android:layout_marginBottom="6dip"          android:background="@drawable/icon_myyouku" />    </RelativeLayout>      </RelativeLayout>效果图如下:主要实现大圆环的旋转进入和退出 

二、activity代码如下:

package com.youku;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.RelativeLayout;public class MainActivity extends Activity {private ImageButton home;private RelativeLayout level2;private boolean isLevel2Show;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);home = (ImageButton) findViewById(R.id.home);level2 = (RelativeLayout) findViewById(R.id.level2);home.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Log.i("home", "click");if(!isLevel2Show){                                       //动画进入                                        MyAnimation.startAnimationIN(level2, 500);}else{                                       //动画退出                                         MyAnimation.startAnimationOUT(level2, 500,0);}isLevel2Show = !isLevel2Show;}});}}

动画类如下:

 package com.youku;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.RotateAnimation;public class MyAnimation {//入动画public static void startAnimationIN(ViewGroup viewGroup,int duration){for(int i = 0;i < viewGroup.getChildCount();i++){viewGroup.getChildAt(i).setVisibility(View.VISIBLE);viewGroup.getChildAt(i).setClickable(true);viewGroup.getChildAt(i).setFocusable(true);}Animation animation;/** * fromDegrees 初始角度 * toDegrees 开始角度 * pivotXType x轴的参照物 * pivotXValue 参照x轴的参照物的哪个位置 * pivotYType */animation = new RotateAnimation(-180, 0,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);animation.setFillAfter(true);//停留在动画结束的位置animation.setDuration(duration);//持续时间viewGroup.startAnimation(animation);}//出动画public static void startAnimationOUT(final ViewGroup viewGroup,int duration,int offSet){Animation animation;animation = new RotateAnimation(0, -180,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);animation.setFillAfter(true);//停留在动画结束的位置animation.setDuration(duration);//持续时间animation.setStartOffset(offSet);//动画延迟时间animation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {for(int i = 0;i < viewGroup.getChildCount();i++){viewGroup.getChildAt(i).setVisibility(View.VISIBLE);viewGroup.getChildAt(i).setClickable(false);viewGroup.getChildAt(i).setFocusable(false);}viewGroup.setVisibility(View.GONE);}});viewGroup.startAnimation(animation);}}
部分含义:

android:fromDegrees 起始的角度度数

android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针。如10圈则比android:fromDegrees大3600即可

android:pivotX 旋转中心的X坐标

浮点数或是百分比。浮点数表示相对于Object的左边缘,如5; 百分比表示相对于Object的左边缘,如5%; 另一种百分比表示相对于父容器的左边缘,如5%p; 一般设置为50%表示在Object中心

android:pivotY 旋转中心的Y坐标

浮点数或是百分比。浮点数表示相对于Object的上边缘,如5; 百分比表示相对于Object的上边缘,如5%; 另一种百分比表示相对于父容器的上边缘,如5%p; 一般设置为50%表示在Object中心

android:duration 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。

android:interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,

android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行

android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止

android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效

android:detachWallpaper 表示是否在壁纸上运行

android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。

normal保持内容当前的z轴顺序

top运行时在最顶层显示

bottom运行时在最底层显示

 


下面是通过配置文件实现的该效果:

package com.youku;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageButton;import android.widget.RelativeLayout;public class MainActivity extends Activity {private ImageButton home;private RelativeLayout level2;private boolean isLevel2Show;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);home = (ImageButton) findViewById(R.id.home);level2 = (RelativeLayout) findViewById(R.id.level2);home.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Log.i("home", "click");if(!isLevel2Show){  Animation animation=AnimationUtils.loadAnimation(MainActivity.this,  R.anim.in); level2.startAnimation(animation); level2.setVisibility(View.VISIBLE);  }else{Animation animation=AnimationUtils.loadAnimation(MainActivity.this,  R.anim.out); level2.startAnimation(animation); level2.setVisibility(View.GONE);}isLevel2Show = !isLevel2Show;}});}}

in.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate                  android:fromDegrees="-180"                android:toDegrees="0"                 android:pivotX="50%"                 android:pivotY="100%"                 android:duration="500"                  android:fillAfter="true"                 /></set>


out.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate                  android:fromDegrees="0"                android:toDegrees="180"                 android:pivotX="50%"                 android:pivotY="100%"                 android:duration="500"                  android:fillAfter="true"                 /></set>


0 0
原创粉丝点击