【问题标题】:How to scale a bitmap larger than 4096 pixels?如何缩放大于 4096 像素的位图?
【发布时间】:2014-09-14 00:27:13
【问题描述】:

我有两个想要实现的目标。

  1. 按比例缩放位图。
  2. 缩放相同的位图,使其高度等于设备的高度。

尝试此操作时,我发现在像素较少的设备上,缩放工作正常,但在像素较多的设备上,缩放会强制位图达到 4096x4096 像素限制:

W/OpenGLRenderer(10630): Bitmap too large to be uploaded into a texture (4476x885, max=4096x4096)

到目前为止,我正在使用 Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) 来缩放我的位图:

float conversion = (float) view.getHeight() / (float) originalBitmap.getHeight();

Matrix mat = new Matrix();
mat.postScale(conversion,conversion);

Bitmap resizedBitmap =
     Bitmap.createBitmap(originalBitmap,0,0,originalBitmap.getWidth(),
     originalBitmap.getHeight(), mat, false);
imageView.setImageBitmap(resize);

上面的代码加上下面的 xml 允许位图缩放整个屏幕:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:scaleType="matrix"/>

如果有任何其他方法可以将位图按比例缩放到整个屏幕,我将非常感谢这些建议。

【问题讨论】:

    标签: android matrix bitmap scale fullscreen


    【解决方案1】:

    使用Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter); 其中 dstWidth 和 dstHeight 分别是所需的宽度和高度。 (过滤器只是在为真时尝试平滑边缘,如果为假则不尝试平滑边缘,如 here 所示。)这不需要 Matrix 参数或所需位置,它只返回缩放位图。

    以下内容可能对您有用:

    Bitmap resizedBitmap = 
         Bitmap.createScaledBitmap(originalBitmap, 
         originalBitmap.getWidth() * (view.getHeight() / originalBitmap.getHeight()),
         view.getHeight());
    imageView.setImageBitmap(resizedBitmap);
    

    您现在可能也只需要这么多 XML:

    <ImageView
         android:id="@+id/imageView1"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    

    这是因为您不需要将占据其整个父级的东西居中,并且您不再使用矩阵进行缩放。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      相关资源
      最近更新 更多