之前项目一直要个图片翻转的动画,由于本人也是个初学者,查了很多资料最后还是零零碎碎的写出来了。第一申请想记录下来!
两个图片翻转可以在一个FrmeLayout写两个ImageView,然后根据时间翻转不同的图片。
图片翻转动画关键代码:
class MyAnimation extends Animation { int centerX, centerY; Camera camera = new Camera(); @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); //获取中心点坐标 centerX = width / 2; centerY = height / 2; //动画执行时间 自行定义 setDuration(1500); setInterpolator(new DecelerateInterpolator()); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final Matrix matrix = t.getMatrix(); camera.save(); //中心是绕Y轴旋转 这里可以自行设置X轴 Y轴 Z轴 camera.rotateX(180 * interpolatedTime); //把我们的摄像头加在变换矩阵上 camera.getMatrix(matrix); //设置翻转中心点 matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); camera.restore(); } } 后面我也根据这个提取出了图片翻转的代码: private Bitmap rota(int resurce) { Matrix matrix = new Matrix(); Bitmap bmp = BitmapFactory.decodeResource(getResources(), resurce); Camera camera = new Camera(); camera.save(); camera.rotateX(180f); camera.getMatrix(matrix); camera.restore(); Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); return bitmap; } 设置图片翻转动画后,加入AnimationSet中,记得要写上AnimationSet的fillAfter方法。