Android 可缩放和可滚动棋盘游戏
我正在开发一款 Android 棋盘游戏,并且有一个关于创建可缩放和可滚动棋盘的问题。棋盘包含静态背景、角色(玩家)和使用图块绘制的实际“关卡”。
我的解决方案是拥有一个元素集合(图块、图形、所有游戏元素 - 都有 x、y 坐标和宽度 + 高度)、一个相机和一个渲染器,该渲染器根据cameraX、cameraY、cameraWidth 和cameraHeight 绘制集合。因此,如果用户向右滚动,相机只会适当地设置cameraX - 并且表面是可滚动的。如果用户放大/缩小,渲染器只会适当地缩放每个元素图像。
具有可滚动表面和放大/缩小的渲染器的示例代码
protected function draw(Canvas c){
Collection elements = collection.getElements(cameraX,cameraY,cameraWidth,cameraHeight);
if(elements.size() > 0) {
for(int i = 0; i < elements.size(); i++) {
elements.get(i).drawElement(c);
}
}
}
.
.
.
// element class drawElement function
protected drawElement function(Canvas c) {
if(this.image != null) {
int w = this.width;
int h = this.height;
if(this.zoomFactor < 1) {
w*=this.zoomFactor;
h*=this.zoomFactor;
}
c.drawBitmap(this.image,this.x,this.y,w,h);
}
}
- 这是最好的解决方案吗?
- 可以通过其他方式实现吗?
- 可以使用 ScrollView 实现滚动吗?
我不想使用任何引擎,因为这是一个学校项目。
I'm developing a Android board game and have a question regarding creating a board that is zoomable and scrollable. The board contains a static background, characters (players) and the actual "level" which is drawn using tiles.
My solutions is to have a collection of elements (tiles, figures, all game elements - all have x,y coordinates and width + height), a camera and a renderer that draws the collection according to cameraX,cameraY, cameraWidth and cameraHeight. So if a user would scroll to the right, the camera would just set the cameraX appropriately - and the surface is scrollable. And if a user would zoom in/out the renderer would just scale every element image appropriately.
Example code for the renderer with scrollable surface and zoom in/out
protected function draw(Canvas c){
Collection elements = collection.getElements(cameraX,cameraY,cameraWidth,cameraHeight);
if(elements.size() > 0) {
for(int i = 0; i < elements.size(); i++) {
elements.get(i).drawElement(c);
}
}
}
.
.
.
// element class drawElement function
protected drawElement function(Canvas c) {
if(this.image != null) {
int w = this.width;
int h = this.height;
if(this.zoomFactor < 1) {
w*=this.zoomFactor;
h*=this.zoomFactor;
}
c.drawBitmap(this.image,this.x,this.y,w,h);
}
}
- Is this the best solutions?
- Could it be achived somehow else?
- Could scrolling be achived using a ScrollView?
I dont wanna use any engine, because this is for a school project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上你可以稍微简化一下这种情况。如果您确实正在寻找一个简单地因透视而扭曲的平坦纹理平面,那么 Android
Camera
类可以帮助您。不要将其与用于拍照的硬件相机混淆。该相机是一个围绕矩阵的辅助类,用于对 2D 对象执行转换。您可以通过谷歌搜索“快速傅里叶变换”来阅读有关这个非常复杂的渲染主题的更多信息。基本上,您需要创建一个画布并以完全 2D 的方式进行绘图。然后,在绘制到屏幕之前,您应该使用 Camera 类转换此画布。如果您需要澄清,请告诉我。幕后有很多很酷的数学!查看 Android API 演示中的示例
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/animation/Rotate3dAnimation.html
Android
graphics.Camera
文档http://developer.android.com/reference/android/graphics/Camera。 html
Actually you can simplify this situation somewhat. If you are indeed seeking a flat texture plane that is simply distorted by perspective, the Android
Camera
class can help you. Do not confuse this with the hardware camera for taking photos. This camera is a helper class wrapped around a matrix to perform transformations on 2D objects. You can read more about this very complex rendering topic by googling "fast fourier transforms". Basically you will want to create a canvas and do your drawing in a completely 2D way. Then right before you draw to the screen, you should transform this canvas using the Camera class. Let me know if you need some clarification. There is a lot of cool mathematics going on behind the scenes!Take a look at this sample from the Android API Demos
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/animation/Rotate3dAnimation.html
Android
graphics.Camera
documentationhttp://developer.android.com/reference/android/graphics/Camera.html