我正在使用 Android View 创建一个棋盘。
java: 我有一个 Board 对象,其中包含 64 个 Cell 对象的数组。每个 Cell 对象都有一个必需的背景图像和一个可选的前景图像。我正在尝试想出一个很好的方法来绘制它。
机器人:
我目前拥有的: - 每个单元格都有一个 drawSelf 函数,它返回一个 FrameLayout,两个图像有两个子 ImageView 。 - board.xml 有一个网格布局作为根。
我已经尝试制作这种结构,但是当我尝试将 FrameLayouts 添加到面板时我遇到了各种错误,而且我在保持 FrameLayouts 受 GridLayout 的网格线限制方面也遇到了问题。
注意: 我不受此数据结构的束缚,最终我想要的是某种方法来绘制一个 8x8 板,其单元格的宽度和高度为方形板的 1/8,每个单元格都能够显示两个图像,持有一个 ID,并触发对我的游戏逻辑的 onClick(self.id) 调用。
谢谢。
最佳答案
实际上,我构建相同的东西是为了好玩,以扩展我在 Android 开发方面的知识。我正在尝试一种方法,我有一个 Framelayout,其中包含 8 个 LinearLayouts 的 8 个 LinearLayouts,每个 LinearLayouts 都有一个 ImageView。这种方法为我吸引了一切,而且我免费获得了 onclick。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/car_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66ccff"
tools:context=".Activity_Main" >
<FrameLayout
android:id="@+id/checker_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="false" >
<LinearLayout
android:layout_width="315dp"
android:layout_height="300dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageView
android:id="@+id/square_1_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:onClick="squareOnClick" />
<ImageView
android:id="@+id/square_1_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000"
android:onClick="squareOnClick" />
<ImageView
android:id="@+id/square_1_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:onClick="squareOnClick" />
<ImageView
android:id="@+id/square_1_4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000"
android:onClick="squareOnClick" />
<ImageView
android:id="@+id/square_1_5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:onClick="squareOnClick" />
<ImageView
android:id="@+id/square_1_6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000"
android:onClick="squareOnClick" />
<ImageView
android:id="@+id/square_1_7"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFFFFF"
android:onClick="squareOnClick" />
<ImageView
android:id="@+id/square_1_8"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000"
android:onClick="squareOnClick" />
</LinearLayout>
--Repeat Linear layouts
您可以看到我在所有 ImageView 上都有一个 squareOnClick 调用方法。每次点击都会转到此监听器。由于它的 ID 名称,我可以识别它的 ID,您可以根据它使用react:
public void squareOnClick(View view)
{
String idName = getResources().getResourceEntryName(view.getId());
idName = idName.replace("square_", "");
String[] coordinate = idName.split("_");
if(coordinate.length != 2)
return;
int x = Integer.parseInt(coordinate[0]);
int y = Integer.parseInt(coordinate[1]);
Log.i("Activity_Checker", "Coordinate pressed: " + x + ":" + y);
TextView tv = (TextView) findViewById(R.id.statustextview);
tv.setText("X:" + x + " Y:" + y);
}
然而,如果免费绘制所有内容,我正在考虑这个想法,但那只是在我无法弄清楚其他一些问题的情况下:
- 正确重新定位框架布局
- 正确调整框架布局
- 跨布局的属性动画
这就是我得到的结果。
关于棋盘的 Android 布局层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17310077/