动画区分为补间动画(TweenAnimation)DrawableAnimation和属性动画(Android3.0( Api 11)后新增的)
1.TweenAnimation补间动画 也称View动画,它包括透明、旋转、缩放和位移四中。
duration代表动画执行时间;pivotX、pivotY代表动画的执行中心 ,如果以自己的百分比为中心则为加% ,如果以父容器为中心则加p,如果以父容器的百分比为中心则加%p
以下是dp转像素和像素转dp
public class DensityUtils {
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
xml加载动画方式均为以下格式:
Animation mAnim= AnimationUtils.loadAnimation(this, R.anim.alpha_animation);
运行动画可以为如下两种格式:
1 .
mIvTest.setAnimation(mAlphaAnim);
mAlphaAnim.start();
2.
mIvTest.startAnimation(mAlphaAnim);
Animation mAlphaAnim = AnimationUtils.loadAnimation(this, R.anim.alpha_animation);
mIvTest.startAnimation(mAlphaAnim);
AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
alphaAnimation.setDuration(2000);
if(mIvTest!= null){
mIvTest.startAnimation(alphaAnimation);
}
RotateAnimation rotateAnimation=new RotateAnimation(0,360);
rotateAnimation.setDuration(2000);
此方式默认旋转中心为起始坐标 view的左上角坐标,因此旋转时围绕左上角坐标旋转。RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);
在用代码书写旋转动画时,可以设置旋转的中心有相对自己也有相对父容器的:Animation.RELATIVE_TO_SELF
Animation.RELATIVE_TO_PARENT
RotateAnimation rotateAnimation=new RotateAnimation(0,360,100,100);
此方式后两个参数为旋转中心的坐标原点点为View左上角坐标。
(以下坐标均为相对于view左上角坐标)TranslateAnimation translateAnimation = new TranslateAnimation(0, 400, 0, 0);
translateAnimation.setDuration(2000);
具体参数为起始X坐标, 终止X坐标; 起始Y坐标,终止Y坐标TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0.0f,//fromXtype fromXvalue
Animation.RELATIVE_TO_SELF,0.0f,//toXtype toXvalue
Animation.RELATIVE_TO_SELF,0.5f,//fromYtype fromYvalue
Animation.RELATIVE_TO_SELF,2.0f//toYtype toYvalue
);
translateAnimation.setDuration(2000);
同样可以写成相对于当前View或者父容器的位置。
pivotX、pivotY为旋转中心点位置。ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.1f, 0, 1.1f);
scaleAnimation.setDuration(2000);
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.1f, 0, 1.1f, DensityUtils.dip2px(this, 200), 0);
scaleAnimation.setDuration(2000);
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.0f, 0, 1.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f
);
scaleAnimation.setDuration(2000);
AlphaAnimation alphaAnim = new AlphaAnimation(0.1f, 1.0f);
alphaAnim.setDuration(2000);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
);
scaleAnimation.setDuration(2000);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0.0f,//fromXtype fromXvalue
Animation.RELATIVE_TO_SELF,0.0f,//toXtype toXvalue
Animation.RELATIVE_TO_SELF,0.0f,//fromYtype fromYvalue
Animation.RELATIVE_TO_SELF,1.0f//toYtype toYvalue
);
translateAnimation.setDuration(2000);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);
AnimationSet animSet = new AnimationSet(true);
animSet.setDuration(2000);
animSet.addAnimation(alphaAnim);
animSet.addAnimation(scaleAnimation);
animSet.addAnimation(rotateAnimation);
animSet.addAnimation(translateAnimation);
mIvTest.startAnimation(animSet);