首页 > 系统 > Android > 正文

android动画整理

2019-11-09 18:26:38
字体:
来源:转载
供稿:网友

         在Android里大概提供了几种动画,有补间动画、帧动画、以及属性动画。补间动画支持四种类型:平移(Translate)、旋转(Rotate)、缩放(Scale)、不透明度(Alpha),View Animation是此类动画。帧动画是一系列连续的图片组成的动画,像电影一样,Drawable Animation指的是此类动画。而属性动画就是通过改变属性的方式来实现动画,需要支持对所有View能更新的属性的动画(需要属性的setXxx()和getXxx())。

        1.补间动画的大概使用方法,一般可以在静态的使用和动态的使用分为2种,静态使用是需要在xml里定义在通过代码的形式进行加载,而动态则是通过代码的形式直接进行实现的一种方式:

       在xml里写法,名字叫alphatween:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:fillAfter="true"    android:fillEnabled="true">    <alpha         android:fromAlpha="1"        android:toAlpha="0"        android:duration="500"        /></set>     在Activity里的引用:

   Animation anima=AnimationUtils.loadAnimation(this,R.anim.alphatween);   mMv.startAnimation(anima);在这里想要强调的一点是,如何想要最后的动画效果留下来,是需要在set里写android:fillAfter="true",android:fillEnabled="true",否则是不会保存效果的。

      动态的使用方式:

   AnimationSet animationSet=new AnimationSet(true);   animationSet.setFillAfter(true);   animationSet.setFillEnabled(true);   Animation anima=new TranslateAnimation(0, 300,0, 0);   anima.setDuration(500);   animationSet.addAnimation(anima);   mMv.startAnimation(animationSet); 2.帧动画的使用方法:

在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/birdie1" android:duration="400"></item>    <item android:drawable="@drawable/birdie2" android:duration="400"></item>    <item android:drawable="@drawable/birdie3" android:duration="400"></item>  <item android:drawable="@drawable/birdie4" android:duration="400"></item></animation-list>      在布局里写好:

<ImageView        android:id="@+id/id_mv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:src="@drawable/donghua" /> 之后在代码里引用效果:

   AnimationDrawable anima=(AnimationDrawable) id_bired.getBackground();   anima.start(); 3.属性动画的使用方法:

ObjectAnimator :对象动画执行类。ValueAnimator :值动画执行类,常配合AnimatorUpdateListener使用。PRopertyValuesHolder : 属性存储器,为两个执行类提供更新多个属性的功能。Keyframe :为 PropertyValuesHolder提供多个关键帧的操作值。AnimatorSet :动画集合,你可以定义一组动画,一起执行或者顺序执行。。AnimatorUpdateListener :动画更新监听。AnimatorListener :动画执行监听,在动画开始、重复、结束、取消时进行回调。AnimatorInflater :加载属性动画的xml文件。TypeEvaluator :类型估值,用于设置复杂的动画操作属性的值。TimeInterpolator :时间插值,用于控制动画执行过程。

    id_ball是要实现动画的对象,x是指x轴进行动画,这是一个向右滑动的一个动画。

ObjectAnimator anim = ObjectAnimator				.ofFloat(id_ball, "x", 1.0F,  500F)				.setDuration(1000);		anim.start();    这个是一个绕y轴的一个旋转动画的效果。

//旋转动画效果		ValueAnimator valueanimator=ValueAnimator.ofFloat(0F,720F);		valueanimator.setDuration(3000);		valueanimator.addUpdateListener(new AnimatorUpdateListener() {			@Override			public void onAnimationUpdate(ValueAnimator animation) {				Float anima=(Float) animation.getAnimatedValue();				id_ball.setRotationY(anima);			}		});		valueanimator.start();      之后是组合动画的一个使用,

     

ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",				1.0f, 2f);		ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",				1.0f, 2f);				AnimatorSet animSet = new AnimatorSet();		animSet.setDuration(2000);		animSet.setInterpolator(new LinearInterpolator());		//两个动画同时执行		animSet.playTogether(anim1, anim2);//		animSet.playSequentially(anim1, anim2);//动画顺序播放		animSet.start();        基本的使用方式大致上就是这样的。

      


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表