2016-09-08 13 views
3

タッチしたボタンがあるアプリケーションを作成しました。 enter image description hereボタンを動かすタッチした正確な位置でタッチする

は今onTouchのために、私は別のclass.Thereを実装した2クラス1は、メインCircleMActivity.javaonTouchための別のあるです。 今、アプリケーションは正常に動作していますが、の問題があります。ボタンをクリックして移動すると移動しますが、ボタンと画面の接触の間には隙間があります。私が欲しいもの

は、私は後でいくつかの距離でませそれを触れたり、あなたが、私は正確なにカーソル位置それを移動することを考えることができる場所を正確にそれを移動することです。 私はそれをどのように達成するべきですか?

コード:

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> 

どのように達成する必要がありますか?

+0

あなたはVyacheslavの回答を試す必要があります。http://stackoverflow.com/a/9398861/829034 –

答えて

0

シンプルなコーディー ネイト数学です。あなたの

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

mPrevY = arg1.getY()+250;

まあそれは子供linear layoutの外に出ますが、それは格好良いしていることを確認します!

関連する問題