java按钮随机移动_java – 使按钮移动触摸我们触摸的确切位置

我创建了一个应用程序,其中有一个按钮在触摸它时移动.

5SxcF.png

现在对于onTouch,我实现了一个不同的类.有两个类,一个是主要的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:

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="4.95"

android:background="#fff"

android:orientation="vertical" >

android:id="@+id/editText1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="X coord"

android:textColor="#ff00ee"

android:inputType="number" >

android:id="@+id/editText2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Y coord"

android:textColor="#ff00ee"

android:inputType="number" />

android:id="@+id/editText3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Radius"

android:textColor="#ff00ee"

android:inputType="number" />

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

android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="4.95"

android:background="@drawable/custom" />

"

那我该怎么做呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值