android 动画深入分析(一)《android开发艺术与探索》
16lz
2021-01-23
android的动画可以分成三种:View动画 帧动画 属性动画
View 动画:
它支持4中效果:平移 缩放 旋转 透明度
对应的Animation 的4个子类:TranslateAnimation ScaleAnimation RotateAnimation AlphaAnimation
也可以用XML格式的来定义: 举个例子:
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
属性:
<?xml version="1.0" encoding="utf-8"?>set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:shareInterpolator="true" >
标签:
android:interpolator 动画集合所采用的插值器,插值器影响动画的速度 ,可以不指定,默认@android:anim/accelerate_decelerate_interpolator
android:shareInterpolator动画是否和集合共享一个插值器。
透明度动画
android:fromAlpha 表示透明度的起始值(0-1)
android:toAlpha 表示透明度的结束值(0-1)
缩放动画
android:fromXScale x的起始值(标准为1)
android:toXScale x的结束值
android:fromYScale y的起始值
android:toYScale y的结束值
android:pivotX 缩放的轴点x坐标
android:pivotY 缩放的轴点y坐标
平移动画
android:fromXDelta x的起始值
android:toXDelta x的结束值
android:fromYDelta y的起始值
android:toYDelta y的结束值
旋转动画
android:fromDegrees 旋转的开始角度
android:toDegrees 旋转的结束角度
android:pivotX 旋转的轴点x坐标
android:pivotY 旋转的轴点y坐标
还有一些共有的
android:duration 动画持续的时长
android:fillAfter 动画结束后View是否停留在结束位置
android:fillBefore 动画结束后View是否停留在开始位置
使用xml动画:
Animation anim= AnimationUtils.loadAnimation(this, R.anim.animation_left_in);btn_anim.startAnimation(anim);
也可以代码应用动画:
AlphaAnimation alpha = new AlphaAnimation(0,1);alpha.setDuration(300);btn_anim.startAnimation(alpha);
也可以加监听
anim.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {//动画开始回调}@Overridepublic void onAnimationRepeat(Animation animation) {//动画重复执行 回调}@Overridepublic void onAnimationEnd(Animation animation) {//动画结束回调}});
自定义View动画:
自定义动画要继承Animation 这里只给Rotate3dAnimation例子
import android.view.animation.Animation; import android.view.animation.Transformation; import android.graphics.Camera; import android.graphics.Matrix; public class Rotate3dAnimation extends Animation { private final float mFromDegrees; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private final float mDepthZ; private final boolean mReverse; private Camera mCamera; /** * Creates a new 3D rotation on the Y axis. The rotation is defined by its * start angle and its end angle. Both angles are in degrees. The rotation * is performed around a center point on the 2D space, definied by a pair of * X and Y coordinates, called centerX and centerY. When the animation * starts, a translation on the Z axis (depth) is performed. The length of * the translation can be specified, as well as whether the translation * should be reversed in time. * * @param fromDegrees * the start angle of the 3D rotation * @param toDegrees * the end angle of the 3D rotation * @param centerX * the X center of the 3D rotation * @param centerY * the Y center of the 3D rotation * @param reverse * true if the translation should be reversed, false otherwise */ public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ; mReverse = reverse; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = mFromDegrees; float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix matrix = t.getMatrix(); // 将当前的摄像头位置保存下来,以便变换进行完成后恢复成原位, camera.save(); // camera.translate,这个方法接受3个参数,分别是x,y,z三个轴的偏移量,我们这里只将z轴进行了偏移, if (mReverse) { // z的偏移会越来越大。这就会形成这样一个效果,view从近到远 camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); } else { // z的偏移会越来越小。这就会形成这样一个效果,我们的View从一个很远的地方向我们移过来,越来越近,最终移到了我们的窗口上面~ camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); } // 是给我们的View加上旋转效果,在移动的过程中,视图还会移Y轴为中心进行旋转。 camera.rotateY(degrees); // 是给我们的View加上旋转效果,在移动的过程中,视图还会移X轴为中心进行旋转。 // camera.rotateX(degrees); // 这个是将我们刚才定义的一系列变换应用到变换矩阵上面,调用完这句之后,我们就可以将camera的位置恢复了,以便下一次再使用。 camera.getMatrix(matrix); // camera位置恢复 camera.restore(); // 以View的中心点为旋转中心,如果不加这两句,就是以(0,0)点为旋转中心 matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
帧动画:
AnimationDrawable
<?xml version="1.0" encoding="utf-8"?>
播放动画
btn_frame.setBackgroundResource(R.anim.loading);AnimationDrawable drawable = (AnimationDrawable) btn_frame.getBackground();drawable.start();
View动画的特殊使用场景:
1 ViewGroup上控制子元素的动画效果
LayoutAnimation
android:delay 子类动画时间间隔 (延迟) 70% 也可以是一个浮点数 如“1.2”
android:animationOrder 的取值有
normal 0 默认
reverse 1 倒序
random 2 随机
android:animation="@anim/animation_left_in" 表示孩子显示时的具体动画是什么
animation_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
ViewGroup
也可以用代码LayoutAnimationController实现:
Animation animation =AnimationUtils.loadAnimation(this, R.anim.animation_left_in);LayoutAnimationController controller = new LayoutAnimationController(animation);controller.setDelay(0.5f);controller.setOrder(LayoutAnimationController.ORDER_NORMAL);listview.setLayoutAnimation(controller);
2.Acivity的切换效果:
Intent intent = new Intent(MainActivity.this,AnimActivity.class);startActivity(intent);overridePendingTransition(R.anim.animation_left_in, R.anim.animation_left_out);@Overridepublic void finish() {super.finish();overridePendingTransition(R.anim.animation_left_in, R.anim.animation_left_out);}
animation_left_in.xml <?xml version="1.0" encoding="utf-8"?>
animation_left_out.xml <?xml version="1.0" encoding="utf-8"?>
更多相关文章
- android视图切换动画:ViewAnimator类及其子类
- android 去除ScrollVIew拉到尽头时再拉的阴影效果和个别机型的阻
- Android 实现 按钮从两边移到中间动画效果
- Android VR效果GoogleVR
- Android之一种很有趣的界面跳动提示动画
- Android textView点击滚动(跑马灯)效果
- android TextView的跑马灯效果的实现
- android 动画之水波纹效果ripple
- Andorid旋转动画