> 文档中心 > android 图片旋转实现的两种方法的比较

android 图片旋转实现的两种方法的比较

转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/73506752

图片旋转的方法有两种(旋转ImageView所在布局暂不考虑),这两种分别是动画和使用Matrix(齐次变换矩阵)。
我们想要达到的目标是旋转长图(非正方形),甚至长宽比例很夸张那种。想达到的效果是以图片中心为原点旋转,旋转过程中不失真,不缺失

首先先看一下动画的效果

Animation rotateAnimation = new RotateAnimation(0f, getRoll(i), Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);rotateAnimation.setFillAfter(true);rotateAnimation.setDuration(0);rotateAnimation.setRepeatCount(0);rotateAnimation.setInterpolator(new LinearInterpolator());rotateAnimation.setDetachWallpaper(true);photoView.startAnimation(rotateAnimation);

效果图:
这里写图片描述

然后我们再看一下Matrix的方法旋转

Matrix matrix = new Matrix(); //旋转图片 动作matrix.setRotate(getRoll(i));//旋转角度width = bitmap.getWidth();height = bitmap.getHeight(); // 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);BitmapDrawable bitmapDrawable = new BitmapDrawable(resizedBitmap);imageView.setImageDrawable(bitmapDrawable);

效果图:
这里写图片描述

两种方法的对比:
性能方面:动画的方式不用操作一个bitmap和生bitmap对象,性能比较好
效果:采用动画的方式长图在旋转过程中会出现缺失,而Matrix不会
总结:如果图片是正方形选择动画的实现方式,如果是长方形且不允许缺失则选择Matrix

转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/73506752

Demo下载地址:http://download.csdn.net/detail/nmyangmo/9875840