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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:33:01+00:00 2026-05-27T04:33:01+00:00

In my custom views OnDraw method I draw a Bitmap to the center of

  • 0

In my custom views OnDraw method I draw a Bitmap to the center of the Canvas with

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Rect r = canvas.getClipBounds();
    displayWidth = r.right;
    displayHeight = r.bottom;
    camera.applyToCanvas(canvas);
    float zW = (float)bitmapWidth / (float)displayWidth;
    float zH = (float)bitmapHeight / (float)displayHeight;
    float z = 0.0f;
    if (zW>1 || zH>1) {
        z = Math.max(zW, zH); 
    }
    canvas.drawColor(Color.DKGRAY);
    canvas.drawBitmap(bitmap, (displayWidth/2.0f - (bitmapWidth)/2.0f), (displayHeight/2.0f - bitmapHeight/2.0f), paint);
    if (z>0) {
        camera.translate(z, -z, z);
    }
}

If the Bitmap is larger in height or width is larger then Canvas size (displayWidth, displayHeight), how can I use the Camera class to autozoom to fit the Bitmap and center it to the Canvas. Any ideas?

  • 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-27T04:33:02+00:00Added an answer on May 27, 2026 at 4:33 am

    Try to create a Matrix instance and initialize it using

    public boolean setRectToRect (RectF src, RectF dst, Matrix.ScaleToFit stf)

    You need to do this only once and keep the matrix in memory. Note that Matrix.ScaleToFit define the CENTER value.

    Later when you draw the bitmap use this version of the drawBitmap:

    public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)

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

Sidebar

Related Questions

I have a custom view which I override the onDraw method to draw bitmaps
I've created a custom view class and right now just want to draw an
I have a custom View which I draw on bitmaps in the onDraw function.
I'm having an issue using the canvas.drawText() method. I have a custom view, as
I have created a custom ImageView and in its onDraw method I need to
I'm trying to draw to a Bitmap so I can put my custom view
i have prepared one custom view using onDraw method, my view class is, public
I need to draw text to a canvas (of a custom view), and need
how can i add a custom view to the right click menu of every
I created a custom View by extending from View. In onDraw() I managed to

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.