我是Android Studio的新手,在与BitmapFactory有关的任务中,我特别感到困惑。我遇到的问题如下。当我在可绘制文件夹下插入尺寸为(578x496)的jpg文件时,Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.background1));Log.e("WIDTH", String.valueOf(mBitmap.getWidth());Log.e("HEIGHT", String.valueOf(mBitmap.getHeight());我得到的是(2312x1984)的尺寸,比实际尺寸大x4。是因为我的文件格式为jpg而不是png吗?所以我决定要做的是BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 4;Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.background1,options));然后通过以下方式将其设置为我的SurfaceView的背景,public class GamePanel extends SurfaceView implements SurfaceHolder.Callback { public static final int WIDTH=578; public static final int HEIGHT=496; .... @Override public void draw(Canvas canvas){ final int scaleXFactor = getWidth()/WIDTH; final int scaleYFactor = getHeight()/HEIGHT; if(canvas!=null){ final int savedState = canvas.save(); canvas.scale(scaleXFactor,scaleYFactor); bg.draw(canvas); canvas.restoreToCount(savedState); }}但是此canvas.scale(scaleXFactor,scaleYFactor)仅允许背景水平放置,而不能垂直放置。所以就像--------------------------------------| IMG || ||-------------------------------------| <-Viewed as "Landscape"| Black Empty Screen || ||-------------------------------------|到目前为止,我的理解是,Bitmap在任何由Android Studio制作的应用程序中都是非常重要的概念,如果未正确实施,它将导致应用程序因OOM错误而崩溃或使应用程序减速。请让我了解一下这个可怜的人,那里的专家。 最佳答案 当/res/drawable或/res/drawable-mdpi文件夹中有图像,并且在具有BitmapFactory.decodeResource()密度的设备上调用xxxhdpi时,BitmapFactory将为您放大位图,以便在屏幕,看起来与较低密度的屏幕相同。如果您确定自己确实不希望发生这种情况,请将图像放在xxxhdpi文件夹中。然后,它将在每个平台上以相同大小进行解码。但是,当您显示图像时,在不同密度的设备上图像看起来会非常不同。关于android - BitmapFactory getWidth()getHeight()和实际图片大小的像素不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38737386/
10-11 03:43