2011-07-27 4 views
2

J'ai des problèmes pour obtenir les coordonnées de la vue. J'ai trois vues personnalisées placées dans LinearLayout. Quand je touche l'un d'eux, j'ai de mauvaises coordonnées. Par exemple, la deuxième vue est placée au centre, mais getLocationOnScreen() renvoie x = 0. Ceci est impossible. Et ces coordonnées diffèrent toujours des valeurs que je peux obtenir à travers event.getX() et event.getY().Problème Android getLocationOnScreen: coordonnées erronées

S'il vous plaît, pourriez-vous me dire, quel est le problème?

Résultats (par Log.w)

: Première vue onTouch_coords: 0, 50

vue: Deuxième onTouch_coords: 0, 98

vue: Troisième onTouch_coords: 0, 146

Ce

est un fichier xml IUG:

<?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> 

Ce code source est f ou mon DragableView:

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(); 
    } 
} 

Voici le code pour l'activité:

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); 
    } 
} 

Répondre

1

this.getLocationOnScreen(this_coords); il retourne toujours l'emplacement de this View, a.k.a DragableView. method specification here..

Comme cette vue est votre point de vue principal, il serait certainement montrer x=0

event.getX() vous a donné les coordonnées x où votre effectué l'événement comme le toucher, glisser ...

Ce sont deux méthodes différentes pour deux Objets.