安卓中为View添加动画效果(尺寸缩放、透明度渐变、旋转、移动)
2015-10-29 16:15
597 查看
import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationSet; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; /** * AnimationEffect 安卓中为View添加动画效果 * ----- * 2015-10-23 上午11:09:04 * wangzhongyuan */ public class AnimationEffect { /** * 为View添加透明度变换效果 */ public static void AddAlphaAni(View view) { AddAlphaAni(view, 0, 1, 1000, Animation.REVERSE, Animation.INFINITE); } /** * 为View添加透明度变换效果,透明度从fromAlpha变化到toAlpha,变化持续时间durationMillis,重复模式repeatMode */ public static void AddAlphaAni(View view, float fromAlpha, float toAlpha, long durationMillis, int repeatMode, int repeatCount) { AlphaAnimation alphaAni = new AlphaAnimation(fromAlpha, toAlpha); alphaAni.setDuration(durationMillis); // 设置动画效果时间 alphaAni.setRepeatMode(repeatMode); // 重新播放 alphaAni.setRepeatCount(repeatCount); // 循环播放 view.startAnimation(alphaAni); } /** * 设置位移动画效果,来回移动 */ public static void setTransAni(View view, int x0, int x1, int y0, int y1, long durationMillis) { TranslateAnimation transAni = new TranslateAnimation(x0, x1, y0, y1); transAni.setDuration(durationMillis); transAni.setRepeatMode(Animation.REVERSE); transAni.setRepeatCount(Animation.INFINITE); view.startAnimation(transAni); } /** * 设置旋转动画 */ public static void setRotateAni(View view, float fromDegrees, float toDegrees, long time) { setRotateAni(view, fromDegrees, toDegrees, time, Animation.REVERSE, Animation.INFINITE, true); } /** * 设置旋转动画, 从角度fromDegrees旋转到toDegrees,旋转速度durationMillis毫秒,重启模式repeatModes,重启次数repeatCount,是否匀速旋转linear。 * 示例:AnimationEffect.setRotateAni(view, 0, 360, 1000, Animation.RESTART, Animation.INFINITE, true); // 控制view每秒旋转360度 */ public static void setRotateAni(View view, float fromDegrees, float toDegrees, long durationMillis, int repeatModes, int repeatCount, boolean linear) { RotateAnimation rotateAni = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAni.setDuration(durationMillis); rotateAni.setRepeatMode(repeatModes); rotateAni.setRepeatCount(repeatCount); if(linear) rotateAni.setInterpolator(new LinearInterpolator()); // 匀速旋转 view.startAnimation(rotateAni); } /** * 为控件添加尺寸渐变动画 */ public static void setScaleAni(View V, float fromScale, float toScale, long ANITIME) { AnimationSet aniSet = new AnimationSet(true); // final int ANITIME = 500; // 尺寸变化动画,设置尺寸变化 ScaleAnimation scaleAni = new ScaleAnimation(fromScale, toScale, fromScale, toScale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAni.setDuration(ANITIME); // 设置动画效果时间 aniSet.addAnimation(scaleAni); // 将动画效果添加到动画集中 V.startAnimation(aniSet); // 添加光效动画到控件 } /** * 为控件添加扩散光效 */ public static void setLightExpendAni(View V) { AnimationSet aniSet = new AnimationSet(true); final int ANITIME = 1200; // 尺寸变化动画,设置尺寸变化 ScaleAnimation scaleAni = new ScaleAnimation(0.98f, 1.1f, 0.98f, 1.24f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAni.setDuration(ANITIME); // 设置动画效果时间 scaleAni.setRepeatMode(Animation.RESTART); // 重新播放 scaleAni.setRepeatCount(Animation.INFINITE);// 循环播放 aniSet.addAnimation(scaleAni); // 将动画效果添加到动画集中 // 透明度变化 AlphaAnimation alphaAni = new AlphaAnimation(1f, 0.05f); alphaAni.setDuration(ANITIME); // 设置动画效果时间 alphaAni.setRepeatMode(Animation.RESTART); // 重新播放 alphaAni.setRepeatCount(Animation.INFINITE);// 循环播放 aniSet.addAnimation(alphaAni); // 将动画效果添加到动画集中 V.startAnimation(aniSet); // 添加光效动画到控件 } /** * 设置背景旋转光效动画 */ public static void setLightAni(final View view) { AnimationListener listenser = new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { view.clearAnimation(); setRotateAni(view, 36, 3996, 110000, Animation.RESTART, Animation.INFINITE, true); } @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationStart(Animation animation) {} }; AnimationSet set = new AnimationSet(true); // 渐现 AlphaAnimation alphaAni = new AlphaAnimation(0.0f, 1f); alphaAni.setDuration(1000); // 设置动画效果时间 set.addAnimation(alphaAni); // 旋转 RotateAnimation rotateAni = new RotateAnimation(0, 36, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAni.setDuration(1000); set.addAnimation(rotateAni); // 尺寸由小变大 ScaleAnimation scaleAni = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAni.setDuration(1000); scaleAni.setAnimationListener(listenser); set.addAnimation(scaleAni); view.startAnimation(set); } }
相关文章推荐
- 不错的主要用于加密的vbs(asp)位移运算类
- C#实现字体旋转的方法
- 基于jQuery实现的旋转彩圈实例
- C++实现一维向量旋转算法
- 使用JavaScript实现旋转的彩圈特效
- jQuery平滑旋转幻灯片特效代码分享
- jQuery中实现动画效果的基本操作介绍
- C#位移的介绍与例子
- C#中图片旋转和翻转(RotateFlipType)用法分析
- 图片的左右移动,js动画效果实现代码
- 实现动画效果核心方式的js代码
- Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Javascript实现可旋转的圆圈实例代码
- Android UI之ImageView实现图片旋转和缩放
- Android实现屏幕旋转方法总结
- javascript 通用loading动画效果实例代码
- JS实现3D图片旋转展示效果代码
- IOS等待时动画效果的实现
- jQuery Easing 动画效果扩展