使用画布绘制 AViewOnAnother

发布于 2024-12-02 21:45:47 字数 220 浏览 0 评论 0原文

我对此进行了很多搜索,但没有找到任何支持我的原因的东西,我有view1,即国际象棋棋盘。我使用canvas绘制view1。现在我有另一个view2(国际象棋车的元素,象)要在国际象棋上绘制木板。所以指导我

 1).Is  it possible?
 2)If not then what is alternative? ,i donot want to use layout

I search a lot on this , but did not find anything that support my cause, I have view1 that is chess board.I draw view1 using canvas.Now i have another view2(element of chess rook,bishop) to be drawn on the chess board. So guide me

 1).Is  it possible?
 2)If not then what is alternative? ,i donot want to use layout

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2024-12-09 21:45:47

是的,这是可能的。您必须已经将车等作为可绘制对象放在 res/drawable 文件夹中,然后在每个方格中间的 ArrayList 中记录 X 和 Y 坐标,然后在相同的 x 和 y 处绘制车、骑士等协调。

每次棋子移动时,您都需要根据玩家将棋子移动到的 x、y 坐标重新绘制视图 1 和视图 2。

转到这里: http://developer.android.com/reference/android/graphics/ Canvas.html

您可以将其用于 drawRect 来绘制棋盘上的正方形,并且您可以使用 drawBitmap 来绘制车和马, ETC。

但是如何通过canvas在view1上绘制view2

你实际上并不需要view2。只需使用一个视图绘制棋盘,然后在同一个位图上绘制每个棋子。

但如果不这样做,在同一画布上移动玩家将会变得非常忙碌
创建另一个画布。请告诉我是否可以在彼此上绘制两个画布

。假设您有多个视图,您可以有多个画布。所以我想你可以让 view1 绘制棋盘,然后 view2 将成为棋子,但这确实没有必要。假设 pawn1 位于 x1,y1,user2 移动到 x2,y2,然后在 x1,y1 处绘制一个新矩形(黑色或白色,具体取决于它在棋盘上的位置),然后在 x2 处重新绘制棋子,y2。

您可以在 View2 上执行此操作,但我不能 100% 确定如何实现第二个视图。我想你必须将每个动作的每一个部分重新绘制到新的位图上,而不是回收旧的位图。

重新绘制正方形然后在 x2, y2 处重新绘制棋子会容易得多。

Yes it is possible. You would have to already have the rooks, etc as drawables in your res/drawable folder, then record an X and Y coordinate in an ArrayList for the middle of each square, then draw the rooks, knights, etc at the same x and y coordinate.

Every time a piece moves, you will need to re-draw view1 and view 2 based on whatever x,y coordinate the player moved the piece to.

go here: http://developer.android.com/reference/android/graphics/Canvas.html

You could maybe use that for drawRect in order to draw the squares on the board, and you would use drawBitmap to draw the rooks and the knights, etc.

But how to draw a view2 on view1 both by canvas

You really don't actually need view2. Just use one view and draw the chessboard, then on the same bitmap, draw each chess piece.

But to move player on same canvas will became so hectic if do not
create another canvas.Tell me please it possible to draw two canvas on each other

You could have more than one canvas, assuming you have more than one View. So I suppose you could make view1 draw the board, then view2 would be the pieces, but that's really not necessary. Say you have pawn1 that is at x1,y1, and user2 moves to x2,y2, then draw a new rectangle at x1, y1 (black or white depending on where it is on the board), then re-draw the piece at x2,y2.

And you could do that on View2, but I'm not 100% sure how you would implement a second view. I guess you'd have to re-draw every piece for every move onto a new bitmap instead of recycling an old bitmap.

It would be much easier to re-draw the square then re-draw the piece at x2, y2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文