2012-01-26 64 views
70

Có một tuyên bố trong android canvas.drawBitmap(visiblePage, 0, 0, paint);làm thế nào để xoay một bitmap 90 độ

Khi tôi thêm canvas.rotate(90), không có hiệu lực. Nhưng nếu tôi viết

canvas.rotate(90) 
canvas.drawBitmap(visiblePage, 0, 0, paint); 

Tôi không nhận được bitmap được vẽ. Vậy tôi đang làm gì không đúng?

+0

tôi đã trả lời ở đây: http://stackoverflow.com/questions/8608734/android-rotate-bitmap-9 0-degrees-results-in-squashed-image-need-a-true-rotate-b/14475148 # 14475148 – EyalBellisha

Trả lời

10

Dưới đây là đoạn code để xoay hoặc tái kích thước hình ảnh của bạn trong android

public class bitmaptest extends Activity { 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     LinearLayout linLayout = new LinearLayout(this); 

     // load the origial BitMap (500 x 500 px) 
     Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
       R.drawable.android); 

     int width = bitmapOrg.width(); 
     int height = bitmapOrg.height(); 
     int newWidth = 200; 
     int newHeight = 200; 

     // calculate the scale - in this case = 0.4f 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     // createa matrix for the manipulation 
     Matrix matrix = new Matrix(); 
     // resize the bit map 
     matrix.postScale(scaleWidth, scaleHeight); 
     // rotate the Bitmap 
     matrix.postRotate(45); 

     // recreate the new Bitmap 
     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
          width, height, matrix, true); 

     // make a Drawable from Bitmap to allow to set the BitMap 
     // to the ImageView, ImageButton or what ever 
     BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

     ImageView imageView = new ImageView(this); 

     // set the Drawable on the ImageView 
     imageView.setImageDrawable(bmd); 

     // center the Image 
     imageView.setScaleType(ScaleType.CENTER); 

     // add ImageView to the Layout 
     linLayout.addView(imageView, 
       new LinearLayout.LayoutParams(
         LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT 
       ) 
     ); 

     // set LinearLayout as ContentView 
     setContentView(linLayout); 
    } 
} 

Bạn cũng có thể kiểm tra liên kết này để biết chi tiết: http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

+0

Điều này không hoạt động nếu hình ảnh không vuông – xavimb

175

Bạn cũng có thể thử này một

Matrix matrix = new Matrix(); 

matrix.postRotate(90); 

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true); 

Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true); 

Sau đó, bạn có thể sử dụng hình ảnh được xoay để đặt trong số lần xem hình ảnh của mình thông qua

imageView.setImageBitmap(rotatedBitmap); 
+1

Tôi nghĩ cho scaledBitmap bạn muốn (bitmapOrg, width, chiều cao, đúng) – Jameo

+1

Nhập ma trận nào? android.graphics hoặc android.opengl? – Poutrathor

+2

Nhập android.graphics – kirtan403

128
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); 
} 

Để có được Bitmap từ nguồn:

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img); 
+1

thx arvis .. giải pháp tuyệt vời – Antonio

+0

Tôi mới sử dụng Android. Chỉ cần băn khoăn liệu tôi có làm bitmap newBitmap = RotateBitmap (oldBitmap, 90), bitmap đã giải mã của tôi có hai khối bộ nhớ (cũ và mới) hay chúng đang đề cập đến cùng một bộ nhớ, nhưng không có luân phiên. ? .... Quan tâm của tôi là, nếu tôi giải mã R.drawable.picture thành oldBitmap, nếu giả sử chiếm 2 MB bộ nhớ (Heap tôi đoán?), NewBitmap sẽ nhận thêm 2 MB bộ nhớ (tức là 2+ 2 = 4MB tổng cộng)?hoặc newBitmap sẽ chỉ tham chiếu đến oldBitmap (và do đó không cần thêm 2MB)? ......... Tôi muốn tránh lỗi OutOfMemory bằng mọi giá! –

+3

@ShishirGupta Không được thử nghiệm nhưng bằng tài liệu android: 'Nếu bitmap nguồn là không thay đổi và tập hợp con được yêu cầu giống với bitmap nguồn, thì bitmap nguồn được trả về và không có bitmap mới được tạo.' – Arvis

6

Theo mặc định điểm xoay là của Canvas (0,0) điểm, và tôi đoán là bạn có thể muốn xoay nó xung quanh trung tâm. tôi đã làm điều đó:

protected void renderImage(Canvas canvas) 
{ 
    Rect dest,drawRect ; 

    drawRect = new Rect(0,0, mImage.getWidth(), mImage.getHeight()); 
    dest = new Rect((int) (canvas.getWidth()/2 - mImage.getWidth() * mImageResize/2), // left 
        (int) (canvas.getHeight()/ 2 - mImage.getHeight()* mImageResize/2), // top 
        (int) (canvas.getWidth()/2 + mImage.getWidth() * mImageResize/2), //right 
        (int) (canvas.getWidth()/2 + mImage.getHeight()* mImageResize/2));// bottom 

    if(!mRotate) { 
     canvas.drawBitmap(mImage, drawRect, dest, null); 
    } else { 
     canvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated. 
     canvas.rotate(90,canvas.getWidth()/2, canvas.getHeight()/ 2); 
     canvas.drawBitmap(mImage, drawRect, dest, null); 
     canvas.restore(); 
    } 
} 
1

Nếu bạn xoay bitmap, 90 180 270 360 là ok nhưng cho độ khác canvas sẽ vẽ bitmap với kích thước khác nhau.

Vì vậy, cách tốt nhất là

canvas.rotate(degree,rotateCenterPoint.x,rotateCenterPoint.y); 
canvas.drawBitmap(...); 
canvas.rotate(-degree,rotateCenterPoint.x,rotateCenterPoint.y);//rotate back 
2

hạn ngắn cho Kotlin

fun Bitmap.rotate(degrees: Float): Bitmap { 
    val matrix = Matrix().apply { postRotate(degrees) } 
    return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true) 
} 

Và sử dụng:

val rotatedBitmap = bitmap.rotate(90) 
Các vấn đề liên quan