Android可点击地图图片

时间:2012-07-16 19:56:35

标签: android imagemagick clickable

我正在创建一个应用程序,它将作为背景美国地图。我想点击地图,无论我点击哪里,我想创建一个小红点。但它不应该在地图边界之外创建。我可以使用GridView吗?感谢。

1 个答案:

答案 0 :(得分: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>

希望有所帮助。