我现在正在开展一个项目。我在一个圆圈中有2个可拖动的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" />
此代码效果很好。任何人都可以解决这个问题,以便我可以在它们相互碰撞时在圆圈内添加值吗?
答案 0 :(得分:2)
试试这个:
getLeft()和getTop()将返回起始的x,y坐标。