mScrollX:View左边缘和View内容左边缘在水平方向上的距离。
mScrollY.View上边缘和View内容上边缘在竖直方向上的距离。
scrollTo和scrollBy只能改变View内容的位置而不能改变View在布局中的位置。
单位:像素。
使用动画来移动View,主要是操作View的translationX和translationY属性。
补间动画,给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变,只能应用于View对象,而且只支持支持旋转、渐变、移动、缩放属性,而不支持背景颜色的改变。而且只是改变了View对象绘制的位置,而没有改变View对象本身.
View动画并不能真正改变View的位置,变换后的View并不会响应点击事件。属性动画可以解决上述问题。但属性动画从Android3.0后才能使用,无法兼容到Android2.2.
Tween Animation实现的效果
1. Alpha :淡入淡出效果 2. Scale :缩放效果 3. Rotate :旋转效果 4. Translate :移动 效果Tween Animation的通用方法
1. setDuration (long durationMills ) 动画持续时间(单位:毫秒) 2. setFillAfter (Boolean fillAfter ) 如果 fillAfter 的值为 true, 则动画执行后,控件将停留在执行结束的状态 3. setFillBefore (Boolean fillBefore ) 如果 fillBefore 的值为 true ,则动画执行后,控件将回到动画执行之前的状态 4. setStartOffSet (long startOffSet ) 设置动画执行之前的等待时间 5. setRepeatCount ( int repeatCount ) 设置动画重复执行的 次数Animation的四个子类:AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
ObjectAnimator.ofFloat(targetView, "translationX", 0 , 100).setDuration(100).start();
改变布局参数
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mButtonToShow.getLayoutParams(); params.width += 100; params.leftMargin += 100; mButtonToShow.requestLayout(); //mButtonToShow.setLayoutParams(params);属性动画实现View弹性滑动
ObjectAnimator.ofFloat(targetView, "translationX", 0, 100).setDuration(100),start();
另一种形式
final int startX = 0;
final int deltaX = 0;
ValueAnimator animator = ValueAnimator.ofInt(0,1).setDuration(1000);
animator.addUpdateListener(new AnimatorUpdateListener(){
@Override
public void onAnimatorUpdate(ValueAnimator animator){
float fraction = animator.getAnimatedFraction();
mButton1.scrollTo(startX + (int) (deltaX * fraction) , 0);//此处可以实现以时间为参数进行变化的复杂动画。
}
});
使用Handler或View的postDelayed方法,也可以使用线程的sleep方法,发送一系列延时消息从而达到一种渐近式的效果。