将大图像作为SurfaceView的背景,使之适合在Android上的显示高度 [英] Set large image as SurfaceView's background so that it suits the display height in Android

查看:408
本文介绍了将大图像作为SurfaceView的背景,使之适合在Android上的显示高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android中一个SurfaceView。该应用程序始终处于横向模式。

在我的 RES /绘制的文件夹,我有一个大的背景,这也是在横向模式,但大于大多数显示器尺寸。

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

我该怎么办呢?我一定要使用替代资源LDPI,MDPI,华电国际和xhdpi?但即使如此,我不知道显示器的确切高度。

我目前使用的:

  canvas.drawBitmap(BitmapFactory.de codeResource(getResources(),R.drawable.background),0,0,NULL);
 

我试图用一个矩阵来调整位图。但随后的图像质量就真的很差,不是吗?

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

解决方案

 受保护的位图缩放= NULL;

公共无效surfaceCreated(SurfaceHolder为arg0){
    位图背景= BitmapFactory.de codeResource(getResources(),R.drawable.background);
    浮规模=(浮点)background.getHeight()/(浮动)的getHeight();
    INT newWidth = Math.round(background.getWidth()/比例);
    INT newHeight = Math.round(background.getHeight()/比例);
    位图缩放= Bitmap.createScaledBitmap(背景下,newWidth,newHeight,真正的);
}

公共无效的OnDraw(帆布油画){
    canvas.drawBitmap(缩放,0,0,NULL); //绘制背景
}
 

I'm using a SurfaceView in Android. The application is always in landscape mode.

In my res/drawable folder I have a large background that is also in landscape mode but larger than most display sizes.

Now I would like to set it as the background of the SurfaceView so that it perfectly fits into the display. So the image's height should be equal to the display's height.

How do I do that? Do I have to use alternative resources for ldpi, mdpi, hdpi and xhdpi? But even then I don't know the exact height of the display.

I'm currently using:

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

I tried to resize the bitmap using a matrix. But then the quality of the image becomes really poor, doesn't it?

(Of course, the solution should be performant, too.)

解决方案

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
}

这篇关于将大图像作为SurfaceView的背景,使之适合在Android上的显示高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