0

我现在正在做一个项目。我在一个圆圈中有 2 个可拖动的 textView。当圆圈拖到另一个圆圈上时,我想在圆圈内添加这些值。我的第一个选择是获取圆的 X 和 Y,但我明白了。谁能修复我的代码?

这是代码:

主要活动

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" />

这段代码运行良好。任何人都可以解决这个问题,以便我可以在他们互相碰撞时在圆圈内添加值吗?

4

1 回答 1

2

尝试这个 :

getLeft() 和 getTop() 将返回起始 x,y 坐标。

于 2012-10-11T09:34:33.607 回答