一、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
alpha AlphaAnimation
scale ScaleAnimation

一种是frame by frame(画面转换动画)

XML中 JavaCode
translate TranslateAnimation
rotate RotateAnimation

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

步骤如下:

  1. 新建 Android 项目
  2. 在res目录中新建anim文件夹
  3. 在anim目录中新建一个my_anim.xml(注意文件名小写)
  4. 在 my_anim.xml 加入动画代码
<?xml version="1.0" encoding="utf-8"?>                

五、Android XML动画解析

  1. Alpha
<?xml version="1.0" encoding="utf-8"?> 
  1. Scale
<?xml version="1.0" encoding="utf-8"?>   
  1. Translate
<?xml version="1.0" encoding="utf-8"?>
  1. Rotate
<?xml version="1.0" encoding="utf-8"?>  

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"?>            

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"?>                  

activity_frame_animation XML代码

<?xml version="1.0" encoding="utf-8"?>            

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();            }        });    }}

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

更多相关文章

  1. 【转】详解android:scaleType属性
  2. 相对布局的属性
  3. android 属性个人收集 android:visibility
  4. 关于visibility的属性值visible,invisible,gone的区别
  5. Android界面基本属性
  6. 对android LinearLayout中layout_weight属性使用
  7. Android(4)---Android 控件布局常用属性

随机推荐

  1. android:gravity和android:layout_gravit
  2. android layout,xml属性介绍
  3. Android笔试(一)
  4. Android开发入门教程1-初试Android
  5. Android解析服务器端发来的xml数据示例
  6. android面试题总结
  7. Android绘图之2D绘图基础
  8. Mac上如何使用adb命令进行操作?(Android(安
  9. selector 及 Shape 小结
  10. Android蓝牙开发浅析