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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:56:11+00:00 2026-05-23T04:56:11+00:00

I’m trying to have an image scale to a certain size depending on the

  • 0

I’m trying to have an image scale to a certain size depending on the horizontal size sent to an update function, but the following code doesnt seem to size the image correctly.

EDIT: The code:

public class GlassesView extends View {

    private Paint paint;
    private BitmapFactory.Options options;
    private Bitmap bitmapOrg;
    private Bitmap target;
    private Bitmap bitmapRev;
    private Bitmap resizedBitmap;

    private int currY;

    public int glassesX;
    public int glassesY;
    public float glassesSizeX;
    public float glassesSizeY;
    private boolean drawGlasses;
    private boolean glassesMirrored;

    public GlassesView(Context context) {
        super(context);
        paint = new Paint();
        paint.setDither(false);
        paint.setAntiAlias(false);

        options = new BitmapFactory.Options();
        options.inDither = false;
        options.inScaled = false;

        bitmapOrg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),
                R.drawable.micro_glasses, options), 32, 5, false);
        bitmapRev = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),
                R.drawable.glasses_reverse, options), 32, 5, false);
        drawGlasses = false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(target, 0, 0, paint);

        boolean moving = currY < glassesY;
        if (moving) {
            currY++;
        }
        if (drawGlasses) {
            int newWidth = resizedBitmap.getWidth();
            int newHeight = resizedBitmap.getHeight();
            Paint bluey = new Paint();
            bluey.setColor(Color.argb(64, 0, 0, 255));
            canvas.drawRect(new Rect(glassesX, currY, glassesX + newWidth,
                    currY + newHeight), bluey);

            canvas.drawBitmap(resizedBitmap, glassesX, currY, paint);
        }
        if (moving) {
            invalidate();
        }
    }

    public void drawGlasses(int x1, int x2, int y, boolean mirror) {
        drawGlasses = true;
        glassesMirrored = mirror;
        if (!mirror) {
            glassesSizeX = (float) (x2 - x1) / (float) (25 - 16);
            glassesSizeY = glassesSizeX;
            glassesY = y - (int)(1*glassesSizeX);
            glassesX = (int) (x1 - (glassesSizeX * 16));
        } else {
            glassesSizeX = (float) (x1 - x2) / (float) (25 - 16);
            glassesSizeY = glassesSizeX;
            glassesY = y - (int)(1*glassesSizeX);
            glassesX = (int) (x1 - (glassesSizeX * 16));
        }

        currY = -1;

        if (!glassesMirrored) {
            resizedBitmap = Bitmap.createScaledBitmap(bitmapOrg,
                    (int) (bitmapOrg.getWidth() * glassesSizeX),
                    (int) (bitmapOrg.getHeight() * glassesSizeY), false);
        } else {
            resizedBitmap = Bitmap.createScaledBitmap(bitmapRev,
                    (int) (bitmapRev.getWidth() * glassesSizeX),
                    (int) (bitmapRev.getHeight() * glassesSizeY), false);
        }
    }

    public void setTargetPic(Bitmap targetPic) {
        target = targetPic;
    }

}

The result. (The blue rectangle being the bounding box of the image’s intended size)

Which part am I going wrong at?

EDIT 2:
Here are the glasses: glasses

EDIT 3:
Out of curiousity, I ran it on my actual phone, and got a much different result, the image was stretched passed the intended blue box.

EDIT 4:
I tried running the app on a few emulators to see if it was an Android version incompatibility thing, but they all seemed to work perfectly. The scaling issue only occurs on my phone (Vibrant, rooted, CM7) and my cousin’s (Droid, also rooted). These are the only physical devices I have tested on, but they both seem to have the same issue.
I’d really appreciate if someone could help me out here, this is a huge roadblock in my project and no other forums or message groups are responding.

EDIT 5:
I should mention that in update 4, the code changed a bit, which fixed the problem in the emulators as I stated, but doesn’t work on physical devices. Changes are updated in the code above. *desperate for help* 😛

EDIT 6:
Yet another update, I tested the same code on my old G1, and it works perfectly as expected. I have absolutely no clue now.

  • 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-23T04:56:13+00:00Added an answer on May 23, 2026 at 4:56 am

    Found what was wrong. The image has to be in a folder called “drawable-nodpi”, otherwise it will be scaled depending on DPI.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.