代码实现补间动画

来源:互联网 发布:mysql的日期转换函数 编辑:程序博客网 时间:2024/05/21 17:02
//!!!!多个动画组合执行的时候需要使用animationSet,就像是view 和viewGroup。
public void btnAddOne(View view){
        TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,100);
        translateAnimation.setDuration(1000);


        RotateAnimation rotateAnimation = new RotateAnimation(0,7200,0,0);
        rotateAnimation.setDuration(72000);


        AnimationSet animationSet = new AnimationSet(this,null);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(rotateAnimation);
        zanOne.startAnimation(animationSet);
        animationSet.setAnimationListener(this);

    }


++++

ps:动画的总结

补充:代码里面添加动画的操作。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
分为补间动画,帧动画,属性动画。
补间动画:是放在anim里面的
属性动画: 是放在animator里面的
帧动画:  帧动画里面就是播放图片,这些图片放在mipmap里面,然后drawable 里面创建一个animation-list为rootelement的xml文件


补间动画,首先是动画的效果,是位移,大小,旋转和透明度,set集合
    android:duration="2000"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromXScale="1"
    android:fromYScale="1"
    android:toXScale="1.2"
    android:toYScale="1.2"
    android:interpolator="@android:interpolator/bounce"
    android:repeatMode="reverse"
    android:repeatCount="infinite"
代码只有两行
参数有如下的:


Animation animation = new AnimationUtils().loadAnimation(this,R.anim.btn_zan_add);
        mZan.startAnimation(animation);
属性动画,首先动画的效果是,可以是位移,大小,旋转和透明度,set集合,这里面的valueType 可以不设置,如果设置错了反而会不现实效果。
<objectAnimator android:duration="5000"
        android:propertyName="rotation"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="7360"
        />
代码只有三行:
 Animator animator_camera = AnimatorInflater.loadAnimator(this,R.animator.animator);
        animator_camera.setTarget(imageView);
        animator_camera.start();
还可以设置监听:
animator_contact.addListener(this);


帧动画:帧动画里面就是播放图片,这些图片放在mipmap里面,然后drawable 里面创建一个animation-list为rootelement的xml文件
 AnimationDrawable animationDrawable = (AnimationDrawable)animationList.getDrawable();
        if(!animationDrawable.isRunning())
            animationDrawable.start();
        else
            animationDrawable.stop();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++






0:补间动画其实很简单只要两行代码就可以了,必要的时候需要一个监听器:animation.setAnimationListener(this);


Animation animation = new AnimationUtils().loadAnimation(this,R.anim.btn_zan_add);
        mZan.startAnimation(animation);
   
     
1:补间动画有4种动画,rotate,scale,alpha,traslate,为什么动画里面要是set呢?因为这个动画可以是一个结合,例如移动和透明度变化可以一起执行。所以一定是要是集合的。
interpolator 
参数有如下的:
    android:duration="2000"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromXScale="1"
    android:fromYScale="1"
    android:toXScale="1.2"
    android:toYScale="1.2"
    android:interpolator="@android:interpolator/bounce"
    android:repeatMode="reverse"
    android:repeatCount="infinite"


其中pivotX,pivotY,
pivotX,pivotY:
        取值 1:纯数字;代表,当前控件的左上角位置
                        android:pivotX="30"
                        android:pivotY="30"
               2:空间百分比,代表,以当前空间尺寸为基准,百分比位置。
                        android:pivotX="50%"
                        android:pivotY="50%"
               3:芙蓉起百分比,代表当前控件父容器中的百分比


                        android:pivotX="50%p"
                        android:pivotY="50%p"
 android:interpolator="@android:interpolator/linear" api 11才有这个
如果linear 的话就是线性的,如果是bounce就是带回弹效果的,如果是accelerate的话就是加速的。


2: 补间动画的特点:任何动画的操作都不会影响实际空间的状态,
帧动画:其实也很简单就是下面的,一定要记住要强制转换成AnimationDrawable。然后才可以播放和停止帧动画。
和上面的补间动画不同的是,补间动画是通过组件来启动动画,而这个是通过AnimationDrawable ,其实本质上面都是一样的,补间动画的组件不就是动画,而AnimationDrawable 部就是动画的列表




        ImageView animationList = (ImageView) findViewById(R.id.animationList);


        AnimationDrawable animationDrawable = (AnimationDrawable)animationList.getDrawable();
        if(!animationDrawable.isRunning())
            animationDrawable.start();
        else
            animationDrawable.stop();
    
