Android 动画(二)-Drawable Animation(Frame Animation)、Property Animation


1、Drawable Animation

1)说明

·Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。

·不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用

start()。

·要在代码中调用Imageview的setBackgroundResource方法,如果直接在XML布局文件中设置其src属性当触发动画时会FC。

·在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次。

animation.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false">
    
    <item android:drawable="@drawable/loading_frame1" android:duration="100" />
    <item android:drawable="@drawable/loading_frame2" android:duration="100" />
    <item android:drawable="@drawable/loading_frame3" android:duration="100" />
    <item android:drawable="@drawable/loading_frame4" android:duration="100" />

</animation-list>

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setBackgroundResource(R.drawable.animation);
        anim = (AnimationDrawable) imageView.getBackground();
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            anim.stop();
            anim.start();
            return true;
        }
        return super.onTouchEvent(event);
    }
也可以封装成方法:

public void startAnimation() {
		this.post(new Runnable() {

			@Override
			public void run() {
				AnimationDrawable animationDrawable = (AnimationDrawable) Loading.this
						.getBackground();
				animationDrawable.start();
			}
		});
	}

2.Property Animation

1)ValueAnimator

·说明

         ValueAnimator包含Property Animation动画的所有核心功能,如动画时间,开始、结束属性值,相应时间属性值计算方法等。应用Property Animation有两个步聚:

  1. 计算属性值
  2. 根据属性值执行相应的动作,如改变对象的某一属性。
  ValuAnimiator只完成了第一步工作,如果要完成第二步,需要实现ValueAnimator.onUpdateListener接口

2)ObjectAnimator

·说明

        继承自ValueAnimator,要指定一个对象及该对象的一个属性,实际应用中一般都会用ObjectAnimator来改变某一对象的某一属性,要想使用ObjectAnimator,应该满足以下条件:

          ① 对象应该有一个setter函数:set<PropertyName>(驼峰命名法)
          ② 如下面的中,像ofFloat之类的工场方法,第一个参数为对象名,第二个为属性名,后面的参数为可变参数,如果values…参数只设置了 一个值的话,那么会假定为目的值,属性值的变化范围为当前值到目的值,为了获得当前值,该对象要有相应属性的getter方 法:get<PropertyName>
          ③  如果有getter方法,其应返回值类型应与相应的setter方法的参数类型一致。

    /**
     * Android 源代码
     * @param target The object whose property is to be animated. This object should
     * have a public method on it called <code>setName()</code>, where <code>name</code> is
     * the value of the <code>propertyName</code> parameter.
     * @param propertyName The name of the property being animated.
     * @param values A set of values that the animation will animate between over time.
     * @return An ObjectAnimator object that is set up to animate between the given values.
     */
	public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
        anim.setFloatValues(values);
        return anim;
    }

         ④如果上述条件不满足,则不能用ObjectAnimator,应用ValueAnimator代替。

         ⑤根据应用动画的对象或属性的不同,可能需要在onAnimationUpdate函数中调用invalidate()函数刷新视图。

3)AnimationSet

说明

AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等。

	AnimatorSet bouncer = new AnimatorSet();
	//播放anim1
	bouncer.play(anim1).before(anim2);
	//同时播放anim2,anim3,anim4;
	bouncer.play(anim2).with(anim3);
	bouncer.play(anim2).with(anim4)
	//播放anim5
	bouncer.play(anim5).after(amin2);

4)TypeEvalutors

根据属性的开始、结束值与TimeInterpolation计算出的因子计算出当前时间的属性值,android提供了以下几个evalutor:

     ①IntEvaluator:属性的值类型为int;

     ②FloatEvaluator:属性的值类型为float;

     ③ArgbEvaluator:属性的值类型为十六进制颜色值;

     ④TypeEvaluator:一个接口,可以通过实现该接口自定义Evaluator

	/**
	*自定义FloatEvaluator
	*/
		
	public class FloatEvaluator implements TypeEvaluator {
		public Object evaluate(float fraction, Object startValue, Object endValue) {
			float startFloat = ((Number) startValue).floatValue();
			return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);
		}
	}

5)TimeInterplator

在Property Animation中是TimeInterplator,在View Animation中是Interplator,这两个是一样的,在3.0之前只有Interplator,3.0之后实现代码转移至了 TimeInterplator。

    AccelerateInterpolator           加速,开始时慢中间加速
    DecelerateInterpolator          减速,开始时快然后减速
    AccelerateDecelerateInterolator     先加速后减速,开始结束时慢,中间加速
    AnticipateInterpolator         反向 ,先向相反方向改变一段再加速播放
    AnticipateOvershootInterpolator      反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
    BounceInterpolator          跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
    CycleIinterpolator          循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2 * mCycles * Math.PI * input)
    LinearInterpolator         线性,线性均匀改变
    OvershottInterpolator         超越,最后超出目的值然后缓慢改变到目的值
    TimeInterpolator           一个接口,允许你自定义interpolator,以上几个都是实现了这个接口





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值