Android显示网格图

时间:2013-09-22 17:11:30

标签: android map

我是Android新手。

我的目标是能够创建最多30 x 30的网格图。网格图的大小可能会有所不同。最重要的是,我能够改变细胞的颜色。我尝试了两种方法,但它们似乎不起作用。

方法1,我使用onDraw。我设法得到了地图,但我无法为我想要的细胞上色:

public static final float DEFAULT_X_OFFSET= 10;
public static final float DEFAULT_Y_OFFSET= 10;
Paint paint = new Paint();

    public void onDraw(Canvas canvas)
     {
               paint.setColor(Color.YELLOW);
                float X=DEFAULT_X_OFFSET;
                float Y=DEFAULT_Y_OFFSET;
                //Draw The rows
                for(float iRow=0;iRow<=m_NoOfRows;iRow++)
                {
                        canvas.drawLine(X, Y,X+ this.m_width* this.m_NoOfCols,Y, paint);
                        Y=Y+ m_Height;

                }

                //Draw The Cols
                X=DEFAULT_X_OFFSET;
                Y=DEFAULT_Y_OFFSET;
                for(float iColumn=0;iColumn<=m_NoOfCols;iColumn++)
                {
                        canvas.drawLine(X, Y,X,Y+this.m_Height*this.m_NoOfRows,paint );
                        X=X+ this.m_width;
                }

        }

方法2,我用按钮来表示细胞。我做了部分看它是否有效,但似乎创建30x30按钮是不合理的。

activity_main.xml中:

        <LinearLayout
    android:id="@+id/row1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/r1c1"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginBottom="2dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c2"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c3"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c4"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c5"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c6"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c7"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c8"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c9"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c10"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c11"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c12"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c13"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c14"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c15"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c16"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c17"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c18"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c19"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c20"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c21"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c22"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c23"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c24"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c25"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c26"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c27"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c28"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c29"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />

    <Button
        android:id="@+id/r1c30"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />
</LinearLayout>

<LinearLayout
    android:id="@+id/row2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/row1"
    android:layout_marginBottom="2dip"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/r2c1"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_marginRight="2dip"
        android:background="@color/gray"
        android:clickable="false"
        android:enabled="false" />
        <!-- teting purposes, created 10 rows -->
    </LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/row10"
    android:layout_below="@+id/row10"
    android:layout_marginTop="29dp"
    android:onClick="trigger"
    android:text="Trigger" />

Map.java:

Button[][] mapArrays;

private int row = 2;
private int col = 10;

private Button r1c1;
private Button r1c2;
private Button r1c3;
private Button r1c4;
private Button r1c5;
private Button r1c6;
private Button r1c7;
private Button r1c8;
private Button r1c9;
private Button r1c10;
//initialize for 2rows of buttons for testing purposes

    public Map(Context context) {
    super(context);

}

public void mapSize(){

    initialize_array();

    for(int r=0; r<row; r++){
        for(int c=0; c<col; c++){
            mapArrays[r][c].setBackgroundColor(0xffffffff); 
        }
    }

}

