Android Animations动画详解

一、Animation 动画属性

动画相关的属性:SET属性

名称属性备注
android:shareInterpolator是否共享插入器共享时,四个子节点都用一个插入器
android:interpolator指定一个动画的插入器使用系统资源
android:fillEnabled当设置为true时,fillAfter和fillBefroe将会都为true,此时会忽略fillBefore 和fillAfter两种属性
android:fillAfter该动画转化是否在动画结束后被应用boolean
android:fillBefore该动画转化是否在动画开始前被应用boolean
android:repeatMode重复模式“restart” =从头开始 或者 “reverse”=从末尾开始
android:repeatCount重复次数integer -1为无限循环
android:duration动画持续时间integer
android:startOffset动画时间间隔(动画执行前停留时间)long
android:zAdjustment定义动画z order的变换[normal] or [top] or [bottom]
android:detachWallpaper未知boolean

二、Animation 动画类型

Android的animation由四种类型组成:

XML中
名称作用
alph渐变透明度动画效果
scale渐变尺寸伸缩动画效果
translate画面转换位置移动动画效果
rotate画面转移旋转动画效果
JavaCode中
名称作用
AlphaAnimation渐变透明度动画效果
ScaleAnimation渐变尺寸伸缩动画效果
TranslateAnimation画面转换位置移动动画效果
RotateAnimation画面转移旋转动画效果

三、Android动画模式

Animation主要有两种动画模式:

一种是tweened animation(渐变动画)
XML中JavaCode
alphaAlphaAnimation
scaleScaleAnimation
一种是frame by frame(画面转换动画)
XML中JavaCode
translateTranslateAnimation
rotateRotateAnimation

四、如何在XML文件中定义动画

步骤如下:

  1. 新建 Android 项目
  2. 在res目录中新建anim文件夹
  3. 在anim目录中新建一个my_anim.xml(注意文件名小写)
  4. 在 my_anim.xml 加入动画代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha />
    <scale />
    <translate />
    <rotate />
</set>

五、Android XML动画解析

  1. Alpha
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
/> 
<!-- 透明度控制动画效果 alpha
        浮点型值:
            fromAlpha 属性为动画起始时透明度
            toAlpha   属性为动画结束时透明度
            说明: 
                0.0表示完全透明
                1.0表示完全不透明
            以上值取0.0-1.0之间的float数据类型的数字

        长整型值:
            duration  属性为动画持续时间
            说明:     
                时间以毫秒为单位
-->
</set>
  1. Scale
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <scale  
          android:interpolator=
                     "@android:anim/accelerate_decelerate_interpolator"
          android:fromXScale="0.0"
          android:toXScale="1.4"
          android:fromYScale="0.0"
          android:toYScale="1.4"
          android:pivotX="50%"
          android:pivotY="50%"
          android:fillAfter="false"
          android:duration="700" />
</set>
<!-- 尺寸伸缩动画效果 scale
       属性:interpolator 指定一个动画的插入器
        在我试验过程中,使用android.res.anim中的资源时候发现
        有三种动画插入器:
            accelerate_decelerate_interpolator  加速-减速 动画插入器
            accelerate_interpolator         加速-动画插入器
            decelerate_interpolator         减速- 动画插入器
        其他的属于特定的动画效果
      浮点型值:

            fromXScale 属性为动画起始时 X坐标上的伸缩尺寸    
            toXScale   属性为动画结束时 X坐标上的伸缩尺寸     

            fromYScale 属性为动画起始时Y坐标上的伸缩尺寸    
            toYScale   属性为动画结束时Y坐标上的伸缩尺寸    

            说明:
                 以上四种属性值    

                    0.0表示收缩到没有 
                    1.0表示正常无伸缩     
                    值小于1.0表示收缩  
                    值大于1.0表示放大

            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置

            说明:
                    以上两个属性值 从0%-100%中取值
                    50%为物件的X或Y方向坐标上的中点位置

        长整型值:
            duration  属性为动画持续时间
            说明:   时间以毫秒为单位

        布尔型值:
            fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
-->
  1. Translate
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="30"
android:toXDelta="-80"
android:fromYDelta="30"
android:toYDelta="300"
android:duration="2000"
/>
<!-- translate 位置转移动画效果
        整型值:
            fromXDelta 属性为动画起始时 X坐标上的位置    
            toXDelta   属性为动画结束时 X坐标上的位置
            fromYDelta 属性为动画起始时 Y坐标上的位置
            toYDelta   属性为动画结束时 Y坐标上的位置
            注意:
                     没有指定fromXType toXType fromYType toYType 时候,
                     默认是以自己为相对参照物             
        长整型值:
            duration  属性为动画持续时间
            说明:   时间以毫秒为单位
