关于github上开源nineoldandroids兼容动画的笔记

github上面有个例子很好的列举了NineOldAndroids兼容动画的用法。下载了下来然后运行测试了下:


因为要引用nineoldandroids的项目,我这里直接把它的源代码拷贝进去了,用做测试。

android 11版本也有ObjectAnimator动画属性,但是不能兼容10版本甚至8的版本号,国内这个版本号的手机份额还是不小的。所以兼容也必须只有用这个开源的了。

 static {
        PROXY_PROPERTIES.put("alpha", PreHoneycombCompat.ALPHA);
        PROXY_PROPERTIES.put("pivotX", PreHoneycombCompat.PIVOT_X);
        PROXY_PROPERTIES.put("pivotY", PreHoneycombCompat.PIVOT_Y);
        PROXY_PROPERTIES.put("translationX", PreHoneycombCompat.TRANSLATION_X);
        PROXY_PROPERTIES.put("translationY", PreHoneycombCompat.TRANSLATION_Y);
        PROXY_PROPERTIES.put("rotation", PreHoneycombCompat.ROTATION);
        PROXY_PROPERTIES.put("rotationX", PreHoneycombCompat.ROTATION_X);
        PROXY_PROPERTIES.put("rotationY", PreHoneycombCompat.ROTATION_Y);
        PROXY_PROPERTIES.put("scaleX", PreHoneycombCompat.SCALE_X);
        PROXY_PROPERTIES.put("scaleY", PreHoneycombCompat.SCALE_Y);
        PROXY_PROPERTIES.put("scrollX", PreHoneycombCompat.SCROLL_X);
        PROXY_PROPERTIES.put("scrollY", PreHoneycombCompat.SCROLL_Y);
        PROXY_PROPERTIES.put("x", PreHoneycombCompat.X);
        PROXY_PROPERTIES.put("y", PreHoneycombCompat.Y);
    }

支持的动画属性名有上面那些

支持上下移动的动画实例:

@Override
	protected void setupAnimation(View view) {
		// TODO Auto-generated method stub
//		getAnimatorSet().playTogether(
//				ObjectAnimator.ofFloat(view, "translationY", 0,-300).setDuration(500),
//				ObjectAnimator.ofFloat(view, "translationY", -300,300).setDuration(900)
//				);
//		getAnimatorSet().play(ObjectAnimator.ofFloat(view, "translationY", 0,-300).setDuration(500));
		getAnimatorSet().playSequentially(ObjectAnimator.ofFloat(view, "translationY", 0,-100).setDuration(400),
				ObjectAnimator.ofFloat(view, "translationY", -100,100).setDuration(800),
				ObjectAnimator.ofFloat(view, "translationY", 100,-100).setDuration(800),
				ObjectAnimator.ofFloat(view, "translationY", -100,100).setDuration(800),
				ObjectAnimator.ofFloat(view, "translationY", 100,0).setDuration(400));
	}

也可以用下面的方法:

  @Override
    protected void setupAnimation(View view) {
        getAnimatorSet().playTogether(
                ObjectAnimator.ofFloat(view, "translationY", 0,-100,-200,-300,-200,-100,0,100,200,300,200,100,0,
                		-100,-200,-300,-200,-100,0,100,200,300,200,100, 0).setDuration(4000),
                ObjectAnimator.ofFloat(view, "alpha", 1, 1).setDuration(mDuration*3/2)

        );
    }

这里的效果和上面的效果差不多,只不过是这里分割了整段距离,可以把时间平分,

也就是用

playTogether这个方法也可以实现<pre name="code" class="java">playSequentially这个方法的效果。(播放动画队列,很好理解)


 title = (TextView) findViewById(R.id.title);
        final BaseEffects animator = Effectstype.Slidetogether.getAnimator();
        final Handler handler = new Handler(new Handler.Callback() {
			
			@Override
			public boolean handleMessage(Message arg0) {
				// TODO Auto-generated method stub
				animator.start(title);
				return true;
			}
		});
        new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					try {
						Thread.sleep(8000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					handler.sendEmptyMessage(0);
				}
			}
		}).start();

直接对单个View播放动画,如上图。


笔记先作到这里,如果有错误的,还请各位大牛批评指正。

Write the code,Change the world!



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。