Question

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* :P

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.

Was it helpful?

Solution 3

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

OTHER TIPS

have you been using the /res/drawable-nodpi/ directory to store your images?

Apparently if you use the /drawable-ldpi/, /drawable-mdpi/ or /drawable-hdpi/ folders, Android will apply scaling to the image when you load it depending on the device. The reason your G1 may work is that it may not require any scaling depending on the folder you used.

Also, your IMGUR links are broken... also your code doesn't seem correct... you call DrawGlasses with no arguments.

Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, glassesWidth,   
                                           glassesHeight, matrix, false);

Change the last parameter from false to true. you can have a try.

The follow is i used zoom in method by scale:

 private void small()    {  
     int bmpWidth=bmp.getWidth();   
     int bmpHeight=bmp.getHeight();  

     Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);  

     /* 设置图片缩小的比例 */  
     double scale=0.8;  
     /* 计算出这次要缩小的比例 */   
     scaleWidth=(float) (scaleWidth*scale);   
     scaleHeight=(float) (scaleHeight*scale);   
     /* 产生reSize后的Bitmap对象 */  
     Matrix matrix = new Matrix();  
     matrix.postScale(scaleWidth, scaleHeight);  
     Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,   
             bmpHeight,matrix,true);  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top