-->
</set>
  1. Rotate
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0" 
        android:toDegrees="+350"         
        android:pivotX="50%" 
        android:pivotY="50%"     
        android:duration="3000" />  
<!-- rotate 旋转动画效果
       属性:interpolator 指定一个动画的插入器
             在我试验过程中,使用android.res.anim中的资源时候发现
             有三种动画插入器:
                accelerate_decelerate_interpolator    加速-减速 动画插入器
                accelerate_interpolator                加速-动画插入器
                decelerate_interpolator                减速- 动画插入器
             其他的属于特定的动画效果

       浮点数型值:
            fromDegrees 属性为动画起始时物件的角度    
            toDegrees   属性为动画结束时物件旋转的角度 可以大于360度   


            说明:
                     当角度为负数——表示逆时针旋转
                     当角度为正数——表示顺时针旋转              
                     (负数from——to正数:顺时针旋转)   
                     (负数from——to负数:逆时针旋转) 
                     (正数from——to正数:顺时针旋转) 
                     (正数from——to负数:逆时针旋转)       

            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置

            说明:        以上两个属性值 从0%-100%中取值
                         50%为物件的X或Y方向坐标上的中点位置

        长整型值:
            duration  属性为动画持续时间
            说明:       时间以毫秒为单位
-->
</set>

XML中使用动画效果

public static Animation loadAnimation (Context context, int id) 
//第一个参数Context为程序的上下文    
//第二个参数id为动画XML文件的引用
//例子:
myAnimation= AnimationUtils.loadAnimation(this, R.anim.my_action);
//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件

六、Java代码实现上述动画

xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/MatchMatch"
    android:orientation="vertical"
    tools:context="com.example.administrator.foundationdemo.animation.AnimationActivity">

    <LinearLayout
        android:orientation="horizontal"
        style="@style/MatchWrap">
        <Button
            android:id="@+id/tweened_rotate_btn"
            style="@style/WrapWrap"
            android:text="旋转" />
        <Button
            android:id="@+id/tweened_scale_btn"
            style="@style/WrapWrap"
            android:text="缩放" />
        <Button
            android:id="@+id/tweened_alpha_btn"
            style="@style/WrapWrap"
            android:text="淡入淡出" />
        <Button
            android:id="@+id/tweened_translate_btn"
            style="@style/WrapWrap"
            android:text="移动" />

    </LinearLayout>
    <ImageView
        android:id="@+id/tweened_img"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:src="@drawable/mz"/>
    <Button
        android:id="@+id/Tween_Animations"
        style="@style/WrapWrap"
        android:text="帧动画"/>
</LinearLayout>

AnimationActivity代码

package com.example.administrator.foundationdemo.animation;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.example.administrator.foundationdemo.R;

public class AnimationActivity extends AppCompatActivity {

