2013-09-22 105 views
0

我是Android新手。Android顯示網格地圖

我的目標是能夠創建30乘30柵格地圖的最大值。網格地圖的大小可能會有所不同。最重要的是,我能夠改變細胞的顏色。我嘗試了2種方法,但它們似乎並不奏效。

方法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錯誤。

任何指導或解決方案可以指導我通過實現我的目標?

謝謝!

回答

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); 
+0

尼斯!謝謝你的建議! =) – user2804646

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監聽器應該在Oncreate方法中。