标签:
功能介绍:让按钮水平向右移动100单位
Button btn = ... ObjectAnimator.ofFloat(btn,"translationX",100).setDuration(2000).start();
Button btn = ...
ValueAnimator animator = ValueAnimator.ofFloat(0,100).setDuration(2000);
animator.start();
animator.addUpdateListener(new AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
float curVal = (float) animation.getAnimatedValue();
ViewHelper.setTranslationY(btn,curVal);//注意不要使用btn.setTranslationY
}
});
Button btn =... AnimatorSet set = new AnimatorSet(); set.playTogether( ObjectAnimator.ofFloat(btn,"translationX",100), ObjectAnimator.ofFloat(btn,"translationY",100), ObjectAnimator.ofInt(btn,"alpha",1,0) ); set.setDuration(2000); set.start();当然了,这个效果也可以通过PropertyValuesHolder来实现:
Button btn = ...
ObjectAnimator.ofPropertyValuesHolder(btn,
PropertyValuesHolder.ofFloat("translationX",100),
PropertyValuesHolder.ofFloat("translationY",100),
PropertyValuesHolder.ofInt("alpha",1,0)
).start();
Button btn = ... ViewPropertyAnimator.animate(btn).translationX(100).translationY(100);
public static void setAlpha(View view, float alpha) {
if (NEEDS_PROXY) {
wrap(view).setAlpha(alpha);
} else {
Honeycomb.setAlpha(view, alpha);
}
}public static final boolean NEEDS_PROXY = Integer.valueOf(Build.VERSION.SDK).intValue() < Build.VERSION_CODES.HONEYCOMB;判断当前sdk版本是否是API11以下,如果是的话那么NEEDS_PROXY为true。这时候会进入if分支,调用AnimatorProxy类中的wrap方法,将view进行包装,然后调用setAlpha方法设置透明度:
public void setAlpha(float alpha) {
if (mAlpha != alpha) {
mAlpha = alpha;
View view = mView.get();
if (view != null) {
view.invalidate();
}
}
}可以看到这个方法会设置mAlpha变量,并调用invalidate方法刷新视图,刷新过程中,会调用applyTransformation方法更新透明度。@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
//setAlpha方法会导致view.invalidate刷新,而在刷新过程中,会回调此方法设置透明度
View view = mView.get();
if (view != null) {
t.setAlpha(mAlpha);
transformMatrix(t.getMatrix(), view);
}
}之所以这样做,是因为低版本没有提供setAlpha方法。如果sdk版本大于11,那么进入else分支,调用HoneyComb.setAlpha: static void setAlpha(View view, float alpha) {
view.setAlpha(alpha);
}
标签:
原文地址:http://blog.csdn.net/chdjj/article/details/42293599