如何在位图变量中获取实际大小的位图,而不管我使用的是什么设备

How to get Bitmap with actual size of it, in Bitmap variable irrespective of deivce I am using

我的可绘制文件夹中有一张大小为 1080*1080 的 jpg 图像,当我将其存储在位图变量中时,位图的大小并未显示为 1080*1080,而是基于我的设备显示正在使用,我想在 canvas 上绘制大小为 1080*1080 的实际图像,知道如何获得实际大小

的位图吗
    Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.template_image);
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

变量w和h不显示1080*1080,在不同设备上不一样

将您的图像文件放入 drawable-nodpi 此文件夹告诉 android 避免在任何像素密度上缩放图像文件:

更多信息Medium Article

如果您只需要检索图像大小而不显示内部图像视图:

BitmapFactory.Options options = new BitmapFactory.Options();
//Just Retreive info about the bitmap without loading inside memory to avoid OutOfMemory.
options.inJustDecodeBounds(true)
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.template_image, options);
int w = options.outWidth;
int h = options.outHeight;