Mohit Deshpande
Mohit Deshpande

Reputation: 55247

Bitmap image displaying VERY top left corner, not scaling

I am trying to display an image on a Canvas, but when I call the drawBitmap method, I get only the VERY top left corner, the image doesn't scale to the the screen height and width. Here is my code that involves the custom view:

    private class ImageOverlay extends View {

    public ImageOverlay(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Bitmap image = BitmapFactory.decodeFile(getPath(baseImage));
        Paint paint = new Paint();
        paint.setAlpha(200);

        canvas.drawBitmap(image, 0, 0, paint);
        Log.v(TAG, "Drew picture");

        super.onDraw(canvas);           
    }

    private String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }

baseImage is a Uri object that is passed to my activity and it is NOT null! Here is the actual image:
Actual image
And here is the Canvas image:
The canvas image

Upvotes: 1

Views: 1244

Answers (2)

Maaalte
Maaalte

Reputation: 6081

but when I call the drawBitmap method, I get only the VERY top left corner, the image doesn't scale to the the screen height and width.

You're telling your programm to do so: canvas.drawBitmap(image, 0, 0, paint);
As (0,0) is the top left corner.

If you need a smaller image, you should downscale it, an possibility would be Bitmap small = Bitmap.createScaledBitmap(image, width, height, false);

Upvotes: 2

Reuben Scratton
Reuben Scratton

Reputation: 38727

Use a version of Canvas.drawBitmap() which performs the scaling you want, such as this one.

Btw, your decompressed Bitmap is obviously much larger than you need it to be and it will be taking up a prodigious amount of RAM. I recommend using BitmapFactory.Options.inSampleSize to reduce it.

Upvotes: 1

Related Questions