使用画布绘制 AViewOnAnother
我对此进行了很多搜索,但没有找到任何支持我的原因的东西,我有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。您必须已经将车等作为可绘制对象放在 res/drawable 文件夹中,然后在每个方格中间的 ArrayList 中记录 X 和 Y 坐标,然后在相同的 x 和 y 处绘制车、骑士等协调。
每次棋子移动时,您都需要根据玩家将棋子移动到的 x、y 坐标重新绘制视图 1 和视图 2。
转到这里: http://developer.android.com/reference/android/graphics/ Canvas.html
您可以将其用于
drawRect
来绘制棋盘上的正方形,并且您可以使用drawBitmap
来绘制车和马, ETC。你实际上并不需要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 usedrawBitmap
to draw the rooks and the knights, etc.You really don't actually need view2. Just use one view and draw the chessboard, then on the same bitmap, draw each chess piece.
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.