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

  • SEARCH
  • Home
  • 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 7398935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:56:16+00:00 2026-05-29T03:56:16+00:00

I have 5 variables. The bitmap width and height. The screen width and height.

  • 0

I have 5 variables.

The bitmap width and height.
The screen width and height.
The desired percentage

How do I make the bitmap a desired percentage of the screen using a matrix (I.e 1.0 normal, 0.5 half size, 2.0 double size).

The bitmaps are sprites that are different frame widths (I.E explosion is 200×4000 (20 frames of 200×200 while the object is 65×195 so 65×65 per object if that helps).

My aim is to pass something into ResizeBitmap like (Bitmap, 100) which would be 100% of the screen while keeping the aspect ratio.

So ResizeBitmap(Bitmap, 10) would make it 10% of the screen.

This is what I’ve got so far, it crashes show’s the wrong sizes.

public Bitmap ResizeBitmap(Bitmap bitmap, int screen_percentage)
{   
    float scale;

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    float percentage_of_screen;
    float percentage_of_percentage_screen;

    if(screen_width > screen_height)
    {
        percentage_of_screen = (screen_width / 100) * width;
        percentage_of_percentage_screen = (screen_width / 100) * screen_percentage;
        scale = percentage_of_screen / percentage_of_percentage_screen;
    }

    else
    {
        percentage_of_screen = (screen_height / 100) * height;
        percentage_of_percentage_screen = (screen_height / 100) * screen_percentage;
        scale = percentage_of_screen / percentage_of_percentage_screen;
    }

    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();

    // RESIZE THE BIT MAP
    matrix.postScale(scale, scale);

    // RECREATE THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);

    return resizedBitmap;
}

Can anyone fix my code or at least help me because I’ve wasted so much time on this function.

Thanks.

  • 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-05-29T03:56:17+00:00Added an answer on May 29, 2026 at 3:56 am

    Tested:

    public class StackOverflowActivity extends Activity{
    
        private int screen_width;
        private int screen_height;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // in main.xml use an ImageView with android:id="@+id/img"
            ImageView view = (ImageView)findViewById(R.id.img);
    
            // in your drawables folder, put an image named circle.png
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.circle);
    
            Display display = getWindowManager().getDefaultDisplay(); 
            screen_width = display.getWidth();
            screen_height = display.getHeight();
    
            Bitmap scale = resizeBitmap(bmp, 100);
    
            view.setImageBitmap(scale);
    
        }
    
        public Bitmap resizeBitmap(Bitmap bitmap, int screen_percentage)
        {
                ////////////// IMPORTANT ////////////////////
                // Use '/ 100.0f' otherwise your percentage will be 0..
                // this will later on, cause the newWidth & newHeight to be 0 as well
                // which causes the app to crash
            float percentage = screen_percentage / 100.0f;
    
            float scale = screen_width / 100 * percentage;
            if(screen_width < screen_height)
            {
                scale = screen_height / 100 * percentage;
            }
    
            int newWidth = (int) (bitmap.getWidth() * scale);
            int newHeight = (int) (bitmap.getHeight() * scale);
    
                if(newWidth <= 0 || newHeight <= 0)
                { // Extra check, for invalid width/height
                    Log.e("test", "invalid dimension ("+newWidth+"x"+newHeight+")");
                    return bitmap;
                }
            return Bitmap.createScaledBitmap (bitmap, newWidth, newHeight, true);
        }
    }
    

    To avoid scaling ratio problems use this in your main.xml

    <ImageView
        android:id="@+id/img"
        android:background="@null"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_gravity="center" 
        android:scaleType="centerInside"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of bitmap images that I need to save using .NET
I have variables with values like 1.7m 1.8k and 1.2b how can I convert
I have 3 variables like this: $first = 2; $second = 5; $operation =
I have two variables, key and value , and I want to add them
i have following variables in jquery var uemail=abc@domain.com,xyz@domain.com,gty@domain.com; var subj=test message; var text=<b>This is
I have some variables inside a template and I don't know where I assigned
I have two variables: char charTime[] = TIME; char buf[] = SOMETHINGELSE; I want
If I have two variables: Object obj; String methodName = getName; Without knowing the
If I have two variables containing binary values, how do I append them together
Basically I have some variables that I don't want to preinitialize: originalTime = None

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.