将大图像设置为SurfaceView';s的背景,使其适合Android中的显示高度

将大图像设置为SurfaceView';s的背景,使其适合Android中的显示高度,android,bitmap,2d,surfaceview,Android,Bitmap,2d,Surfaceview,我在安卓系统中使用SurfaceView。应用程序始终处于横向模式 在我的res/drawable文件夹中,我有一个大背景,也处于横向模式,但比大多数显示尺寸都大 现在,我想将其设置为SurfaceView的背景,以使其完全适合显示器。因此,图像的高度应该等于显示器的高度 我该怎么做?我是否必须为ldpi、mdpi、hdpi和xhdpi使用替代资源?但即使这样,我也不知道显示器的确切高度 我目前正在使用: canvas.drawBitmap(BitmapFactory.decodeResou

我在安卓系统中使用SurfaceView。应用程序始终处于横向模式

在我的res/drawable文件夹中,我有一个大背景,也处于横向模式,但比大多数显示尺寸都大

现在,我想将其设置为SurfaceView的背景,以使其完全适合显示器。因此,图像的高度应该等于显示器的高度

我该怎么做?我是否必须为ldpi、mdpi、hdpi和xhdpi使用替代资源?但即使这样,我也不知道显示器的确切高度

我目前正在使用:

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.background), 0, 0, null);

我尝试使用矩阵调整位图的大小。但是图像的质量变得很差,不是吗


(当然,解决方案也应该是高性能的。)

“我试图使用矩阵调整位图的大小。但是图像的质量变得很差,不是吗?”-你试过了,对吗?我试过了。质量变差了。但也许我做错了;-)
protected Bitmap scaled = null;

public void surfaceCreated(SurfaceHolder arg0) {
    Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
    float scale = (float)background.getHeight()/(float)getHeight();
    int newWidth = Math.round(background.getWidth()/scale);
    int newHeight = Math.round(background.getHeight()/scale);
    Bitmap scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
}

public void onDraw(Canvas canvas) {
    canvas.drawBitmap(scaled, 0, 0, null); // draw the background
}