首相将动画放到mipmap里面去,然后在drawable 里面创建一个rootElement为animation-list的文件drawable_elec,
然后将这个animationList添加内容如下:


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/elec01" android:duration="200"/>
<item android:drawable="@mipmap/elec02" android:duration="200"/>
<item android:drawable="@mipmap/elec03" android:duration="200"/>
<item android:drawable="@mipmap/elec04" android:duration="200"/>
<item android:drawable="@mipmap/elec05" android:duration="200"/>
</animation-list>


布局里面的ImageView的src 就填写这个
<ImageView
        android:layout_marginBottom="100dp"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/drawable_elec"
        android:id="@+id/animationList"/>


3:属性动画
设置ordering的值为together
propertyName 属性代表 对象 修改的属性名称;要求兑现个必须
            必须包含setxxx;
            例如propertyName 设置为textColor 之后,那么要求对象必须包含setTextColor(int);
valueType: 属性,代表,属性的数据类型,支持float和int ,只有这两种,
            例如int-> setXXX(int) float -》setXXX(float);


首先在res下面创建一个animator的文件夹。然后再这里面
创建一个set集合的,设置ordering为together。


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together">
    <objectAnimator android:duration="5000"
        android:propertyName="rotation"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="7360"
        />
</set>


然后布局里面:
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/composer_camera"
        android:id="@+id/composer_camera"
        android:onClick="animator"/>


然后MainActivity
    public void animator(View view) {
        ImageView imageView =(ImageView)findViewById(R.id.composer_camera);
        Animator animator_camera = AnimatorInflater.loadAnimator(this,R.animator.animator);
        animator_camera.setTarget(imageView);
        animator_camera.start();
    }












++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


0:动画的设置是在res下面创建一个anim 的文件夹,然后再创建resouce的文件,记得root element 一定要选择set,集合,
 <set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0"
    android:toYDelta="-100"
    android:duration="200"></translate>
</set>




还有要注意的是要在TextView 上面设置点击事件之前 要先设置 这个 TextView 是可以被点击的 。
<TextView android:text="Hello World!" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:onClick="zanAdd"/>




    <TextView
        android:id="@+id/zan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+1"
        android:textColor="#600F"
        android:visibility="invisible"/>


首先动画Animation 是一个抽象的类是不可以通过new Animation来创建的必须要通过AnimationUtils.loadAnimation(this,R.anim.btn_add);来创建
其次,启动动画的时候是需要在那个上面有动画就要这个View 来启动,例如这里就是zan的这个所以就是zan.startAnimation(animation)
public void zanAdd(View view) {
        mZan.setVisibility(View.VISIBLE);
        Animation animation = new AnimationUtils().loadAnimation(this,R.anim.btn_zan_add);
        mZan.startAnimation(animation);
        animation.setAnimationListener(this);
    }










补间动画的重点:++++++++++++++++++++++++++++++++++++++++++++
+++1:任何动画的操作都不会影响实际空间的状态,
它还在原来的位置,因为实际上不见动画,只是把空间的外观进行了各种黑醋栗,相当于空间还是原来的位置
,还是原来的尺寸。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


6:代码形式的补间动画设置。很少用。++++
public void btnAddOne(View view){
//        zanOne.setVisibility(View.VISIBLE);
//        Animation animation = AnimationUtils.loadAnimation(this, R.anim.btn_add_zan);
//        zanOne.startAnimation(animation);
//        animation.setAnimationListener(this);


        TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,100);
        translateAnimation.setDuration(1000);
        zanOne.startAnimation(translateAnimation);
        translateAnimation.setAnimationListener(this);
    }


如果有两种animation要放在set 里面:
如果考虑set
//!!!!多个动画组合执行的时候需要使用animationSet,就像是view 和viewGroup。
public void btnAddOne(View view){
        TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,100);
        translateAnimation.setDuration(1000);


        RotateAnimation rotateAnimation = new RotateAnimation(0,7200,0,0);
        rotateAnimation.setDuration(72000);


        AnimationSet animationSet = new AnimationSet(this,null);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(rotateAnimation);
        zanOne.startAnimation(animationSet);
        animationSet.setAnimationListener(this);
    }




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


