动画
1、分类
        <1>视图动画(view animation)
                (1)帧动画(很多静态图片不断切换的效果,类似gif动态图。)
                (2)补间动画(知道开始和结束,将中间的过程给补充起来。组件从初始状态变成结束状态,为了让改变看起来更自然更平滑的的一种动画)
        <2>属性动画(property animation)3.0以上
2、帧动画
        <1>创建资源文件
                (1)在res/drawable以animation-list作为根节点
                            a.属性oneshot:是否显示一次
- android:oneshot="false"
                            b.子节点<item>设置要显示的图片和时间
- <item android:drawable= "@drawable/girl_1" android:duration="500"/>
        <2>使用
                (1)用图片控件设置背景setBackgroundResource
- img = (ImageView) findViewById(R.id.img);
- //设置图片的背景资源
- img.setBackgroundResource(R.drawable.frame);
                (2)然后通过getBackground()获取里面的资源,将图片资源Drawable转变为动画资源AnimationDrawable,再启动
- //获取到静态图片
- Drawable drawable = img.getBackground();
- //强制转变成动态图
- AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
- //启动动态图片
- animationDrawable.start();
3、补间动画
        <1>平移(TranslateAnimation)
                (1)构造方法
                        (1)fromXDelta, toXDelta, fromYDelta, toYDelta
                            fromXDelta:动画的初始x坐标为:控件的原始坐标+参数
                            toXDelta:动画的结束x坐标:控件的原始坐标+参数
                            fromYDelta:动画的初始y坐标为:控件的原始坐标+参数
                            toYDelta:动画的结束y坐标:控件的原始坐标+参数
                        (2)fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue
                            fromXType:
                                    Animation.RELATIVE_TO_SELF
                                    Animation.RELATIVE_TO_PARENT
                            fromXValue:动画的初始x坐标为:控件的原始坐标+参数*控件/父元素的宽度
                            toXValue:动画的结束x坐标:控件的原始坐标+参数*控件/父元素的宽度
                            fromyValue:动画的初始y坐标为:控件的原始坐标+参数*控件/父元素的高度
                (2)设置展示时间setDuration
                (3)设置重复展示次数.setRepeatCount(2): 无限次:Animation.INFINITE
                (4)设置动画停留在最后结束的位置上:.setFillAfter(true)
                (5)设置重复模式:setRepeatMode(Animation.REVERSE/RESTART)
                (6)让某个空间展示动画 控件.startAnimation(ta)
- //平移
- public void translate(View view){
- //通过XML布局文件实现
- //把一个动画资源文件加成一个动画类
- Animation animation=AnimationUtils.loadAnimation(this,R.anim.mytranslate);
- TranslateAnimation animation2 = (TranslateAnimation) animation;
- //启动一个控件展示动画
- img.startAnimation(animation2);
-     //通过代码实现
-     TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0);
- animation = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, 0,
- Animation.RELATIVE_TO_PARENT, 0.8f,
- Animation.RELATIVE_TO_SELF, 0,
- Animation.RELATIVE_TO_SELF, 0);
- animation.setDuration(2000);//设置时间
- animation.setFillAfter(true);//保证动画最终的效果
- animation.setRepeatCount(Animation.INFINITE);//重复次数
- animation.setRepeatMode(Animation.REVERSE);//重复模式
- img.startAnimation(animation);
- }
        <2>缩放(ScaleAnimation)
                (1)构造方法
                        (1)fromX, toX, fromY, toY(其实就是缩放的大小倍数)
                            fromX:动画的初始宽度为参数*控件的宽度
                            toX:动画的结束宽度为参数*控件的宽度
                            fromY:动画的初始高度为参数*控件的高度
                           toY:动画的结束高度为参数*控件的高度
                        (2)fromX, toX, fromY, toY, pivotX, pivotY
                            pivotX, pivotY:缩放点的x,y坐标
                        (3)fromX, toX, fromY, toY, pivotXType,
                            pivotXValue, pivotYType, pivotYValue
                            pivotXType:缩放类型
                            pivotX, pivotY:(如果类型为相对自己)代表缩放点的x坐标:控件的x+参数*控件的宽度
                (2)设置展示时间setDuration
                (3)设置重复展示次数.setRepeatCount(2)
                (4)设置重复模式:setRepeatMode(Animation.REVERSE)
                (5)设置动画停留在最后结束的位置上:.setFillAfter(true)
                (6)让某个空间展示动画 控件.startAnimation(ta)
- //缩放
- public void scale(View view){
- //通过XML布局文件实现动画
- Animation animation = AnimationUtils.loadAnimation(this, R.anim.myscale);
- ScaleAnimation animation2 = (ScaleAnimation) animation;
- img.startAnimation(animation2);
- //通过代码实现动画,以下是三种构造方法
- ScaleAnimation animation = new ScaleAnimation(1, 0, 1, 0);
- animation = new ScaleAnimation(1, 0, 1, 0, img.getWidth()/2, img.getHeight()/2);
- animation = new ScaleAnimation(1, 0, 1, 0,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_PARENT, 0.5f);
- animation.setDuration(2000);
- img.startAnimation(animation);
- }
        <3>透明(AlphaAnimation)
                (1)构造方法
                        (1)fromAlpha, toAlpha
                            fromAlpha:开始透明度
                            toAlpha:结束透明度    1:全透明   0:不透
