2011-07-27 18 views
2

私はビューの座標を取得することに問題があります。 LinearLayoutには3つのカスタムビューが配置されています。私がそれらのいずれかに触れると、私は座標が間違っています。たとえば、2番目のビューは中央に配置されますが、getLocationOnScreen()はx = 0を返します。これは不可能です。 この座標は、event.getX()とevent.getY()を介して取得できる値とは常に異なります。Android getLocationOnScreen問題:座標が間違っています

教えてください。問題は何ですか? (Log.wスルー)

結果

まず図:onTouch_coords:0、50

第2のビュー:onTouch_coords:0、98

第ビュー:onTouch_coords:0、146

これはgui xmlファイルです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <com.remaller.android.DragAndDropBasicsInheriting.DragableView 
     android:id="@+id/DragableView1" 
     android:layout_height="wrap_content" 
     android:src = "@drawable/icon" android:layout_width="wrap_content"/> 

    <com.remaller.android.DragAndDropBasicsInheriting.DragableView 
     android:id="@+id/DragableView2" 
     android:layout_height="wrap_content" 
     android:src = "@drawable/icon" android:layout_width="match_parent"/> 

    <com.remaller.android.DragAndDropBasicsInheriting.DragableView 
     android:id="@+id/DragableView3" 
     android:src = "@drawable/icon" android:layout_width="wrap_content" android:layout_height="match_parent"/> 

</LinearLayout> 

これはソースコードですまたは私のDragableView:それは常にDragableView a.k.a、thisビューの位置を返します

public class DragAndDropBasicsInheritingActivity extends Activity 
{ 
    public static final int CREATE = 0x00000001; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.draganddropbasicsinheritingactivity_gui); 
    } 
} 

答えて

1

this.getLocationOnScreen(this_coords);

public class DragableView extends ImageView 
{ 
    private void init() 
    { 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) 
    { 
     int action = event.getAction(); 

     int this_coords[] = {0,0}; 
     this.getLocationOnScreen(this_coords); 

     Log.w("daxh","onTouch_coords: "+this_coords[0]+", "+this_coords[1]); 

     switch(action) 
     { 
      case MotionEvent.ACTION_DOWN: 

      break; 

      case MotionEvent.ACTION_MOVE: 

      break; 

      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_CANCEL: 
      default: 

      break; 
     } 

     return super.onTouchEvent(event); 
    } 

    public DragableView(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public DragableView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(); 
    } 

    public DragableView(Context context) 
    { 
     super(context); 
     init(); 
    } 
} 

これは、活動のためのコードです。 method specification here.

このビューは、メインビューであるとして、それは間違いなくx=0

event.getX()は...あなたに

を自分のタッチ、ドラッグなどのイベントを行った場所の座標xを与えた二から二種類の方法だことを示しているでしょうオブジェクト。

関連する問題