    private Button tweened_rotate_btn;
    private Button tweened_scale_btn;
    private Button tweened_alpha_btn;
    private Button tweened_translate_btn;
    private ImageView tweened_img;
    private Button tween_animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        init();
        viewListener();
    }

    private void init() {

        tweened_rotate_btn = (Button) findViewById(R.id.tweened_rotate_btn);
        tweened_scale_btn = (Button) findViewById(R.id.tweened_scale_btn);
        tweened_alpha_btn = (Button) findViewById(R.id.tweened_alpha_btn);
        tweened_translate_btn = (Button) findViewById(R.id.tweened_translate_btn);
        tweened_img = (ImageView) findViewById(R.id.tweened_img);
        tween_animation = (Button) findViewById(R.id.tween_animation);


    }

    private void viewListener() {
        tweened_rotate_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //创建一个AnimationSet对象 参数为boolean型

                //true表示使用Animation的interpolation,false则是使用自己的
                AnimationSet animationSet = new AnimationSet(true);

                //参数1:从哪个旋转角度开始

                //参数2:转到什么角度

                //后4个参数用于设置围绕着旋转的圆的圆心在哪里

                //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标

                //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴

                //参数5:确定y轴坐标的类型

                //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴

                RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

                        Animation.RELATIVE_TO_SELF, 0.5f,

                        Animation.RELATIVE_TO_SELF, 0.5f);
                //设置执行时间,单位ms
                rotateAnimation.setDuration(1000);
                //将动画对象添加到序列中
                animationSet.addAnimation(rotateAnimation);

                tweened_img.startAnimation(animationSet);

            }
        });

        tweened_scale_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AnimationSet animationSet = new AnimationSet(true);

                //参数1:x轴的初始值

                //参数2:x轴收缩后的值

                //参数3:y轴的初始值

                //参数4:y轴收缩后的值

                //参数5:确定x轴坐标的类型

                //参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴

                //参数7:确定y轴坐标的类型

                //参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴

                ScaleAnimation scaleAnimation = new ScaleAnimation(

                        0, 0.1f,0,0.1f,

                        Animation.RELATIVE_TO_SELF,0.5f,

                        Animation.RELATIVE_TO_SELF,0.5f);

                scaleAnimation.setDuration(1000);

                animationSet.addAnimation(scaleAnimation);

                tweened_img.startAnimation(animationSet);
            }
        });

        tweened_alpha_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建一个AnimationSet对象 参数为boolean型
                //true表示使用Animation的interpolation,false则是使用自己的
                AnimationSet animationSet = new AnimationSet(true);
                //创建一个AlphaAnimation对象,参数透明度,1完全透明,0不透明
                AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
                //设置执行时间,单位ms
                alphaAnimation.setDuration(1000);
                //将动画对象添加到序列中
                animationSet.addAnimation(alphaAnimation);
                tweened_img.startAnimation(animationSet);
            }
        });

        tweened_translate_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AnimationSet animationSet = new AnimationSet(true);

                //参数1~2:x轴的开始位置

                //参数3~4:y轴的开始位置

                //参数5~6:x轴的结束位置

                //参数7~8:x轴的结束位置

                TranslateAnimation translateAnimation =

                        new TranslateAnimation(

                                Animation.RELATIVE_TO_SELF,0f,

                                Animation.RELATIVE_TO_SELF,0.5f,

                                Animation.RELATIVE_TO_SELF,0f,

                                Animation.RELATIVE_TO_SELF,0.5f);

                translateAnimation.setDuration(1000);

                animationSet.addAnimation(translateAnimation);

                tweened_img.startAnimation(animationSet);

            }
        });

        tween_animation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent = new Intent(AnimationActivity.this,TweenAnimationActivity.class);
                startActivity(mIntent);
            }
        });
    }


}

七、帧动画

xml代码

res下新建anim文件夹animation_list XML代码

<?xml version="1.0" encoding="utf-8"?>
<animation-list  xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/a1" android:duration="100"/>
    <item android:drawable="@drawable/a2" android:duration="100"/>
    <item android:drawable="@drawable/a3" android:duration="100"/>
    <item android:drawable="@drawable/a1" android:duration="100"/>
</animation-list>  

activity_frame_animation XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放动画" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止动画" />

    </LinearLayout>



    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="单次播放" />


        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="循环播放" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拖动进度条修改透明度(0 - 255)之间" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:background="@anim/animation_list" />

</LinearLayout>  

FrameAnimationActivity代码

package com.example.administrator.foundationdemo.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.example.administrator.foundationdemo.R;

public class FrameAnimationActivity extends AppCompatActivity {

    /** Called when the activity is first created. */
    private Button button1,button2;
    private RadioGroup radioGroup;
    private RadioButton radioButton1,radioButton2;
    private SeekBar seekBar;
    private ImageView imageView1;
    private AnimationDrawable animationDrawable;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_animation);

        button1=(Button) this.findViewById(R.id.button1);
        button2=(Button) this.findViewById(R.id.button2);
        radioGroup=(RadioGroup) this.findViewById(R.id.radioGroup1);
        radioButton1=(RadioButton) this.findViewById(R.id.radioButton1);
        radioButton2=(RadioButton) this.findViewById(R.id.radioButton2);
        seekBar=(SeekBar) this.findViewById(R.id.seekBar1);
        imageView1=(ImageView) this.findViewById(R.id.imageView1);

        //通过ImageView对象拿到背景显示的AnimationDrawable
        animationDrawable=(AnimationDrawable) imageView1.getBackground();

        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!animationDrawable.isRunning()){
                    animationDrawable.start();
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(animationDrawable.isRunning()){
                    animationDrawable.stop();
                }
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(checkedId==radioButton1.getId()){
                    //设置单次播放
                    animationDrawable.setOneShot(true);
                }else if(checkedId==radioButton2.getId()){
                    //设置循环播放
                    animationDrawable.setOneShot(false);
                }
                //设置播放后重新启动
                animationDrawable.stop();
                animationDrawable.start();
            }
        });
        //监听的进度条修改透明度
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {


            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                //设置动画Alpha值
                animationDrawable.setAlpha(progress);
                //通知imageView 刷新屏幕
                imageView1.postInvalidate();
            }
        });
    }
}

上述就是,属性动画,补间动画,帧动画的全部类容,希望对大家有帮助

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值