如何在Bitmap变量中获得实际大小的Bitmap,而不管我使用的是什么Dieivce。

问题描述 投票:0回答:1

我在我的drawable文件夹里有一张1080*1080大小的jpg图片,当我把它存储在Bitmap变量中时,位图的大小并没有显示为1080*1080,而是根据我使用的设备来显示,我想在画布上画出1080*1080大小的实际图片,有什么办法可以得到实际大小的位图吗?

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

变量 w和h 都不显示1080*1080,在不同的设备上是不同的。

android android-canvas android-drawable android-bitmap bitmapfactory
1个回答
1
投票

把你的图片文件放在 drawable-nodpi 这个文件夹告诉android避免在任何像素密度下缩放你的图像文件。

更多信息 媒介文章

如果你只需要检索图像大小,而不显示图像视图内部。

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;
© www.soinside.com 2019 - 2024. All rights reserved.