日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

發(fā)布時(shí)間:2025/3/15 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java按钮随机移动_java – 使按钮移动触摸我们触摸的确切位置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我創(chuàng)建了一個(gè)應(yīng)用程序,其中有一個(gè)按鈕在觸摸它時(shí)移動(dòng).

現(xiàn)在對(duì)于onTouch,我實(shí)現(xiàn)了一個(gè)不同的類.有兩個(gè)類,一個(gè)是主要的CircleMActivity.java,另一個(gè)是onTouch.

現(xiàn)在應(yīng)用程序運(yùn)行正常,但有一個(gè)問題.當(dāng)我點(diǎn)擊按鈕并移動(dòng)它時(shí)它正在移動(dòng),但按鈕和屏幕觸摸之間有一個(gè)間隙.

我想要的是將它移動(dòng)到我觸摸它的位置,而不是稍后移動(dòng)或者你可以認(rèn)為我想要在光標(biāo)位置完全移動(dòng)它.

我應(yīng)該如何實(shí)現(xiàn)它?

碼:

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

"

那我該怎么做呢?

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。