【问题标题】:Scaling and translating a bitmap in android在android中缩放和翻译位图
【发布时间】:2013-09-13 14:22:24
【问题描述】:

我正在尝试销售位图并在每个步骤中对其进行翻译。

如果我们看下面的代码,我正在绘制一个图像,对其进行平移和缩放,然后反向执行相同的操作以恢复原始配置。但是在应用这些操作之后,我确实得到了原始的缩放图像(比例因子 1),但是图像被平移到了不同​​的位置。

您能指出正确的方法吗? (在上面的例子中,我如何达到原来的配置?​​)

 protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Matrix matrix = new Matrix();

        scale = (float)screenWidth/201.0f;
        matrix.setTranslate(-40, -40);
        matrix.setScale(scale, scale);

        canvas.drawBitmap(bitMap, matrix, paint);

        //back to original
        canvas.drawColor(0, Mode.CLEAR);
        matrix.setScale(1.0f/scale, 1.0f/scale);
        matrix.setTranslate(40,40);
        canvas.drawBitmap(bitMap, matrix, paint);

    }

【问题讨论】:

  • 参考以下链接,它将帮助您[在此处输入链接描述][1] [1]:stackoverflow.com/questions/8722359/…
  • @JohnSakthivel - 感谢您提供信息
  • Matrix.setXXX 会先重置矩阵,然后设置值

标签: android image-processing bitmap scaling


【解决方案1】:

您应该只使用Canvas 方法进行缩放和转换,这样您就可以利用save()restore() API 来做您需要的事情。例如:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    //Save the current state of the canvas
    canvas.save();

    scale = (float) screenWidth / 201.0f;

    canvas.translate(-40, -40);
    canvas.scale(scale, scale);
    canvas.drawBitmap(bitMap, 0, 0, paint);

    //Restore back to the state it was when last saved
    canvas.restore();

    canvas.drawColor(0, Mode.CLEAR);
    canvas.drawBitmap(bitMap, 0, 0, paint);
}

【讨论】:

  • 这解决了我的问题,但是任何关于我的代码为什么不起作用的指针?
【解决方案2】:

我认为您的原始代码的问题可能是因为缩放和翻译使用您缩放/翻译的点的方式。如果您在操作之间/为操作指定正确的枢轴点,一切都会好起来的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2014-03-05
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2014-01-03
    相关资源
    最近更新 更多