private void initialize_array() {
    mapArrays = new Button[][] 
                {
                    {r1c1,r1c2,r1c3,r1c4,r1c5,r1c6,r1c7,r1c8,r1c9,r1c10,r1c11,r1c12,r1c13,r1c14,r1c15,r1c16,r1c17,r1c18,r1c19,r1c20,r1c21,r1c22,r1c23,r1c24,r1c25,r1c26,r1c27,r1c28,r1c29,r1c30},
                    {r2c1,r2c2,r2c3,r2c4,r2c5,r2c6,r2c7,r2c8,r2c9,r2c10,r2c11,r2c12,r2c13,r2c14,r2c15,r2c16,r2c17,r2c18,r2c19,r2c20,r2c21,r2c22,r2c23,r2c24,r2c25,r2c26,r2c27,r2c28,r2c29,r2c30}
                };

    r1c1 = (Button) findViewById(R.id.r1c1);
    r1c2 = (Button) findViewById(R.id.r1c2);
    r1c3 = (Button) findViewById(R.id.r1c3);
    r1c4 = (Button) findViewById(R.id.r1c4);
    r1c5 = (Button) findViewById(R.id.r1c5);
    r1c6 = (Button) findViewById(R.id.r1c6);
    r1c7 = (Button) findViewById(R.id.r1c7);
    r1c8 = (Button) findViewById(R.id.r1c8);
    r1c9 = (Button) findViewById(R.id.r1c9);
    r1c10 = (Button) findViewById(R.id.r1c10);
    r1c11 = (Button) findViewById(R.id.r1c11);
    r1c12 = (Button) findViewById(R.id.r1c12);
    r1c13 = (Button) findViewById(R.id.r1c13);
    r1c14 = (Button) findViewById(R.id.r1c14);
    r1c15 = (Button) findViewById(R.id.r1c15);
    r1c16 = (Button) findViewById(R.id.r1c16);
    r1c17 = (Button) findViewById(R.id.r1c17);
    r1c18 = (Button) findViewById(R.id.r1c18);
    r1c19 = (Button) findViewById(R.id.r1c19);
    r1c20 = (Button) findViewById(R.id.r1c20);
    r1c21 = (Button) findViewById(R.id.r1c21);
    r1c22 = (Button) findViewById(R.id.r1c22);
    r1c23 = (Button) findViewById(R.id.r1c23);
    r1c24 = (Button) findViewById(R.id.r1c24);
    r1c25 = (Button) findViewById(R.id.r1c25);
    r1c26 = (Button) findViewById(R.id.r1c26);
    r1c27 = (Button) findViewById(R.id.r1c27);
    r1c28 = (Button) findViewById(R.id.r1c28);
    r1c29 = (Button) findViewById(R.id.r1c29);
    r1c30 = (Button) findViewById(R.id.r1c30);

    r2c1 = (Button) findViewById(R.id.r2c1);
    r2c2 = (Button) findViewById(R.id.r2c2);
    r2c3 = (Button) findViewById(R.id.r2c3);
    r2c4 = (Button) findViewById(R.id.r2c4);
    r2c5 = (Button) findViewById(R.id.r2c5);
    r2c6 = (Button) findViewById(R.id.r2c6);
    r2c7 = (Button) findViewById(R.id.r2c7);
    r2c8 = (Button) findViewById(R.id.r2c8);
    r2c9 = (Button) findViewById(R.id.r2c9);
    r2c10 = (Button) findViewById(R.id.r2c10);
    r2c11 = (Button) findViewById(R.id.r2c11);
    r2c12 = (Button) findViewById(R.id.r2c12);
    r2c13 = (Button) findViewById(R.id.r2c13);
    r2c14 = (Button) findViewById(R.id.r2c14);
    r2c15 = (Button) findViewById(R.id.r2c15);
    r2c16 = (Button) findViewById(R.id.r2c16);
    r2c17 = (Button) findViewById(R.id.r2c17);
    r2c18 = (Button) findViewById(R.id.r2c18);
    r2c19 = (Button) findViewById(R.id.r2c19);
    r2c20 = (Button) findViewById(R.id.r2c20);
    r2c21 = (Button) findViewById(R.id.r2c21);
    r2c22 = (Button) findViewById(R.id.r2c22);
    r2c23 = (Button) findViewById(R.id.r2c23);
    r2c24 = (Button) findViewById(R.id.r2c24);
    r2c25 = (Button) findViewById(R.id.r2c25);
    r2c26 = (Button) findViewById(R.id.r2c26);
    r2c27 = (Button) findViewById(R.id.r2c27);
    r2c28 = (Button) findViewById(R.id.r2c28);
    r2c29 = (Button) findViewById(R.id.r2c29);
    r2c30 = (Button) findViewById(R.id.r2c30);
}

MainActivity.java:

private Button button;
private Map map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
}

public void initialize() {
    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            map = new Map(MainActivity.this);
            map.mapSize();
        }
    });

}

一旦我触发按钮,我就会遇到AndroidRuntime错误。

可以指导我实现目标的任何指导或解决方案?

谢谢!

2 个答案:

答案 0 :(得分:0)

private Button button;
private Map map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            map = new Map(MainActivity.this);
            map.mapSize();
        }
    });

}

public void initialize() {
    button = (Button) findViewById(R.id.button1);

}

Click listener应该在Oncreate方法中。

答案 1 :(得分:0)

方法2似乎有点矫枉过正,特别是当地图的大小发生变化时,就像你指出的那样。

所以,我使用方法1.您只需要调用canvas.drawRect(...)而不是canvas.drawLine(...),然后告诉矩形填充paint.setStyle(Paint.Style.FILL);

绘制一个单元格如下:

paint.setColor(Color.YELLOW);
paint.setStyle(Paint.Style.FILL); // or Paint.Style.FILL_AND_STROKE
paint.drawRect(x, y, x+width, y-height, paint);