2013-03-29 47 views
1

我想旋轉一個ImageView圖像。這裏是我的代碼:ImageView旋轉而不重新創建位圖

Matrix mat = new Matrix(); 
mat.preRotate(90, ivImage.getWidth()/2, ivImage.getHeight()/2); 
ivImage.setScaleType(ScaleType.MATRIX); 
ivImage.setImageMatrix(mat); 

,但是當我點擊旋轉按鈕,不僅圖像旋轉,但它是按比例也是如此,因爲位圖是較大的,則ImageView

我知道我可以使用

b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), mat, true); 
ivImage.setImageBitmap(b); 

但這明顯變慢,並且具有一定的滯後性。

所以我的問題是:如何旋轉圖像而不被縮放和重新創建位圖?

回答

0

我發現,我的目的是使用Canvas代替ImageView的最佳解決方案。而對於圖像的旋轉我選擇使用,可以發現here

0

如何使用XML動畫?

<?xml version="1.0" encoding="utf-8"?> 
    <rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="1000" 
    android:startOffset="0" 
    android:repeatCount="infinite" 
    /> 

然後使用它與類似的東西;

rotation = AnimationUtils.loadAnimation(this, R.anim.clockwise_rotation); 
buttonRefresh.startAnimation(rotation); 

ps。這無限旋轉。所以需要一些調整。

3

這裏是我用於旋轉圖像到90度的代碼。

  if(oldAngle == 0){ 
       newAngle = oldAngle + 90; 
      } 
      else if(oldAngle == 90) 
       newAngle = oldAngle - 90; 
       LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams(); 
       int centerX = layoutParams.leftMargin + (view.getWidth()/2); 
       int centerY = layoutParams.topMargin + (view.getHeight()/2); 
       RotateAnimation animation = new RotateAnimation(oldAngle, newAngle, centerX, centerY); 
       animation.setDuration(0); 
       animation.setRepeatCount(2); 
       animation.setFillAfter(true); 
       view.startAnimation(animation); 

       oldAngle = newAngle; 

希望它會幫助你...

+0

這太好了,但是當圖像旋轉到90度時,圖像被切割,整個圖像不可見,它將超出圖像視圖的邊界。任何解決方案? – nikmin

+0

@nikmin你可以請附上該圖像的屏幕截圖 – kamil

+0

我認爲當視圖旋轉時,您的linearlayout寬度或高度不可調整。 – kamil

0

使用本

final Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     final Matrix mtx = new Matrix(); 
     mtx.postRotate(n); 
     rotator = Bitmap.createBitmap(all, 0, 0, 
       all.getWidth(), all.getHeight(), mtx, 
       true); 
     view.setImageBitmap(rotator); 

這會隨機旋轉圖像。只是改變.nextInt(n).nextInt(AnyNumberYouLife) 其快速:)

0

這裏是爲把一個旋轉繪製一個ImageView的一個很好的解決方案的另一個庫:

Drawable getRotateDrawable(final Bitmap b, final float angle) { 
    final BitmapDrawable drawable = new BitmapDrawable(getResources(), b) { 
     @Override 
     public void draw(final Canvas canvas) { 
      canvas.save(); 
      canvas.rotate(angle, b.getWidth()/2, b.getHeight()/2); 
      super.draw(canvas); 
      canvas.restore(); 
     } 
    }; 
    return drawable; 
} 

用法:

Bitmap b=... 
float angle=... 
final Drawable rotatedDrawable = getRotateDrawable(b,angle); 
root.setImageDrawable(rotatedDrawable); 

另一種選擇:

private Drawable getRotateDrawable(final Drawable d, final float angle) { 
    final Drawable[] arD = { d }; 
    return new LayerDrawable(arD) { 
     @Override 
     public void draw(final Canvas canvas) { 
      canvas.save(); 
      canvas.rotate(angle, d.getBounds().width()/2, d.getBounds().height()/2); 
      super.draw(canvas); 
      canvas.restore(); 
     } 
    }; 
} 

另外,如果你想旋轉TE上的位圖,就怕OOM的,你可以使用NDK解決方案,我已經取得了here

0

我發現一個旋轉的ImageView的最簡單的方法是做到這一點:

view.animate().rotationBy(degrees); 

很容易實施和安卓處理所有其他

希望這會有所幫助!:)