位图图像显示非常左上角,未缩放

发布于 2024-10-18 05:07:16 字数 1398 浏览 2 评论 0原文

我正在尝试在 Canvas< 上显示图像/a>,但是当我调用 drawBitmap 方法时,我只得到左上角,图像不会缩放到屏幕高度和宽度。以下是涉及自定义视图的代码:

    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 是传递给我的 Activity 的 Uri 对象,并且它NOT null!这是实际图像:
实际图像
这是 Canvas 图像:
画布图像

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-10-25 05:07:16

但是当我调用drawBitmap方法时,我只得到左上角,图像不会缩放到屏幕的高度和宽度。

您告诉您的程序这样做:canvas.drawBitmap(image, 0, 0, Paint);
因为(0,0)是左上角。

如果你需要较小的图像,你应该缩小它,一种可能性是
小位图 = Bitmap.createScaledBitmap(image, width, height, false);

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

无法言说的痛 2024-10-25 05:07:16

使用 Canvas.drawBitmap() 版本来执行所需的缩放,例如 这个

顺便说一句,解压缩后的位图显然比您需要的大得多,并且它将占用大量的 RAM。我建议使用 BitmapFactory.Options.inSampleSize 来减少它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文