【问题标题】:draw an image in the center of another rectangle using canvas使用画布在另一个矩形的中心绘制图像
【发布时间】:2011-07-22 01:38:35
【问题描述】:

在我看来,我有一个大矩形,矩形可以移动。当矩形移动到某个地方时,我想在大矩形的中心绘制图像。我的问题是我不能将图像的中心放在矩形的中心。 我用过:

 canvas.drawBitmap(rotatedBitmap, matrix, paint)
 canvas.drawBitmap(rotatedBitmap, left, top, paint)

但我找不到 canvas.drawBitmap(rotatedBitmap, centerX, centerY, paint),所以我想使用矩阵,但矩阵也会从左侧和顶部开始移动图像,而不是从中心开始,你能提供一些线索吗在矩形的中心绘制图片?

【问题讨论】:

    标签: android android-canvas


    【解决方案1】:

    尝试使用矩形的边界作为参考点,然后使用类似的东西:

    imageStartX = (rectStartX + (rectWidth/2)) - (imageWidth/2);

    imageStartY = (rectStartY + (rectHeight/2)) - (imageHeight/2);

    【讨论】:

    • 我试过了,但图像没有正确居中,因为我将矩形的 x 和 y 值分别用于 rectStartX 和 rectStartY。然后我使用了 X 的中心(即宽度/ 2)和 rectStartX 和 rectStartY 的 Y 中心(高度/2),它起作用了。图像完全居中!感谢您提供答案。
    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多