2016-01-15 9 views
2

我想旋轉圖像使用滑翔庫。以前,能夠做畢加索(由於一個問題,我移動滑翔)。現在我缺少滑動旋轉功能。我嘗試使用轉換,但沒有奏效。如何使用滑行庫來旋轉圖像? (像在畢加索)

//代碼使用

public class MyTransformation extends BitmapTransformation { 

private float rotate = 0f; 

public MyTransformation(Context context, float rotate) { 
    super(context); 
    this.rotate = rotate; 
} 

@Override 
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, 
          int outWidth, int outHeight) { 
    return rotateBitmap(toTransform, rotate); 
} 

@Override 
public String getId() { 
    return "com.example.helpers.MyTransformation"; 
} 

public static Bitmap rotateBitmap(Bitmap source, float angle) 
{ 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 
} 
} 

//下滑

Glide.with(context) 
       .load(link) 
       .asBitmap() 
       .transform(new MyTransformation(context, 90)) 
       .into(imageView); 

在此先感謝。

回答

8

也許你已經找到了解決方案,如果沒有,也許這可以幫助你。我使用這段代碼來旋轉我從相機獲得的圖像。

public MyTransformation(Context context, int orientation) { 
    super(context); 
    mOrientation = orientation; 
} 

@Override 
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
    int exifOrientationDegrees = getExifOrientationDegrees(mOrientation); 
    return TransformationUtils.rotateImageExif(toTransform, pool, exifOrientationDegrees); 
} 

private int getExifOrientationDegrees(int orientation) { 
    int exifInt; 
    switch (orientation) { 
     case 90: 
      exifInt = ExifInterface.ORIENTATION_ROTATE_90; 
      break; 
//more cases 
     default: 
      exifInt = ExifInterface.ORIENTATION_NORMAL; 
      break; 
    } 
    return exifInt; 
} 

以及如何使用它:

Glide.with(mContext) 
      .load(//your url) 
      .asBitmap() 
      .centerCrop() 
      .transform(new MyTransformation(mContext, 90)) 
      .diskCacheStrategy(DiskCacheStrategy.RESULT) 
      .into(//your view); 

更多的情況下,或Exif int類型,檢查android.media.ExifInterface公共常量

+1

謝謝。我會測試它。 –