【问题标题】:Set large image as SurfaceView's background so that it suits the display height in Android将大图设置为 SurfaceView 的背景,使其适合 Android 中的显示高度
【发布时间】:2012-02-05 12:04:21
【问题描述】:

我在 Android 中使用 SurfaceView。应用程序始终处于横向模式。

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

现在我想将它设置为 SurfaceView 的背景,以便它完全适合显示。所以图片的高度应该等于显示器的高度。

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

我目前正在使用:

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

我尝试使用矩阵调整位图的大小。但是这样画质就真的很差了不是吗?

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

【问题讨论】:

  • "我尝试使用矩阵来调整位图的大小。但是图像的质量变得很差,不是吗?" - 你试过了,对吧?
  • 我做到了。而且质量变差了。但也许我做错了;-)

标签: android bitmap 2d surfaceview


【解决方案1】:
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
}

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 2011-08-24
    • 2020-04-14
    • 2015-09-11
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 2014-03-31
    • 2013-04-08
    相关资源
    最近更新 更多