2012-07-16 78 views
0

我正在創建一個應用程序,它將作爲背景美國地圖。我想單擊地圖並單擊我想要創建的一個小紅點。但它不應該在地圖邊界之外創建。我可以使用GridView嗎?謝謝。Android可點擊的地圖圖片

回答

1

您只需創建一個線性佈局與美國地圖作爲背景,然後設置一個onTouchListener到線性佈局,裏面的onTouch只需添加在x一個紅點和y座標,該人點擊。事情是這樣的:

public class MyAppActivity extends Activity {  

LinearLayout mLayout; 
double x, y;  

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  

    mLayout = (LinearLayout) findViewById(R.id.yourID); 
    mLayout.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      x = event.getX(); 
      y = event.getY(); 
      //Draw a ball using the x and y coordinates. You can make a separate class to do that. 

      return false; 
    });   

} 
} 

和您的XML可能看起來像:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/yourID" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/yourUSABackGround" 
    > 




</LinearLayout> 

希望有所幫助。

+0

謝謝。我如何畫球? – 2012-07-16 20:33:07

+0

我不想讓球出現在地圖邊界之外。我怎麼做?謝謝。 – 2012-07-16 20:40:59

+0

我不想/有時間爲你編寫整個代碼。您應該開始通過在Google或StackOverflow上搜索來查找一些示例。然後如果你有一個具體的問題回來問。 – 0gravity 2012-07-16 20:51:37