- //透明
- public void alpha(View view){
- //通过XML布局文件实现动画
- Animation animation = AnimationUtils.loadAnimation(this, R.anim.myalpha);
- img.startAnimation(animation);
- /**
- * 1, 0.5f:开始和结束的透明度值[0,1]
- */
- AlphaAnimation animation = new AlphaAnimation(1, 0.5f);
- animation.setDuration(2000);
- img.startAnimation(animation);
- }
        <4>旋转(RotateAnimation)
                (1)构造方法
                        (1)fromDegrees,toDegrees
                            开始角度和结束角度
                        (2)fromDegrees, toDegrees, pivotX, pivotY
                            pivotX, pivotY:旋转点的x,y坐标
                        (3)fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue
                            相对位置
                (2)设置展示时间setDuration
                (3)设置重复展示次数.setRepeatCount(2)
                (4)设置动画停留在最后结束的位置上:.setFillAfter(true)
                (5)设置重复模式:setRepeatMode(Animation.REVERSE/RESTART)
                (6)让某个空间展示动画 控件.startAnimation(ta)
- //旋转
- public void rotate(View view){
- //用XML布局文件实现动画
- Animation animation = AnimationUtils.loadAnimation(this, R.anim.myrotate);
- img.startAnimation(animation);
                  //通过代码来实现动画
- RotateAnimation animation = new RotateAnimation(0, -180);
- //img.getWidth()/2:当前的x= 原来的x+参数
- animation = new RotateAnimation(0, 360, img.getWidth()/2, img.getHeight()/2);
- //Animation.RELATIVE_TO_SELF, 0.5f:当前的x =原来的x + 控件的宽度*参数
- //Animation.RELATIVE_TO_PARENT, 0.5f:当前的x = 原来的x + 当前的父布局的宽度*参数
- animation = new RotateAnimation(0, 360,
- Animation.RELATIVE_TO_SELF, 0.5f,
- Animation.RELATIVE_TO_SELF, 0.5f);
- animation.setDuration(2000);
- img.startAnimation(animation);
- //绑定动画的监听器
- animation.setAnimationListener(new AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- }
- });
- }
        <5>用xml文件写补间动画
                在res文件下创建xml文件,文件夹为anim,设置动画类型,时间,重复次数....startOffset:延迟显示时间
- <?xml version="1.0" encoding="utf-8"?>
- <scale
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:fromXScale="1"
- android:toXScale="2"
- android:fromYScale="1"
- android:toYScale="2"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="2000"
- android:fillAfter="true"
- android:repeatCount="1"
- android:repeatMode="reverse"
- >
- <!--
- 缩放的倍数
- android:fromXScale,android:fromYScale="1":初始的x,y的坐标:原来x,y的坐标*参数
- android:pivotX,android:pivotY:缩放点的x,y坐标
- -->
- </scale>
                在java文件中AnimationUtils的加载动画方式加载动画文件, 最后启动就可以了
- Animation animation = AnimationUtils.loadAnimation(this, R.anim.myrotate);
- img.startAnimation(animation);
        <6>启动所有动画AnimationSet
                (1)set.addAnimation:添加动画
- //动画集
- AnimationSet set = new AnimationSet(false);
- //添加动画类型,可以添加多个
- //set.addAnimation(a);
- //set.addAnimation(b);
- //set.addAnimation(c);
- //set.addAnimation(d);
                (2)控件.startAnimation(set)启动
- img.startAnimation(set);
4、属性动画
        <1>ObjectAnimator.ofFloat(  imge, "translationX", -100,100)
                    平移translationX,translationY
                    缩放scaleY,scaleX
                    透明alpha
                    旋转rotationX,rotationY,rotation
        <2>常用设置
                (1)oa.start();//开启动画
                (2)oa.setDuration(2000);//设置时间
                (3)设置重复展示次数.setRepeatCount(2)
                (4)设置重复模式:setRepeatMode(Animation.REVERSE)
        <3>动画集(new AnimatorSet();)
                (1)set.playTogether(ObjectAnimator1,ObjectAnimator2);一起展示
                (2)set.playSequentially(ObjectAnimator1,ObjectAnimator2)挨着展示
                (3)set.start();
        <4>用XML文件写属性动画
                (1)在res文件下创建xml文件,文件夹为animator,子节点为objectAnimator,设置动画类型,时间,重复次数...
- <?xml version="1.0" encoding="utf-8"?>
- <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
- android:duration="2000"
- android:propertyName="translationY"
- android:valueFrom="0"
- android:valueTo="100" >
- <!--
- android:propertyName:设置动画类型
- android:valueFrom="0":开始的值
- android:valueTo="100":结束值
- -->
- </objectAnimator>
                (2)在java文件中AnimatorInflater的加载动画方式加载动画文件.loadAnimator(this, R.animator.myanimator)
- //通过一个动画资源加载器去加载一个动画xml文件
- ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.myobjectanimator);
                (3)最后绑定要展示的控件animator.setTarget(imge);最后启动就可以了
- //绑定动画的执行者
- oa.setTarget(img);
- oa.start();
    失败是什么?没有什么,只是更走近成功一步;成功是什么?就是走过了所有通向失败的路,只剩下一条路,那就是成功的路。
 
                    
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号