Android UI之ImageView旋转的几种方式

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

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

[java]  view plain  copy
  1. Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();  
  2. Matrix matrix  = new Matrix();  
  3. matrix.setRotate(90);  
  4. Bitmap new = Bitmap.create(bitmap,0,bitmap.getWidth(),0,bitmap.getHeight(),matrix);  
  5. image.setBitmapResource(bitmap);  

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


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

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

[java]  view plain  copy
  1. android:rotation="90"  
,这样。

动态调用如下:

[java]  view plain  copy
  1. image.setPivotX(image.getWidth()/2);  
  2. image.setPivotY(image.getHeight()/2);//支点在图片中心  
  3. image.setRotation(90);  


第三种,使用旋转动画

可以使用ImageView配合属性动画实现,如
[java]  view plain  copy
  1. rotateImage.animate().rotation(90);  
或者普通动画
[java]  view plain  copy
  1. Animation rotateAnimation  = new RotateAnimation(lastAngle, progress, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1);  
  2.                 rotateAnimation.setFillAfter(true);  
  3.                 rotateAnimation.setDuration(50);  
  4.                 rotateAnimation.setRepeatCount(0);  
  5.                 rotateAnimation.setInterpolator(new LinearInterpolator());  
  6.                 rotateImage.startAnimation(rotateAnimation);  


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

[java]  view plain  copy
  1. Matrix matrix=new Matrix();  
  2.                 rotateImage.setScaleType(ScaleType.MATRIX);   //required  
  3.                 matrix.postRotate((float) progress, pivotX, pivotY);  
  4.                 rotateImage.setImageMatrix(matrix);  

猜你喜欢

转载自blog.csdn.net/fengyenom1/article/details/80608777