当前位置: 首页 > 知识库问答 >
问题:

位图太大,缩放后无法上传到纹理中

臧梓
2023-03-14

我做了一个有很大背景的应用程序。它在Nexus7上运行得很好,但在我的Galaxy Nexus上,背景消失了,在logcat中给出了一个错误:位图太大,无法上传到纹理中(...)。

Display display = getWindowManager().getDefaultDisplay();
    width = display.getWidth();
    height = display.getHeight();


    //-------------------------------------------------------------------------------------------------------------

    ImageView achtergrond = (ImageView)findViewById(R.id.achtergrond);
    achtergrond.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.achtergrond, width, height));
} (...)

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;

谢谢你提前帮我!

共有1个答案

锺功
2023-03-14

将背景移动到drawable-nodpi文件夹。

正如你在其他类似问题的回答中已经说过的,每个设备对图像在屏幕上呈现为纹理的最大维度都有限制;宽度和高度通常为2048。然而,如果您将drawable放在一个没有密度限定符的文件夹中,Android将假定drawable是mdpi,并将其放大或缩小以用于其他密度。例如,如果您的屏幕具有xhdpi密度,drawable会放大两倍:1100x2=2200,这对于2048的限制来说太大了。

 类似资料:
  • 除了oncreate之外,我的MainActivity是空的。我已经阅读了关于这个问题的其他帖子,但没有一个为我提供任何真正的解决方案。

  • 本文向大家介绍bmp 缩放代码(BMP位图任意放大 和缩小),包括了bmp 缩放代码(BMP位图任意放大 和缩小)的使用技巧和注意事项,需要的朋友参考一下 前几天碰上需要对bmp位图进行缩放的功能, 调用API函数,虽然能实现位图缩放,但是对有放大的效果好,缩小会造成失真,图像上有花点,让人难以接受 ,因为本人以前学易语言,易语言有一段代码,对bmp图像缩放效果非常 好, 昨天抽空,把它翻译成c+

  • 我想在我的Android应用程序中增加位图的大小。 基本上,我想创建一个新的位图,其宽度与原始位图相同,但高度更大。(新)额外像素的背景可以是黑色、白色或透明。 我该怎么做?

  • 问题内容: 如果我有画布,则可以在其上绘制如下的位图: 然后缩放位图: 我想获得比例尺后位图的位置。如果小数位数前的位置是(0,0),小数位数后的位置有一个偏移量,那么我需要该偏移量。如何获取? 谢谢你,很抱歉这个简单的问题,新手在这里… 问题答案: 好吧,让我们尝试为此制定最佳的公式 对于objectNewY也是一样。位图的新宽度和高度当然将是旧大小和比例的倍数。

  • 我有一个图像,我正在尝试缩放,因为屏幕大小减少。在桌面上,1336x625分辨率的图像可以正常工作,但一旦屏幕缩小到500x625时,图像就不能适当缩放,边缘也会被削减。我试着把最大宽度和位置。但还是没用。我需要改变什么才能让它工作。 Css代码 Html代码:

  • 我有一个库伯内特斯部署,它部署了一个pod,该pod将从RabbitMQ队列中下拉一条消息。我还使用KEDA根据队列中当前的RabbitMQ消息扩展部署。它正确地扩展到0,然后在有消息时扩展到1,但部署永远不会扩展到1以上。我当前的部署YAML文件: 我的KEDA YAML文件: KEDA 操作员日志文件: 我知道RabbitMQ连接工作的所有情况,KEDA知道要查看什么部署以及什么队列。所有这些