1:安卓中的动画简述
android中的几种动画:View Animation (补间动画);Drawable Animation(帧动画); Property Animation(属性动画)
1.1:帧动画的简单实用
(1):在res/anim/...xml文件下定义xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- 根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
- 根标签下,通过item标签对动画中的每一个图片进行声明
- android:duration 表示展示所用的该图片的时间长度
- -->
- <animation-list
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true"
- >
- <item android:drawable="@drawable/icon1" android:duration="150"></item>
- <item android:drawable="@drawable/icon2" android:duration="150"></item>
- <item android:drawable="@drawable/icon3" android:duration="150"></item>
- </animation-list>
- animationDrawable = (AnimationDrawable) animationIV.getDrawable();
- animationDrawable.start();
AlphaAnimation,TranslateAnimation,ScaleAnimation,RotateAnomation,AnimationSet
在xml文件中定义使用
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/decelerate_interpolator"
- android:shareInterpolator="true" >
- <scale
- android:duration="2000"
- android:fromXScale="0.2"
- android:fromYScale="0.2"
- android:pivotX="50%"
- android:pivotY="50%"
- android:toXScale="1.5"
- android:toYScale="1.5" />
- <rotate
- android:duration="1000"
- android:fromDegrees="0"
- android:repeatCount="1"
- android:repeatMode="reverse"
- android:toDegrees="360" />
- <translate
- android:duration="2000"
- android:fromXDelta="0"
- android:fromYDelta="0"
- android:toXDelta="320"
- android:toYDelta="0" />
- <alpha
- android:duration="2000"
- android:fromAlpha="1.0"
- android:toAlpha="0.1" />
- </set>
- Animation animation = AnimationUtils.loadAnimation(this,
- R.anim.set_demo);
- imageView.startAnimation(animation);
3:属性动画的简单使用:
3.1:属性动画的一些概念
Duration:设置动画持续时间,默认300ms
Time Intepolation:时间插值,定义动画的变化率。
Repeate Count And Behavior:定义动画的重复次数。
Animator Sets:动画集合,定义一组动画一起执行。
ObjectAnimator,ValueAnimator,AnimatorSet;均为动画的执行类。
AnimationInflater:用于加载属性动画的xml文件
TypeEvaluation:类估值,用于设置属性动画操作的值
3.2:ObjectAnimator
- ImageView view;
- ObjectAnimator//
- .ofFloat(view, "rotationX", 0.0F, 360.0F)//
- .setDuration(500)//
- .start();
3.2.1、提供了ofInt、ofFloat、ofObject,这几个方法都是设置动画作用的元素、作用的属性、动画开始、结束、以及中间的任意个属性值。
当对于属性值,只设置一个的时候,会认为当然对象该属性的值为开始(getPropName反射获取),然后设置的值为终点。如果设置两个,则一个为开始、一个为结束~~~
动画更新的过程中,会不断调用setPropName更新元素的属性,所有使用ObjectAnimator更新某个属性,必须得有getter(设置一个属性值的时候)和setter方法~
3.2.2、如果你操作对象的该属性方法里面,比如上例的setRotationX如果内部没有调用view的重绘,则你需要自己按照下面方式手动调用。
- public void rotateyAnimRun(final View view)
- {
- ObjectAnimator anim = ObjectAnimator//
- .ofFloat(view, "li", 1.0F, 0.0F)// li为随便设置的属性值;
- .setDuration(500);//
- anim.start();
- anim.addUpdateListener(new AnimatorUpdateListener() //在每次更新的时候设置view的属性值
- {
- @Override
- public void onAnimationUpdate(ValueAnimator animation)
- {
- float cVal = (Float) animation.getAnimatedValue(); //获取刚才定义的数值
- view.setAlpha(cVal);
- view.setScaleX(cVal);
- view.setScaleY(cVal);
- }
- });
- }
3.2.3:用propertyValuesHolder设置多个属性的改变
- public void propertyValuesHolder(View view)
- {
- PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
- 0f, 1f);
- PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
- 0, 1f);
- PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
- 0, 1f);
- ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();
- }
3.3:ValueAnimator
3.3.1:基本使用方式
- public void verticalRun( View view)
- {
- ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight
- - view.getHeight());
- animator.setTarget(view); //设置目标
- animator.setDuration(1000).start();
- // animator.setInterpolator(value)
- animator.addUpdateListener(new AnimatorUpdateListener()
- {
- @Override
- public void onAnimationUpdate(ValueAnimator animation)
- {
- view.setTranslationY((Float) animation.getAnimatedValue());
- }
- });
- }
- public void evaluation(View view)
- {
- ValueAnimator valueAnimator = new ValueAnimator();
- valueAnimator.setDuration(3000);
- valueAnimator.setObjectValues(new PointF(0, 0)); //设置自定义对象的属性值
- valueAnimator.setInterpolator(new LinearInterpolator());
- valueAnimator.setEvaluator(new TypeEvaluator<PointF>()
- {
- // fraction = t / duration
- @Override
- public PointF evaluate(float fraction, PointF startValue,
- PointF endValue)
- {
- Log.e(TAG, fraction * 3 + "");
- // x方向200px/s ,则y方向0.5 * 10 * t
- PointF point = new PointF();
- point.x = 200 * fraction * 3;
- point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
- return point;
- }
- });
- valueAnimator.start();
- valueAnimator.addUpdateListener(new AnimatorUpdateListener()
- {
- @Override
- public void onAnimationUpdate(ValueAnimator animation)
- {
- PointF point = (PointF) animation.getAnimatedValue();
- mBlueBall.setX(point.x);
- mBlueBall.setY(point.y);
- }
- });
- }
- public void animation(View view)
- {
- ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f);
- anim.addListener(new AnimatorListener()
- {
- @Override
- public void onAnimationStart(Animator animation)
- {
- Log.e(TAG, "onAnimationStart");
- }
- @Override
- public void onAnimationRepeat(Animator animation)
- {
- // TODO Auto-generated method stub
- Log.e(TAG, "onAnimationRepeat");
- }
- @Override
- public void onAnimationEnd(Animator animation)
- {
- Log.e(TAG, "onAnimationEnd");
- ViewGroup parent = (ViewGroup) mBlueBall.getParent();
- if (parent != null)
- parent.removeView(mBlueBall);
- }
- @Override
- public void onAnimationCancel(Animator animation)
- {
- // TODO Auto-generated method stub
- Log.e(TAG, "onAnimationCancel");
- }
- });
- anim.start();
- }
- public void togetherRun(View view)
- {
- 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.start();
- }
- public void playWithAfter(View view)
- {
- float cx = mBlueBall.getX();
- ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",
- 1.0f, 2f);
- ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",
- 1.0f, 2f);
- ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,
- "x", cx , 0f);
- ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,
- "x", cx);
- /**
- * anim1,anim2,anim3同时执行
- * anim4接着执行
- */
- AnimatorSet animSet = new AnimatorSet();
- animSet.play(anim1).with(anim2);
- animSet.play(anim2).with(anim3);
- animSet.play(anim4).after(anim3);
- animSet.setDuration(1000);
- animSet.start();
- }