java按钮随机移动_java – 使按钮移动触摸我们触摸的确切位置
我創(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 夏普电视android4.4.2,夏普电
- 下一篇: android创建avd,详解如何创建和