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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值