先上效果图:

我这里用的是GifCam来制作的gif动画,能够在http://download.csdn.net/detail/baidu_nod/7628461下载,

制作过程是先起一个模拟器,然后把GifCam的框拖到模拟器上面。点击Rec的new先,然后点击Rec,然后就save到本地成gif文件

这里做一个左右旋转。上下旋转,和左右移动的动画。先自己建立一个View的类,作为操作的对象:

public class MyView extends View {

	private Paint mPaint;
int width = 0;
int height = 0; public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.RED);
this.setBackgroundColor(Color.RED);
width = context.getResources().getDimensionPixelSize(R.dimen.width);
height = context.getResources().getDimensionPixelSize(R.dimen.height); } @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//width 300 height 300
canvas.drawLine(0, 0, width, 0, mPaint);
canvas.drawLine(width, 0, width, height, mPaint);
canvas.drawLine(width, height, 0, height, mPaint);
canvas.drawLine(0, height, 0, 0, mPaint);
canvas.save();
} }

左右旋转动画:

public class RotateLeftRightAnimation 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; private InterpolatedTimeListener listener; public RotateLeftRightAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ,
boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
} public static interface InterpolatedTimeListener {
public void interpolatedTime(float interpolatedTime);
} public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
this.listener = listener;
} @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) {
if (listener != null) {
listener.interpolatedTime(interpolatedTime);
}
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); boolean overHalf = (interpolatedTime > 0.5f);
if (overHalf) {
degrees = degrees - 180;
} 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);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
<span style="color:#ff0000;">camera.rotateY(degrees); //这个Y轴旋转就是左右旋转</span>
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);//这两句的意思是把View移到原点后旋转完再移动到如今的位置
}
}

假设是上线旋转就把camera.rotateY(degrees)改成camera.rotateX(degrees)

假设是移动的话

<span style="color:#330033;">public class MoveAnimation extends Animation {
private Camera mCamera;
private float mMoveDistance; private InterpolatedTimeListener listener; public MoveAnimation(float moveDistance) {
mMoveDistance = moveDistance;
} public static interface InterpolatedTimeListener {
public void interpolatedTime(float interpolatedTime);
} public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
this.listener = listener;
} @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) {
if (listener != null) {
listener.interpolatedTime(interpolatedTime);
} final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save(); camera.getMatrix(matrix);
camera.restore();
matrix.postTranslate(mMoveDistance, 0);
}
}</span>

然后主程序这样来调用:

	final MyView myView = (MyView) findViewById(R.id.myview);

		Button btn = (Button) findViewById(R.id.btn_move);
btn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
MoveAnimation anim = new MoveAnimation(200);
anim.setDuration(500);
myView.startAnimation(anim);
}
}); Button btn_up_down_rotate = (Button) findViewById(R.id.btn_up_down_rotate);
btn_up_down_rotate.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
RotateUpDownAnimation anim = new RotateUpDownAnimation(0,
180, v.getWidth() / 2, v.getHeight() / 2, 0, false);
anim.setDuration(500);
myView.startAnimation(anim);
}
}); Button btn_left_right_rotate = (Button) findViewById(R.id.btn_left_right_rotate);
btn_left_right_rotate.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
RotateLeftRightAnimation anim = new RotateLeftRightAnimation(0,
180, v.getWidth() / 2, v.getHeight() / 2, 0, false);
anim.setDuration(500);
myView.startAnimation(anim);
}
});

最新文章

  1. [注意]SerialPort操作PCI-1621D多串口卡,出现异常&quot;参数不正确&quot;
  2. 【原创】Kakfa utils源代码分析(三)
  3. win 7 下Maven环境的搭建
  4. [Bootstrap]组件(二)
  5. NodeJS:树的反序列化
  6. Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性。
  7. 将ImageView中的图片保存到本地相冊
  8. Linux 编程学习笔记----动笔makefile档
  9. Linux格式化硬盘 常用命令小记
  10. 阿里巴巴的开源项目Druid(关于数据库连接)
  11. POJ_3342_Party at Hali-Bula_树形DP
  12. nuxt npm run dev 报错Solution to the &quot;Error: listen EADDRINUSE 127.0.0.1:8080&quot;
  13. NPOI设置单元格背景色
  14. oath2
  15. Comparison of several types of convergence
  16. WPF模拟键盘输入和删除
  17. SSM框架整合思想
  18. 高效的 JavaScript
  19. FTP 错误1
  20. collection my favoriate websites

热门文章

  1. Linux - 文件系统结构
  2. 【ASP.NET Web API教程】2.3.6 创建产品和订单控制器
  3. 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API
  4. char *和char[]的区别,困扰很长时间了,总结下
  5. H2内存数据库 支持存储到文件
  6. Android调用系统相机、自己定义相机、处理大图片
  7. Hacker News网站的文章排名算法工作原理
  8. openCV中cvSnakeImage()函数代码分析
  9. 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.h
  10. sofa-pbrpc 1.1.1 发布,RPC 网络通信库