关于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!