android 动画 ——视图动画(View Animation)
来源:互联网 发布:连环替代法不拿出算法 编辑:程序博客网 时间:2024/05/29 09:08
android动画分为视图动画(View Animation)、属性动画(Property Animation)
想看属性动画(Property Animation):请移步至http://blog.csdn.net/u013424496/article/details/51700312
这里我们来说下视图动画(View Animation)的纯代码写法,还有一种是xml调用,
对于xml调用可以去看 http://blog.csdn.net/u013424496/article/details/51144171
相对与属性动画视图动画使用环境:
view animation system在View动画的展现方面也是有约束的,只暴露了View的很少方面。比如View支持缩放和旋转,但不支持背景颜色的动画。
view animation system的另一劣势是,其改变的是View的绘制效果,真正的View的属性保持不变,比如无论你在对话中如何缩放Button的大小,Button的有效点击区域还是没有应用到动画时的区域,其位置与大小都不变。
但是View animation system只需花费很少时间创建而且只需很少的代码。如果View 动画完成了你所有的动作,或者你存在的代码已经达到了你想要的效果,就没必要使用property 动画系统了。
图解类结构
animation.setFillAfter(true);//让动画结束的是时候保持现状,不会回到动画开始的显示状态
缩放ScaleAnimation
//以View左上角作为缩放中心,水平方向扩大一倍,垂直方向缩小为原来的一半float fromXScale = 1.0f;float toScaleX = 2.0f;float fromYScale = 1.0f;float toScaleY = 0.5f;Animation animation = new ScaleAnimation(fromXScale, toScaleX, fromYScale, toScaleY);//设置动画持续时间animation.setDuration(3000);//通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(animation);
//以View中心点作为缩放中心,水平方向和垂直方向都缩小为原来的一半float fromXScale = 1.0f;float toScaleX = 0.5f;float fromYScale = 1.0f;float toScaleY = 0.5f;float pivotX = textView.getWidth() / 2;float pivotY = textView.getHeight() / 2;Animation animation = new ScaleAnimation( fromXScale, toScaleX, fromYScale, toScaleY, pivotX, pivotY); //设置动画持续时间 animation.setDuration(3000); //通过View的startAnimation方法将动画立即应用到View上 textView.startAnimation(animation);
//以View中心点作为缩放中心,水平方向和垂直方向都缩小为原来的一半float fromXScale = 1.0f;float toScaleX = 0.5f;float fromYScale = 1.0f;float toScaleY = 0.5f;int pivotXType = Animation.RELATIVE_TO_SELF;float pivotXValue = 0.5f;int pivotYType = Animation.RELATIVE_TO_SELF;float pivotYValue = 0.5f;Animation animation = new ScaleAnimation( fromXScale, toScaleX, fromYScale, toScaleY, pivotXType, pivotXValue, pivotYType, pivotYValue);//设置动画持续时间animation.setDuration(3000);//通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(animation);
平移TranslateAnimation
int fromXDelta = 0;int toXDelta = getResources().getDisplayMetrics().widthPixels / 2;int fromYDelta = 0;int toYDelta = 0;//让动画在水平位置上沿X轴平移toXDelta个像素Animation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);//设置动画持续时间为5000毫秒animation.setDuration(5000);textView.startAnimation(animation);效果图:
fromXDelta 表示动画开始时View相对于原来位置X轴方向的偏移坐标
toXDelta 表示动画结束时View相对于原来位置X轴方向的偏移坐标
fromYDelta 表示动画开始时View相对于原来位置Y轴方向的偏移坐标
toYDelta 表示动画结束时View相对于原来位置Y轴方向的偏移坐标
//设置fromXint fromXType = Animation.ABSOLUTE;float fromXValue = textView.getX();//设置toXint toXType = Animation.RELATIVE_TO_PARENT;float toXValue = 0.5f;//设置fromYint fromYType = Animation.ABSOLUTE;float fromYValue = textView.getY();//设置toYint toYType = Animation.RELATIVE_TO_SELF;float toYValue = 3.0f;//创建动画Animation animation = new TranslateAnimation( fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);//设置动画持续时间为3000毫秒animation.setDuration(3000);//通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(animation);
效果图:
fromXType和fromXValue进行说明,fromXType的取值类型决定了如何设置fromXValue的值。fromXType的取值有三种,分别是:ABSOLUTE、RELATIVE_TO_PARENT和RELATIVE_TO_SELF。
ABSOLUTE
当fromXType取值为ABSOLUTE时,表示fromXValue的值是在该View的父控件的坐标系的绝对值,比如fromXValue为200,表示动画开始时,View的左侧到其父控件左侧的距离是200个像素。RELATIVE_TO_PARENT
当fromXType取值为RELATIVE_TO_PARENT时,表示fromXValue的值是相对于其父控件尺寸的百分比。比如fromXValue为0,表示动画开始时,View的左侧紧靠父控件的左侧;fromXValue为0.5时,表示动画开始时,View的左侧位置在父控件水平方向中间的位置;fromXValue为1时,表示动画开始时,View的左侧位置与父控件的右侧位置完全重合。RELATIVE_TO_SELF
当fromXType取值为RELATIVE_TO_SELF时,表示fromXValue的值是相对于其自身尺寸的百分比。比如fromXValue为0,表示动画开始时,View的X坐标和初始位置的X坐标相同;fromXValue为0.5时,表示动画开始时,View的左侧位置在初始View状态下水平方向中间的位置,即向右偏移了View宽度的一半;fromXValue为1时,表示动画开始时,View的左侧位置正好与初始View状态下的右侧位置重合,即向右偏移了正好View的宽度大小的距离。
渐变AlphaAnimation
//1.0表示完全不透明,0.0表示完全透明float fromAlpha = 0.0f;float toAlpha = 1.0f;//1.0 => 0.0表示View从完全不透明渐变到完全透明Animation animation = new AlphaAnimation(fromAlpha, toAlpha);//设置动画持续时间为3000毫秒animation.setDuration(5000);//通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(animation);
旋转RotateAnimation
//以View左上角为旋转轴,创建旋转60度的动画Animation animation = new RotateAnimation(0, 60);//设置动画持续时间animation.setDuration(3000);//通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(animation);效果图:
解决在onCreate()过程中获取View的width和Height为0的4种方法
)<pre name="code" class="html">//以View中心点作为旋转轴,创建旋转90度的动画textView.post(new Runnable() {@Overridepublic void run() {float pivotX = textView.getWidth() / 2;float pivotY = textView.getHeight() / 2;Animation animation = new RotateAnimation(0, 90, pivotX, pivotY);//设置动画持续时间animation.setDuration(3000);//通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(animation);}});
效果图:
//以View的父控件中心点作为旋转轴,创建旋转360度的动画int pivotXType = Animation.RELATIVE_TO_PARENT;float pivotXValue = 0.5f;int pivotYType = Animation.RELATIVE_TO_PARENT;float pivotYValue = 0.5f;Animation animation = new RotateAnimation( 0, 360, pivotXType, pivotXValue, pivotYType, pivotYValue);//设置动画持续时间animation.setDuration(3000);//通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(animation);
AnimationSet//初始化 Translate动画 TranslateAnimationtranslateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f); //初始化 Alpha动画 AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); //动画集 AnimationSet set = new AnimationSet(true); set.addAnimation(translateAnimation); set.addAnimation(alphaAnimation); //设置动画时间 (作用到每个动画) set.setDuration(3000); //通过View的startAnimation方法将动画立即应用到View上textView.startAnimation(set);
- android 动画 ——视图动画(View Animation)
- 动画——视图动画(View Animation)
- Android视图动画---View Animation
- Android视图动画(View Animation)
- Android-View Animation(视图动画)
- Android动画之View animation(视图动画)
- View Animation——视图动画
- Android 动画之 ---- View Animation (视图动画,补间动画)
- Android——Animation笔记:视图动画
- Android动画之View Animation(视图动画)
- 视图动画(View Animation)
- 视图动画View Animation入门
- View Animation(视图动画)
- android动画 -- view Animation
- Android:View Animation 动画
- Android View 视图动画
- 动画——View Animation,Drawable Animation,Property Animation,LayoutTransition
- android animation——view进入退出动画
- 经历3轮百度面试的问题(iOS)
- 补充slidingmenu之左右侧滑
- 何为内省???
- 日常问题记录,长期更新。
- googleplay经验总结
- android 动画 ——视图动画(View Animation)
- 使用无限大小线程池 newCachedThreadPool 可能遇到的问题
- jquery中获取属性值的方法attr() prop()的区别
- Android中利用手机抓包的方法,经典篇
- makefile第一课
- 缓存
- C118 smsweb: 通过日志关键字检测判断obb程序是否工作正常_20160617_七侠镇莫尛貝
- webView
- Q1:入门必备——细说图片读取imread()