繁体   English   中英

围绕X轴旋转的画布上绘制位图的问题

[英]Problem with draw bitmap on rotated canvas around X axis

美好的一天,

我在绘制图像时遇到了奇怪的问题。 代码和图片说的比话多。

mCamera.save();
mCamera.rotateX(rotateX);
mCamera.getMatrix(mMatrix);
mCamera.restore();

canvas.save();
canvas.setMatrix(mMatrix);

// here I draw images
...

canvas.restore();

结果如下图所示。 第一张图片的角度为0,第二张图片的角度为45度,最后一张为更多。 这取决于我要绘制的区域大小(位图的数量)。 我注意到当我绘制完全适合画布边界的位图时,它工作正常。 但是问题主要是当我用类似的东西绘制图像时(所以画布外部有一部分)

canvas.drawBitmap(image, -100, -100, null)

和图片(嗯,因为我是新来的,所以我不能直接发布图片...)

ttp://locus.asamm.cz/data/_temp/1311873666794.png

http://locus.asamm.cz/data/_temp/1311873695945.png

http://locus.asamm.cz/data/_temp/1311873782054.png

所以问题是,是否有任何可行的解决方案或至少有任何技巧。 另外,如果有经验的人可以告诉您使用OpenGL绘图是否可以解决此问题,如果可以,请指向任何可以帮助我更改位图变化很大的信息源(它是地图应用程序,因此用户随地图一起移动),因为我仍然找不到任何简单明了的信息来源。

非常感谢大家

这是2D图形API的局限性。 要执行真正的3D,您应该使用OpenGL。

我希望您正在尝试产生一种Google地图倾斜/旋转效果,我已经成功为Google地图完成了此效果,因为他们认为我们不需要更新;-),即使您使用的是我自己的应用程序,我也需要它平铺基本上是同一件事。

有帮助的一件事是,实现了Camera Matrix的旋转是在左上角进行的。

因此,您应该转换到适当的位置(取决于旋转轴和预期的结果),然后再返回类似...的效果,在我阅读文档并弄清楚之前,得到的效果与我的非常相似。

camera.save();
    camera.rotateX(tilt);  // tilt forward or backward
    camera.rotateY(0);
    camera.rotateZ(angle); // Rotate around Z access (similar to canvas.rotate)                                 

     camera.getMatrix(cameraMatrix);

     // This moves the center of the view into the upper left corner (0,0) 
     // which is necessary because Matrix always uses 0,0, as it's transform point
     // use the center of the canvas, or the bottom center dependent on your results 
     cameraMatrix.preTranslate(-centerX, -centerY);

     // NOTE: Camera Rotations logically happen here when the canvas has the matrix applied  
     // This happens after the camera rotations are applied, moving the view back to 
     // where it belongs, allowing us to rotate around the center or any point we choose 

     // Move it back after the rotations are applied
     cameraMatrix.postTranslate(centerX, centerY);

camera.restore();

canvas.concat(cameraMatrix);

另一件事是加大容器的尺寸,以便在倾斜/旋转操作之后没有空白空间(或至少限制了它的空间),可以通过计算所需的加大尺寸来做到这一点,显示屏的对角线适用于基本旋转,+(基于倾斜度的百分比)(如果使用的话)

玩pre / postTranslate可以给您一些有趣的结果。

至于移动位图,我不太确定为什么要这么做,可能是平移,但是只要画布被填满就没关系

更新资料

这是我加大画布的方式,我尝试使用LayoutParams来完成此操作,但是运气不佳,因此Measure是适合我的方法。

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    if (!isInEditMode())
        super.onMeasure(View.MeasureSpec.makeMeasureSpec(scaledDimensionsW,
        View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(scaledDimensionsH,
        View.MeasureSpec.EXACTLY));
   else
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM