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

createBitmap()返回未绘制的位图

公英哲
2023-03-14
    Bitmap imageBitmap;
    imageBitmap = weiboView.getDrawingCache();
    Log.d("cosmo", "bitmap generated: "+imageBitmap.getWidth()+" * "+imageBitmap.getHeight());

    //Init and configure and load the image view
    ImageView imageView = new ImageView(this);
    imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    imageView.setScaleType(ScaleType.FIT_CENTER);
    containerLayout.addView(imageView);

    //create a scaled bitmap to assign to the image view
    Bitmap scaledImageBitmap = MyBitmapUtils.getBitmapScaledToFitWidth(getWindowManager().getDefaultDisplay().getWidth(), imageBitmap);
    Log.d("cosmo", "scaled: "+scaledImageBitmap.getWidth()+" * "+scaledImageBitmap.getHeight());
    //Here if I set imageBitmap as the image of imageView it works well
    imageView.setImageBitmap(scaledImageBitmap);

以下是MyBitmapUtils.GetBitmapScaledToFitWidth:

public static Bitmap getBitmapScaledToFitWidth(int targetWidth, Bitmap bitmap) {
    float ratio = (float)targetWidth/(float)bitmap.getWidth();
    Log.d("cosmo", "ratio is "+ratio);
    //ratio = 0.5f;
    Matrix matrix = new Matrix();
    matrix.postScale(ratio, ratio);
    Bitmap target = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false);
    return target;
}

我知道这是怎么发生的。这是因为图像的高度大于2048,而在Android系统中创建一个大于2048*2048的位图会导致OOM(奇怪的是,我的logcat没有报告这个错误)

共有1个答案

饶高雅
2023-03-14

为什么不直接使用createScaledBitmap

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);

您可以直接提供targetWidth和targetHeight,这应该比使用矩阵自己缩放位图更可靠。

如果只向方法提供targetWidth,则必须计算targetHeight,它看起来如下所示:

float ratio = (float)targetWidth/(float)bitmap.getWidth();
int targetHeight = (int)((float)bitmap.getHeight * ratio);
public static Bitmap getBitmapScaledToFitWidth(double targetWidth, Bitmap bitmap) {
    double bitmapWidth = bitmap.getWidth();
    double bitmapHeight = bitmap.getHeight();

    double widthRatio = targetWidth / bitmapWidth;
    double targetHeight = widthRatio * bitmapHeight;

    Bitmap target = Bitmap.createScaledBitmap(bitmap, (int)targetWidth, (int)targetHeight, true);
    return target;
}
public static Bitmap getBitmapScaledToFitWidth(double targetWidth, Bitmap bitmap) {
    double bitmapWidth = bitmap.getWidth();
    double bitmapHeight = bitmap.getHeight();

    double widthRatio = targetWidth / bitmapWidth;
    double targetHeight = widthRatio * bitmapHeight;

    Bitmap target = Bitmap.createScaledBitmap(bitmap, (int)targetWidth, (int)targetHeight, true);

    // Recycle old bitmap to free up memory.
    bitmap.recycle();

    return target;
}
 类似资料:
  • 我现在正在做一个游戏,遇到了一些我想不出来的事情。我有一系列的ImageButton,我正在迭代,并在每个按钮上绘制位图。位图本身绘制得很好,但是我也想在这些位图上绘制矩形。 相关代码(如果这还不够,我可以发布更多): 位图本身出现在按钮上,但是我想要绘制的矩形没有被绘制。

  • 我有一个加载了位图的ImageView。当用户触摸位图时,我使用画布在位图上的触摸位置绘制一个圆。现在,我需要撤消绘制的圆。在任何地方,我都能找到一些片段来撤销绘制的路径,而不是直接撤销一个圆。有人有办法撤消先前绘制的圆吗?

  • 这是我的线程的最终输出,它获取屏幕截图并将其存储在一个矢量中。 用于获取屏幕截图,这些截图基本上是屏幕的光栅,不包含光标位置。作为一种解决办法,我使用类获取,然后获取一个。然后在该点绘制图像。 如果将记录区域设置为全屏分辨率,则一切都很酷。但是,如果我改变了记录区域,光标会被绘制在错误的位置。 这个黑色光标应该在Eclipse IDE的Play按钮的顶部。然而,它处于错误的位置。 如果光标超出记录

  • 我正在运行Ubuntu 16.04。在Android Studio上,当我试图在模拟器中运行我的应用程序时,我得到以下错误: 致命的例外:主要过程:项目名称在这里,PID: 2528java.lang.运行时间例外:画布:试图绘制太大(216090000bytes)位图。在android.view.DisplayListCanvas.throw如果不能绘制(DisplayListCanvas.ja

  • 问题内容: 我目前有一个迷宫游戏,它绘制一个5 x 5的正方形(占用屏幕的宽度并将其均匀分割)。然后,对于每个使用x和y坐标的框,我使用drawRect绘制彩色背景。 我遇到的问题是我现在需要在同一位置绘制图像,因此需要替换当前的纯背景色填充。 这是我当前用于drawRect的代码(一些示例): 然后,我还需要为画布中的所有其他正方形实现背景图像。该背景将在其顶部绘制简单的1px黑色线条,当前代码

  • 我觉得我经历了我需要做的一切: 创建一个名为paintComponent的图形类,并扩展JComponent 将图形g作为参数,然后执行Graphics2D g2d=(Graphics2D)g 将图形类添加到我的JFrame 我没发现有什么问题,所以我有点困惑 我的代码在这里: 和