2012-10-11 194 views
0

我現在在一個項目上工作。我有2個可拖動的textView在一個圓圈內。我想在圓圈拖過另一個圓圈時將這些值添加到圓圈內。我擁有的第一個選項是獲得圓的X和Y,但我明白了。誰能修復我的代碼?Android:獲取TextView的X和Y座標?

這裏是代碼:

MainActivity

public class MainActivity extends Activity { 
int windowwidth; 
int windowheight; 
TextView bola; 
TextView bola2; 
private float x; 
private float y; 

private android.widget.RelativeLayout.LayoutParams layoutParams; 

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

    windowwidth = getWindowManager().getDefaultDisplay().getWidth(); 
    windowheight = getWindowManager().getDefaultDisplay().getHeight(); 

    bola = (TextView) findViewById(R.id.ball); 
    bola2 = (TextView) findViewById(R.id.ball2); 

    bola2.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      layoutParams = (RelativeLayout.LayoutParams) bola2 
        .getLayoutParams(); 
      switch (event.getActionMasked()) { 
      case MotionEvent.ACTION_DOWN: 
       break; 

      case MotionEvent.ACTION_MOVE: 
       int x_cord = (int) event.getRawX(); 
       int y_cord = (int) event.getRawY(); 

       if (x_cord > windowwidth) { 
        x_cord = windowwidth; 
       } 
       if (y_cord > windowheight) { 
        y_cord = windowheight; 
       } 
       layoutParams.leftMargin = x_cord - 25; 
       layoutParams.topMargin = y_cord - 75; 
       bola2.setLayoutParams(layoutParams); 
       break; 

      default: 
       break; 
      } 
      return true; 
     } 
    }); 

    bola.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      layoutParams = (RelativeLayout.LayoutParams) bola 
        .getLayoutParams(); 
      switch (event.getActionMasked()) { 
      case MotionEvent.ACTION_DOWN: 

       break; 

      case MotionEvent.ACTION_MOVE: 
       int x_cord = (int) event.getRawX(); 
       int y_cord = (int) event.getRawY(); 

       if (x_cord > windowwidth) { 
        x_cord = windowwidth; 
       } 
       if (y_cord > windowheight) { 
        y_cord = windowheight; 
       } 
       layoutParams.leftMargin = x_cord - 25; 
       layoutParams.topMargin = y_cord - 75; 
       bola.setLayoutParams(layoutParams); 
       break; 

      default: 
       break; 
      } 
      // TODO Auto-generated method stub 
      return true; 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
}} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id= "@+id/ball" 
    android:background="@drawable/bgshape" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="1" 
    tools:context=".MainActivity" /> 
<TextView 
    android:id= "@+id/ball2" 
    android:background="@drawable/bgshape" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="2" 
    tools:context=".MainActivity" 
    android:layout_x="60dp" 
    android:layout_y="20dp" 
    /> 

的bgshape.xml(對於圓)

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 

<padding 
    android:bottom="20dp" 
    android:left="25dp" 
    android:right="25dp" 
    android:top="20dp" /> 

<stroke 
    android:width="2dp" 
    android:color="#000000" /> 

<solid android:color="#ffffff" /> 

<corners 
    android:bottomLeftRadius="30dp" 
    android:bottomRightRadius="30dp" 
    android:topLeftRadius="30dp" 
    android:topRightRadius="30dp" /> 

此代碼效果很好。任何人都可以解決這個問題,以便我們可以在圈子內部添加值時,他們擊中對方?

回答

2

試試這個:

getLeft()和共達()將返回起始X,Y座標。