首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Animation成效控制(一)

2012-07-23 
Animation效果控制(一)该类提供了旋转、移动、伸展和淡出等等效果1、Alpha——淡入淡出2、Scale——缩放3、Rotate——

Animation效果控制(一)

该类提供了旋转、移动、伸展和淡出等等效果
1、Alpha——淡入淡出
2、Scale——缩放
3、Rotate——旋转
4、Translate——移动

?

创建Tweened Animations的步骤
1、创建一个AnimationSet对象
2、根据需要创建相应的Animation对象
3、根据软件动画的需求,为Animation对象设置相应的数据
4、将Animation对象添加到AnimationSet对象当中
5、使用控件对象执行AnimationSet

?

private class AlphaButtonListener implements OnClickListener {@Overridepublic void onClick(View view) {//创建一个AnimationSet对象AnimationSet animationSet = new AnimationSet(true);//创建一个AlphaAnimation对象,1表示完全不透明,0表示透明,从不透明到完全透明AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//设置动画执行的时间(单位:毫秒)alphaAnimation.setDuration(1000);//将AlphaAnimation对象添加到AnimationSet当中animationSet.addAnimation(alphaAnimation);//使用ImageView的startAnimation方法开始执行动画imageView.startAnimation(animationSet);}}

?

private class RotateButtonListener implements OnClickListener {@Overridepublic void onClick(View view) {AnimationSet animationSet = new AnimationSet(true);/*Animation.RELATIVE_TO_PARENT,相对于父控件,1f表示整个父控件的宽度或者是高度,0.5f表示父控件的高度或者宽度的一半,Animation.RELATIVE_TO_SELF,相对于自身控件,前面两个参数是旋转的角度,后面四个参数用来定义旋转的圆心*/RotateAnimation rotateAnimation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_PARENT, 1f,Animation.RELATIVE_TO_PARENT, 0f);rotateAnimation.setDuration(5000);animationSet.addAnimation(rotateAnimation);imageView.startAnimation(animationSet);}}

?

private class ScaleButtonListener implements OnClickListener {@Overridepublic void onClick(View view) {//表示动画效果Interpolator共享AnimationSet animationSet = new AnimationSet(true);//前四个参数表示从原来大小的100%缩小到10%,后四个参数是为确定“中心点”ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,0.1f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);animationSet.addAnimation(scaleAnimation);//动画效果推迟1秒钟后启动animationSet.setStartOffset(1000);//如果值为true,控件则保持动画结束的状态animationSet.setFillAfter(true);//如果值为false,控件则保持动画结束的状态animationSet.setFillBefore(false);//动画效果重复3次//animationSet.setRepeatCount(3);animationSet.setDuration(2000);imageView.startAnimation(animationSet);}}

?

private class TranslateButtonListener implements OnClickListener {@Overridepublic void onClick(View view) {AnimationSet animationSet = new AnimationSet(true);/*前四个参数分别设置X轴的开始位置,设置X轴的结束位置后四个参数分别设置Y轴的开始位置,设置Y轴的结束位置*/TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 1.0f);translateAnimation.setDuration(1000);animationSet.addAnimation(translateAnimation);imageView.startAnimation(animationSet);}}

?

?利用配置文件xml设置animation(可以复用,类似于CSS样式设定)
1、在res文件夹下面建立一个名为anim的文件夹(控制动画的文件夹)
2、创建xml文件,首先加入set标签
3、在该标签中加入rotate、alpha、scale、translate
4、在代码中使用AnimationUtil当中装载xml文件,并生成Animation对象

?

android:toDegrees="+350" //角度为正角
android:pivotX="50"? //使用绝对位置定位
android:pivotX="50%" //使用相对于控件本身定位
android:pivotX="50%p" //相对于控件的父控件定位

多个效果可以加载到同一个xml文件中,这样可以同时处理多个效果

?

建立文件alpha.xml

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

?

private class AlphaButtonListener implements OnClickListener {@Overridepublic void onClick(View view) {//使用AnimationUtils装载动画设置文件//Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);//imageView.startAnimation(animation);//下面的代码和上面的效果是一致的AnimationSet animationSet = new AnimationSet(false);AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);alpha.setInterpolator(new DecelerateInterpolator());RotateAnimation rotate = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);rotate.setInterpolator(new AccelerateInterpolator());animationSet.addAnimation(alpha);animationSet.addAnimation(rotate);animationSet.setDuration(2000);animationSet.setStartOffset(500);imageView.startAnimation(animationSet);}}

?

?Interpolator类定义了动画变化的速率


android:interpolator=""??//设置动画变化速率
android:shareInterpolator="true" //共享动动画变化速率

?

Frame-by-Frame Animations——帧


Drawable序列,这些Drawable可以按照指定的时间间歇一个一个显示

在res/drawable-ldpi目录下创建anim_nv.xml文件

?

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:drawable="@drawable/nv1" android:duration="500" /><item android:drawable="@drawable/nv2" android:duration="500" /><item android:drawable="@drawable/nv3" android:duration="500" /><item android:drawable="@drawable/nv4" android:duration="500" /></animation-list>

?

?

private class ButtonListener implements OnClickListener{@Overridepublic void onClick(View v) {imageView.setBackgroundResource(R.drawable.anim_nv);AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();animationDrawable.start();}}

?

?

1、LayoutAnimationController用于一个layout里面或是一个ViewGroup里面的控件设置动画效果
2、每个控件都有相同的动画效果
3、这些控件的动画效果在不同的时间显示出来
4、LayoutAnimationController可以在xml文件中设置,也可以在代码中进行设置

热点排行