gpt4 book ai didi

java - 使按钮在我们触摸的确切位置触摸

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:28 25 4
gpt4 key购买 nike

我创建了一个应用程序,其中有一个触摸它时会移动的按钮。 enter image description here

现在对于 onTouch 我实现了一个不同的类。有 2 个类,一个是 ma​​in CircleMActivity.java 另一个是 onTouch。现在该应用程序运行良好,但存在一个问题。当我点击按钮并移动它时它正在移动,但按钮和屏幕触摸之间有间隙。

我想要的是稍后将它移动到我触摸它的位置而不是,或者您可以认为我想将它移动到精确的光标位置。我应该如何实现它?

代码:

CircleMActivity.java:

 public class CircleMActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circle_m);
Button b=(Button)findViewById(R.id.btn1);
MultiTouch mtb=new MultiTouch(this);
b.setOnTouchListener(mtb);
}
}

MultiTouch.java:

 public class MultiTouch implements OnTouchListener{
float mPrevX,mPrevY;
CircleMActivity cm;
public MultiTouch(CircleMActivity circleMActivity) {
// TODO Auto-generated constructor stub
cm=circleMActivity;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
float currentX,currentY;
int action=arg1.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
mPrevX = arg1.getX();
mPrevY = arg1.getY();
break;

case MotionEvent.ACTION_MOVE:

currentX = arg1.getRawX();
currentY = arg1.getRawY();

MarginLayoutParams marginParams = new MarginLayoutParams( arg0.getLayoutParams());
marginParams.setMargins((int)( currentX - mPrevX), (int)( currentY - mPrevY),0, 0);
LayoutParams layoutParams = new LinearLayout.LayoutParams(marginParams);
arg0.setLayoutParams(layoutParams);

break;
case MotionEvent.ACTION_CANCEL:
break;

case MotionEvent.ACTION_UP:

break;
}
return true;
}
}

activity_circle.xml:

  <LinearLayout

android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#000000"
android:weightSum="100">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4.95"
android:background="#fff"
android:orientation="vertical" >

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="X coord"
android:textColor="#ff00ee"
android:inputType="number" >
<requestFocus />
</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Y coord"
android:textColor="#ff00ee"
android:inputType="number" />

<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Radius"
android:textColor="#ff00ee"
android:inputType="number" />

<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textColor="#ff00ee"
android:hint="Colour"
android:inputType="number" />

</LinearLayout>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4.95"
android:background="@drawable/custom" />

"


</LinearLayout>

那我应该怎么实现呢?

最佳答案

这是一个简单的坐标数学。在你的

  case MotionEvent.ACTION_DOWN:
mPrevX = arg1.getX();
mPrevY = arg1.getY();
break;

制作

mPrevY = arg1.getY()+250;

好吧,它会走出子线性布局,但它看起来不错!

关于java - 使按钮在我们触摸的确切位置触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39391110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com