开发者

Bitmap image displaying VERY top left corner, not scaling

开发者 https://www.devze.com 2023-02-12 00:43 出处:网络
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 开发者_StackOverflow中文版the the screen height and

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 开发者_StackOverflow中文版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:

Bitmap image displaying VERY top left corner, not scaling

And here is the Canvas image:

Bitmap image displaying VERY top left corner, not scaling


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消