日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java按钮随机移动_java – 使按钮移动触摸我们触摸的确切位置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我創建了一個應用程序,其中有一個按鈕在觸摸它時移動.

現在對于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" />

"

那我該怎么做呢?

總結

以上是生活随笔為你收集整理的java按钮随机移动_java – 使按钮移动触摸我们触摸的确切位置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。