【问题标题】:Image bimap does not fit into canvas exactly?图像双图不完全适合画布?
【发布时间】:2016-12-29 23:49:04
【问题描述】:

我正在尝试将拍摄的照片位图很好地放入画布中进行绘画,但是画布总是忽略位图的大部分。

我正在使用CanvasView API from GitHub,它使用以下 XML 扩展视图

<com.android.graphics.CanvasView
    android:id="@+id/canvasView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

当使用来自here 的另一个 GitHub API 代码中的位图时出现问题:

Bitmap bitmap = ImageLoader.init().from(photoPath).requestSize(width, height).getBitmap();

之后,我将使用上面的位图导入到画布中进行绘制:this.canvasView.drawBitmap(bitmap);

因此,在 .drawBitmap() API 中,它不是通常的 drawBitmap(bitmap,width,height,paint);

public void drawBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        this.invalidate();
    }

尝试了这两种方法来尝试解决问题,但无济于事,还是我在探索错误的方向?还是我在两者之间错过了间歇性步骤/代码?感谢您提供的任何帮助!

canvasView.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY);
width = canvasView.getMeasuredWidth();
height = canvasView.getMeasuredHeight();

canvasView.post( new Runnable() {
    @Override
    public void run() {
        int width  = canvasView.getMeasuredWidth();
        int height = canvasView.getMeasuredHeight(); 
    }
});

【问题讨论】:

  • 位图尺寸是否按要求正确?

标签: android canvas bitmap


【解决方案1】:

在请求、调试或记录位图尺寸后,我会查看位图是否为预期大小。

查看那个 repo 的代码,我不认为 requestSize 做了你认为应该做的事情。

在该库的repo 中,有一条关于请求大小的评论:

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.

现在我敢打赌,这不是你所期望的。

我永远不会为如此简单的东西使用一个不受欢迎的小库,它没有增加任何价值,最好控制它,其他库也是如此。也因为它们不受欢迎,你不会在这里或其他任何地方找到太多帮助,因为人们没有使用它们的经验。

【讨论】:

  • 如果我不得不使用该位图,因为更改该语句我必须修改我的大部分代码。是否可以按原样发送该位图以使其适合画布?
  • 推文?不,我不建议通过 twitter api!
  • 对不起,我的英语能力很差。我的意思是“解决”现有位图收到以适合画布?
  • 哦,调整一下,对不起,我知道你的意思!是的,只需谷歌在 android 中调整位图大小我相信你会找到很多帮助。
  • 嗨,我添加了位图 scaledbitmap = bitmap.createScaledBitmap(bitmap, width, height, true);但是,错误指出宽度和高度必须> 0。我是否错误地编码了获取画布宽度和高度的方式?
猜你喜欢
  • 1970-01-01
  • 2021-06-13
  • 2020-10-24
  • 1970-01-01
  • 2014-05-31
  • 2015-07-03
  • 2015-06-17
  • 2019-05-12
相关资源
最近更新 更多