Android UI之ImageView旋转的几种方式

我整理了一下,大概有四种,亲测成功三种。

第一种,效率较低,不过看许多博客都使用这种方法,即旋转bitmap:

Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
Matrix matrix  = new Matrix();
matrix.setRotate(90);
Bitmap new = Bitmap.create(bitmap,0,bitmap.getWidth(),0,bitmap.getHeight(),matrix);
image.setBitmapResource(bitmap);

如果程序不断获取新的bitmap重新设置给ImageView的话,那么bitmap在不断旋转,又不回收内存,浪费大大哒,不推荐使用。

 

第二种,使用ImageView自带的旋转方法

可以通过在xml中设置ImageView的属性来实现,如

android:rotation="90"

,这样。动态调用如下:

image.setPivotX(image.getWidth()/2);
image.setPivotY(image.getHeight()/2);//支点在图片中心
image.setRotation(90);

第三种,使用旋转动画

可以使用ImageView配合属性动画实现,如

rotateImage.animate().rotation(90);

或者普通动画

Animation rotateAnimation  = new RotateAnimation(lastAngle, progress, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setDuration(50);
                rotateAnimation.setRepeatCount(0);
                rotateAnimation.setInterpolator(new LinearInterpolator());
                rotateImage.startAnimation(rotateAnimation);

第四种,其他博客看到的,未测试!

Matrix matrix=new Matrix();
                rotateImage.setScaleType(ScaleType.MATRIX);   //required
                matrix.postRotate((float) progress, pivotX, pivotY);
                rotateImage.setImageMatrix(matrix);

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值