动画区分为补间动画(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);
-
1.1透明动画可以通过xml或者代码直接书写xml书写格式:
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="5000" > </alpha>
然后通过AnimationUtils来加载动画然后启动
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); }
2、旋转动画
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360"></rotate>
代码书写格式
此方式默认旋转中心为起始坐标 view的左上角坐标,因此旋转时围绕左上角坐标旋转。RotateAnimation rotateAnimation=new RotateAnimation(0,360); rotateAnimation.setDuration(2000);
在用代码书写旋转动画时,可以设置旋转的中心有相对自己也有相对父容器的: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
此方式后两个参数为旋转中心的坐标原点点为View左上角坐标。RotateAnimation rotateAnimation=new RotateAnimation(0,360,100,100);
3、位移动画
xml书写格式:
(以下坐标均为相对于view左上角坐标)<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="100" android:toYDelta="100"></translate>
fromXDelta为起始X坐标位置
fromYDelta为起始Y坐标位置
toXDelta为终止点X坐标位置
toYDeltr为终止点Y坐标位置
代码书写格式:
具体参数为起始X坐标, 终止X坐标; 起始Y坐标,终止Y坐标TranslateAnimation translateAnimation = new TranslateAnimation(0, 400, 0, 0); translateAnimation.setDuration(2000);
同样可以写成相对于当前View或者父容器的位置。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);
4.缩放动画
xml格式
pivotX、pivotY为旋转中心点位置。<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000" android:fromXScale="0.5" android:fromYScale="0.5" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="1.5"></scale>
fromXScale为X轴方向缩放开始比例占原View的比例。
fromYScale为Y轴方向缩放开始比例占原View的比例。
toXScale为X轴方向缩放结束比例占原View的比例。
toYScale为Y轴方向缩放结束比例占原View的比例。
代码方式:
通过查看构造我们能看到有四个构造方法,其中第一个为自定义动画,暂时不说,后面三个根据参数可以明白
第一个为以View左上角坐标开始缩放
第二个可以指定缩放中心坐标缩放
第三个可以指定相对于本View或者父容器位置进行缩放。
以左上角坐标开始缩放
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);
指定相对于本View或者父容器的缩放中心缩放
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);
如果想要几个动画结合起来使用也可以通过xml或者代码书写
xml格式
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:shareInterpolator="true"> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" /> <rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="720" /> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="100%" /> <scale android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="1.5" /> </set>
代码格式
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);
如果在整体的动画运行中同时存在旋转和位移,一般是先设置旋转 ,后设置位移,这样就不会出现动画错乱的现象。
上面就是view动画做的一些整理,方便他人和自己以后查看。