Animation动画效果(代码实现)

来源:互联网 发布:阿里云服务器能转让吗 编辑:程序博客网 时间:2024/05/17 02:36

1. AlphaAnimation 渐变
  
   AlphaAnimation alph=new AlphaAnimation(0.1f, 1.0f); 

两个参数为动画从开始到结束的变化范围,0为完全透明,1为完全不透明。

 

2. RotateAnimation 旋转

  RotateAnimation  rotate=new RotateAnimation(0, 359, Animation.RELATIVE_TO_PARENT, 0.5f,  Animation.RELATIVE_TO_PARENT,0.5f);

第一二个参数为旋转从开始到结束的角度变化,如果第一个参数大于第二个为逆时针旋转。第三个参数与第五个参数为动画变化时X轴的开始位置的相对类型有RELATIVE_TO_PARENT(2)和ABSOLUTE(0)及RELATIVE_TO_SELF(1)三种。一般不用ABSOLUTE,这个是相对于屏幕分辨率来决定位置。RELATIVE_TO_PARENT为相对于父View决定0坐标位置,RELATIVE_TO_SELF为相对于View自身决定0坐标位置。第四个参数与第六个参数为动画开始时圆心的位置,是相对值。比如第3、5参数设为RELATIVE_TO_SELF,第4、6都设为0.5f,则启用这个动画的View将会以自己为中心旋转。

3.ScaleAnimation 缩放
   缩放时能移位,结束后回原先位置。

/**八参数,前四个分别表示X、Y轴缩放比例从多少到多少,1表示View所占的空间为原大小,可以大于1。     * 后四个参数表示变化时变化对象的开始位置X、Y,结束位置为view所在的位置。    */    Animation scale=new ScaleAnimation(0, 1, 0, 1,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.5f);
//六参数为ABSOLUTE方式,第五个和第七个参数去掉了,后面两个参数表示从屏幕的哪个位置开始,单位为分辨率不再是比例。
//四参数的后面4个参数全是默认,默认第五和第七参数为ABSOLUTE,第六和第八参数为0.1f


4. TranslateAnimation 移位
   

    //四参数构造,为ABSOLUTE方式,参数1、2参数为X轴的开始和结束位置,3、4为Y轴的    //View当前位置为X=0,Y=0,负数为当前位置的左边开始,单位为分辨率。    Animation translate=new TranslateAnimation(-100, 120, 0, 220);
    //八参数构造,两个为一对,第一对表示X轴起点,第二对表示X轴终点,    //第三队表示Y轴起点,第四对表示Y轴终点,每对第一个参数为决定第二个参数的相对类型    //Animation.RELATIVE_TO_SELF==1,Animation.RELATIVE_TO_PARENT==2    Animation translate=new TranslateAnimation(1, -0.2f, 1, 1,2,-0.2f,2,1);

------------------

  这个动画可以用于赋予控件颤动效果:

Animation shake = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.01f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);shake.setDuration(1000);shake.setInterpolator(new CycleInterpolator(10));view.startAnimation(shake);

XML文件设置为
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromXDelta="0"    android:interpolator="@anim/cycle"    android:toXDelta="15" />

<?xml version="1.0" encoding="utf-8"?><cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"    android:cycles="5" />


要看到动画效果,需要给动画设定播放时长,接着调用启动动画

    translate.setDuration(5000);    image.startAnimation(translate);
另一种启动方式
image.setAnimation(alph);       //image.startAnimation(image.getAnimation());alph.start();

注意:RELATIVE_TO_PARENT方式的原点并不是屏幕左上方,而是调用动画的View的内部左上方。

动画实现的类来源于包android.view.animation。这个包里的类大部分都继承或者实现android.view.animation与实现接口Interpolator,继承android.view.animation的类都是动画实现类,实现Interpolator接口的类都是控制动画速度的类。   Animation类还提供一监听方法setAnimationListener(Animation.AnimationListener listener)能监听动画的开始、结束与重复执行。

附一个XML文件,明白用代码设置动画后用文件设置动画就很容易了。

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="true"    android:interpolator="@android:anim/accelerate_interpolator">     <alpha android:fromAlpha="1.0"        android:toAlpha="0.0"        android:duration="4000"        android:startOffset="500"/>    <rotate android:fromDegrees="0"        android:toDegrees="359"        android:duration="4000"        android:pivotX="50%"        android:pivotY="50%"/>    <scale android:fromXScale="0"        android:toXScale="1"        android:duration="4000"        android:fromYScale="0"        android:toYScale="1"        android:pivotX="50%p"        android:pivotY="50%p"/></set>

附一个LayoutAnimationController类的XML文件
  

<?xml version="1.0" encoding="utf-8"?><layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"    android:delay="0.5"    android:animation="@anim/animationset"    android:animationOrder="random"    ></layoutAnimation>
layoutAnimation标签eclipse中默认没带有,所以敲代码时得不到提示。

LayoutAnimationController类主要为ViewGroup服务,主要用来控制ViewGroup中的View的动画,让View按一定顺序播放动画,比如ListView的Item一条条显示出来,通过setDelay(float delay)就能控制每显示一View后下一个View的显示延迟时间单位是秒。
ViewGroup的子类通过android:layoutAnimation="@anim/layout_animation"引用就能实现效果了。用代码设置的方式思路是:先得到一个Animation对象,
接着通过构造方法得到LayoutAnimationController(Animation animation),接下来各种set,最后用ViewGroup子类调用
setLayoutAnimation(controller)就行了。


帧动画简单实用方法:
  帧动画一般用来给一个ImageView不停换图片显示,一般用的比较少,所以没深究。
anim_list.xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:drawable="@drawable/down" android:duration="500" android:visible="true"/><item android:drawable="@drawable/down1" android:duration="500" android:visible="true"/><item android:drawable="@drawable/ic_launcher" android:duration="500" android:visible="true"/></animation-list>
image.setBackgroundResource(R.anim.anim_list);
接下来启动动画(测试时把启动部分写在按钮的监听)
AnimationDrawable an=(AnimationDrawable) image.getBackground();an.start();//不能在Oncreate里启动,因为还没绑定好




	
				
		
原创粉丝点击