Android的View动画使用总结
2017-03-16 11:52
232 查看
View动画的四种变换
名称 | 标签 | 子类 | 效果 |
---|---|---|---|
平移动画 | <translate> | TranslateAnimation | 移动View |
缩放动画 | <scale> | ScaleAnimatioin | 放大或缩小View |
旋转动画 | <Rotate> | RotateAnimation | 旋转View |
透明度动画 | <alpha> | AlphaAnimation | 改变View的透明度 |
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" //持续时间 set的属性会覆盖下面子动画的属性 android:interpolator="@anim/interpolator_resource" //动画集合采用的插值器 默认为加速减速插值器 android:shareInterpolator="true" //表示集合动画是否和集合共享同一个插值器。如果集合不指定插值器,子动画就需要单独指定所需的插值器或者默认值 android:startOffset="1000" //动画开始前延时 可以在子动画中加入,实现顺序执行 默认为一起执行 android:fillAfter="false" //动画结束后View是否留在结束后的状态 默认为false android:fillBefore="false"> //动画开始前View是否留在开始时的状态 默认为false <alpha android:duration="1000" android:fromAlpha="0" //起始透明度 0 ~ 1 android:toAlpha="0.8"/> <rotate android:duration="2000" android:fromDegrees="60" //起始旋转的角度 android:pivotX="50%" //起始旋转中点 默认是坐上角 android:pivotY="50%" android:toDegrees="360"/> <scale android:duration="1500" android:fromXScale="100%" //x方向的缩放起始值 android:fromYScale="100%" android:pivotX="50%" //缩放位置的x坐标 默认是View左上角 android:pivotY="50%" android:toXScale="0%" //x方向的缩放结束值 android:toYScale="0%"/> <translate android:duration="3000" android:fromXDelta="10" //x的起始位置(相对于原View)单位像素(也可使用50%这样的百分比 相对于原View大小) android:fromYDelta="10" android:toXDelta="1" //x的结束位置 android:toYDelta="1"/> </set>
系统提供的插值器
java类 | xml | id值 | 描述 |
---|---|---|---|
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 动画始末速率较慢,中间加速 | |
AccelerateInterpolator | @android:anim/accelerate_interpolator | 动画开始速率较慢,之后慢慢加速 | |
AnticipateInterpolator | @android:anim/anticipate_interpolator | 开始的时候从后向前甩 | |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 类似上面AnticipateInterpolator | |
BounceInterpolator | @android:anim/bounce_interpolator | 动画结束时弹起 | |
CycleInterpolator | @android:anim/cycle_interpolator | 循环播放速率改变为正弦曲线 | |
DecelerateInterpolator | @android:anim/decelerate_interpolator | 动画开始快然后慢 | |
LinearInterpolator | @android:anim/linear_interpolator | 动画匀速改变 | |
OvershootInterpolator | @android:anim/overshoot_interpolator | 向前弹出一定值之后回到原来位置 | |
PathInterpolator | 新增,定义路径坐标后按照路径坐标来跑。 |
使用View动画
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_anim); imageView.startAnimation(animation); //设置监听 animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { Log.d("myinfo", "onAnimationStart"); } @Override public void onAnimationEnd(Animation animation) { Log.d("myinfo", "onAnimationEnd"); } @Override public void onAnimationRepeat(Animation animation) { Log.d("myinfo", "onAnimationRepeat");//动画重复 } });
注意:
set本身也是继承自Animation类,他的属性会覆盖子动画的相关属性。
repeatMode属性必须和repeatCount属性结合使用,并且不能在set中设置,否则无效
set的AnimationListener不能监听到动画重复事件,并且如果子动画没有结束,同样监听不到动画结束
android:repeatCount={infinite(无限)|100(具体的重复次数)} android:repeatMode={reverse(颠倒回放)|restart(重新回放)}
自定义View动画
主要通过继承Animatioin抽象类 并应用矩阵变换操作View动画的应用
1. LayoutAnimation 作用于 ViewGroup,当他的子元素出场时都会具有这种动画效果。
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/rcv_item_anim" android:delay="0.1" //延时执行多少周期,比如动画时长为10s,这的0.1代表,下一个View的出场动画在上一个View的出场动画执行后1s(0.1*10)再执行。默认是一起执行 android:animationOrder="normal" //子View动画执行顺序,默认为顺序执行 /> 在ViewGroup的xml中加上 android:layoutAnimation="@anim/layout_anim" 或者: Animation animation = AnimationUtils.loadAnimation(this, R.anim.rcv_item_anim); LayoutAnimationController controller = new LayoutAnimationController(animation); controller.setDelay(0.1f); controller.setOrder(LayoutAnimationController.ORDER_NORMAL); recyclerView.setLayoutAnimation(controller);
2. Activity的切换效果
overridePendingTransition(R.anim.act_in_anim, R.anim.act_out_anim); 第一个参数是Activity入场的动画,第二个参数是Activity出场等待动画。 **注意:**这个方法必须在startActivity() 或 finish()这两个方法的后面调用,否则无效。
帧动画
帧动画容易引起OOM 避免使用大尺寸图片例子(文件保存在res/drawable文件夹下):
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> //动画是否只执行1次,为false的话可以一直重复执行 <item android:drawable="@drawable/ic_launcher" android:duration="1000"/> <item android:drawable="@drawable/ic_launcher_round" android:duration="1000"/> </animation-list>
linearLayout.setBackgroundResource(R.drawable.drawable_anim); AnimationDrawable animationDrawable = (AnimationDrawable) linearLayout.getBackground(); animationDrawable.start();
参考文章:
- 《Android开发艺术探究》
- http://blog.csdn.net/yanbober/article/details/46481171
相关文章推荐
- Android之SurfaceView使用总结
- Android之SurfaceView使用总结
- Android Fragment使用总结以及与Viewpager相结合使用
- ANDROID笔记:使用动画替代viewpager的header
- Android之SurfaceView使用总结
- 玩转Android---2D图形及动画---View类使用
- android TextView的使用总结
- Android中Webview使用经验总结
- Android WebView使用总结
- Android使用WebView加载文字总结之中文乱码、禁止横向滚动、自动换行
- Android-使用SurfaceView多线程绘制动画
- Android游戏开发---View线程绘动画与显示总结
- android TextView的使用总结
- android TextView的使用总结
- 玩转Android---2D图形及动画---View类使用
- android中使用2D动画 — SurfaceView 转载
- android TextView的使用总结
- Android-----WebView使用知识点总结
- Android动画效果和怎样使用自定义动画的总结
- android中webView使用总结---飞速浏览器实现基础.