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 8451037
In Process

The Archive Base Latest Questions

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

Simple question where the simple answer isn’t working. I have a bitmap and I

  • 0

Simple question where the simple answer isn’t working. I have a bitmap and I want to get its dimensions as scaled to the display by the system for different DPI screens within the onDraw() method. bitmap.width() returns its unscaled width successfully. However, bitmap.getScaledWidth() returns zero. I have tried getScaledWidth(canvas) and getScaledWidth(canvas.getDensity()), but both return 0. Indeed, canvas.getDensity() returns zero so I can’t even calculate it manually.

What am I doing wrong?

Bit more detail. I’m using a custom view. The bitmap is declared in the class and loaded in the constructor.

Edit:

I’ve found that using:

        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);

bitmap.getScaledHeight(metrics) returns the same value as bitmap.getHeight().

It would appear that bitmap.getWidth() returns the dimension of the resident bitmap after it has been scaled, meaning there’s no apparent way to get the bitmap’s original width.

  • 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:07:49+00:00Added an answer on June 10, 2026 at 11:07 am

    The Canvas class has a lots of overloads for the drawBitmap() function. One of them allows you to scale/cut a Bitmap through a pretty comfortable interface.

    public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)

    Where

    • Bitmap bitmap – is your bitmap you want to draw
    • Rect src – the source rect from your bitmap. If its not null, it will
      cut out a piece from your bitmap (in the size and position of src)
    • RectF dst – This Rect will represent the Rectangle, your Bitmap will
      fit in.
    • Paint paint – optional paint

    And now an example! Lets say, you want to shrink your Bitmaps width to 1/2 and increase its height to 2 times of the original:

    float startX = 0; //the left
    float startY = 0; //and top corner (place it wherever you want)
    float endX = startX + bitmap.getWidth() * 0.5f; //right
    float endY = startY + bitmap.getHeight() * 2.0f; //and bottom corner
    
    canvas.drawBitmap(bitmap, null, new RectF(startX, startY, endX, endY), null);
    

    UPDATE

    I don’t really understand, what you are trying to acomplish, after reading your comment, but here is some extra info to get started:

    Get the original size of a Bitmap without loading it into the memory:

    BitmapFactory.Options options = new BitmapFactory.Options();
    bitmapOptions.inJustDecodeBounds = true; // bitmap wont be loaded into the memory
    
    //won't load the Bitmap, but the options will contain the required information.
    BitmapFactory.decodeStream(inputStream, null, options);
    /*or*/ BitmapFactory.decodeFile(pathName, options);
    
    int originalWidth = bitmapOptions.outWidth;
    int originalHeight = bitmapOptions.outHeight;
    

    Now if you have another your actual (scaled) Bitmap, or an ImageView, what you want to compare to the original, then you can use this (to get the width and height use getWidth() and getHeight()):

    /*Get these values*/
    int originalWidth, originalHeight, scaledWidth, scaledHeight; 
    
    float scaleWidthRatio = (float)scaledWidth / originalWidth;
    float scaleHeightRatio = (float)scaledHeight / originalHeight;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question whose answer seems not to be in the official
I have a simple question and answer section on my website that allows comments.
Simple question which I can't seem to find an answer of: I have two
I have a quite simple question but the answer is not so easy. I
Unfortunately, the answer to this question isn't quite as simple as it sounds... but
There is probably a simple answer to this question but I do not have
I have a really simple question here, but I just can't find an answer
I have a simple question, which should hopefully have a quick answer. The code
This should be a fairly simple question to answer, but I am interested to
Simple question but cannot find answer anywhere. When using a MS Chart Control of

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.