首页 技术 正文
技术 2022年11月18日
0 收藏 732 点赞 4,661 浏览 2037 个字

android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransformation方法进行相应的轨阵变化即可!

直接上demo!

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;

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();
}

// 生成Transformation
@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();
if (mReverse) {
//camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
camera.rotateY(mCenterY);
} else {
//camera.translate(0.0f, 0.0f, mDepthZ * (1.0f – interpolatedTime));
camera.rotateY(-mCenterY);
}
camera.rotateY(degrees);
// 取得变换后的矩阵
camera.getMatrix(matrix);
camera.restore();

matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

这是一个模仿3D旋转动画的效果,下面是调用方法,mSecondMenuLayout指的是要开始动画的view

/**
*
* @param start 开始角度
* @param end 结束角度
*/
private void applyRotation(float start, float end) {

// 计算中心点
final float centerX = mSecondMenuLayout.getWidth() / 2.0f;
//final float centerY = mSecondMenuLayout.getHeight() /2.0f;
final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
centerX, 360, 310.0f, true);
//设置动画的持续时间
rotation.setDuration(1000);
//设置动画完成后是否保留原样
rotation.setFillAfter(false);
//设置动画插补器
rotation.setInterpolator(new AccelerateDecelerateInterpolator());
//设置动画监听
rotation.setAnimationListener(mAnimListener);
mSecondMenuLayout.startAnimation(rotation);
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,909
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,434
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,249
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,060
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,692
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,730