您的位置:首页 > 移动开发 > Android开发

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

2017-06-20 19:11 423 查看
转载请标明出处: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息