逐帧动画:
首先将动画的图片放到该文件夹drawable-hdpi里面
然后在res 下面创建一个drawable 的文件夹 然后创建一个drawable 的文件
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:duration="250" android:drawable="@drawable/elec01"/>
    <item android:duration="250" android:drawable="@drawable/elec02"/>
    <item android:duration="250" android:drawable="@drawable/elec03"/>
    <item android:duration="250" android:drawable="@drawable/elec04"/>
    <item android:duration="250" android:drawable="@drawable/elec05"/>
    <item android:duration="250" android:drawable="@drawable/elec06"/>
</animation-list>
然后创建一个button 来里面的onclick 方法来控制开始停止
public void btnAnimationList(View view) {
        Drawable drawable = animiation_list.getDrawable();
        if(drawable!=null){
            if(drawable instanceof AnimationDrawable){
                AnimationDrawable animationDrawable = (AnimationDrawable)drawable;
                if(animationDrawable.isRunning()){
                    animationDrawable.stop();
                }else{
                    animationDrawable.start();
                }
            }
        }
    }


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




//1.加载xml
//相当于属性修改器
//2.给动画设置目标 属性就是设置属性啊属性 属性就是设置属性啊属性 属性就是设置属性啊属性 属性就是设置属性啊属性 属性就是设置属性啊属性
//设置的目标,内部必须包含属性动画配置文件中指定的属性名的setXXX方法,
//3.动画执行,启动


Animator animator =AnimatorInflater.loadAnimator(this,R.animator.animator_path);
animator.setTarget(view);
animator.start();


如果要在代码中实现就如下:
    public void btnupdate(View view){
//        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.ic_launcher);
//        animator.setTarget(view);
//        animator.start();


        //代码形式的属性动画设置
        //利用静态方法来创建一个修改属性的Animator,比我们从布局加载更简单,因为这个值是可以动态变化的。
        float tX = view.getTranslationX();
        ObjectAnimator animator=ObjectAnimator.ofFloat(view, "translationX", new float[]{tX, tX + 100});
        animator.setTarget(view);
        animator.start();


    }




XML的创建过程:注意:android:ordering="sequentially" 




新建一个文件夹,animator,然后xml 的root 选择objectAnimator


<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:ordering="sequentially">
<!--属性动画,就是在一定时间内,针对一个对象的某一个或者一些属性进行修改,
修改的范围是线性的连续的;
objectAnimator 用于修改对象的属性,
propertyName 属性代表 对象 修改的属性名称;要求兑现个必须
            必须包含setxxx;
            例如propertyName 设置为textColor 之后,那么要求对象必须包含setTextColor(int);
valueType: 属性,代表,属性的数据类型,支持float和int ,只有这两种,
            例如int-> setXXX(int) float -》setXXX(float);
valueFrom:
valueTo:
-->
<objectAnimator
        android:duration="5000"
        android:propertyName="left"
        android:valueType="intType"
        android:valueFrom="0"
        android:valueTo="100"
        />
    <objectAnimator android:duration="5000"
                    android:propertyName="rotation"
                    android:valueType="floatType"
                    android:valueFrom="0"
                    android:valueTo="7200"
    />
</set>






作业:
使用到接口回调。
//Contact
            Animator animator_contact = AnimatorInflater.loadAnimator(this, R.animator.animator_back_contact);
            animator_contact.setTarget(imageView_composer_contact);
            animator_contact.start();
            animator_contact.addListener(this);


    @Override
    public void onAnimationEnd(Animator animation) {


        imageView_composer_camera.setVisibility(View.GONE);
        imageView_composer_music.setVisibility(View.GONE);
        imageView_composer_sms.setVisibility(View.GONE);
        imageView_composer_contact.setVisibility(View.GONE);
    }






回弹
animator_contact.setInterpolator(new OvershootInterpolator());


<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:ordering="together">
<objectAnimator
    android:duration="500"
    android:propertyName="translationY"
    android:valueFrom="0"
    android:valueTo="173"
    />
    <objectAnimator
            android:duration="500"
            android:propertyName="translationX"
            android:valueFrom="0"
            android:valueTo="100"
            />
    <objectAnimator
        android:duration="500"
        android:propertyName="Rotation"
        android:valueFrom="0"
        android:valueTo="720"
        />


</set>

0 0
原创粉丝点击