Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8454243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:58:27+00:00 2026-06-10T11:58:27+00:00

I tried to rotate an image with following code, but I found the generated

  • 0

I tried to rotate an image with following code, but I found the generated image gets bigger and bigger:

Matrix matrix = new Matrix();
matrix.setRotate(10);
Bitmap newImage = Bitmap.createBitmap(image, 0, 0, 
        image.getWidth(), image.getHeight(), matrix, true);

You can see the pictures:

Original

enter image description here

Rotate 10 degree

enter image description here

Rotate 10 degree again

enter image description here

Rotate 10 degree again

enter image description here

The blue rectangles are full images.

You can see the images getting bigger and bigger(although the size of sofa is not changed), and the 4 corners of original image are not on the borders of new images later.

How to change the code to keep the corners on the border(just like the 2nd image)?

I forgot to say I’ve created a demo project on github. You can clone it, the main java code is here:

https://github.com/freewind/Android-RotateTest/blob/master/src/com/example/MyActivity.java

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:58:28+00:00Added an answer on June 10, 2026 at 11:58 am

    I tried your code and after some rotation it crashed with the OutOfMemory exception cause each time a new bitmap is created which is very resource intensive. You should never never! use createBitMap() in iteration. I made some modification to your image rotation code and now it’s running as expected.
    Here is the code:

    private void addListeners() {
        this.button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View view) {
    
                Matrix matrix = new Matrix();
    
                //copying the image matrix(source) to this matrix 
                matrix.set(imageView.getImageMatrix());
    
                matrix.postRotate(10, imageView.getWidth()/2, imageView.getHeight()/2);
                imageView.setImageMatrix(matrix);
    
                //checking the size of the image
                Drawable d = imageView.getDrawable();
                Bitmap bmp = ((BitmapDrawable)d).getBitmap();
                imageInfo(bmp);
            }
        });
    }
    

    also set the scale type of the imageView to matrix

    <ImageView
        android:id="@+id/Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#336699"
        android:scaleType="matrix"
        android:padding="2px"
        android:src="@drawable/m" />
    

    And if we want to get the rotated bitmap from ImageView, do this:

    private Bitmap getBitmapFromView() {
        // this is the important code :)
        // Without it the view will have a dimension of 0,0 and the bitmap will be null
        imageView.setDrawingCacheEnabled(true);
        imageView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
        imageView.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(imageView.getDrawingCache());
        imageView.setDrawingCacheEnabled(false); // clear drawing cache
        return b;
    }
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to rotate a bitmap image clockwise 90 degree. I've tried changing the
How do you rotate an image using jQuery-rotate plugin? I have tried the following
I tried to rotate the bitmap using Matrix , its working fine if the
I am loading an image from gallery through bitmap api but the orientation of
rotate image on canvas around its center . i tried some example from net
i'm still working on a piece of code, i want to rotate the image
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried following the instructions here: How to use Google app engine with my own
Tried a bunch of things but I can't get it to work consistently amid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.