简而言之:是的,nineoldandroids 确实支持这些。
如果您查看 Nineoldandroids 中的 ObjectAnimator 的源代码,您会注意到它使用代理来为您要设置动画的属性设置动画。
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);
像往常一样使用 ObjectAnimator(只要确保它是 com.nineoldandroids.animation!
ObjectAnimator anim = ObjectAnimator.ofFloat(yourView, "translationX", 0f, 1f);
anim.setDuration(1000);
anim.start();
编辑:这是一个如何在 onTouchListener 中为视图设置动画的示例。请注意,返回 false 表示侦听器尚未消费该事件。
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", 0f, 1f);
anim.setDuration(1000);
anim.start();
return false;
}
});