2011-12-05 20 views
9

Próbuję przeciągnąć i upuścić krzyżyk na MapView. Mam przycisk ImageView na ekranie. Kiedy go dotknę, przycisk zniknie i pojawi się nowy ImageView. Nowy ImageView powinien siedzieć centralnie pod moim palcem, dopóki go nie zwolnię, ale z jakiegoś powodu przesunięcie jest nieprawidłowe. Krzyżyk pojawia się z prawej strony miejsca, w którym się stykam.Android 2.2 Kliknij i przeciągnij Obraz wyśrodkowany pod dotknięciem

Obraz porusza się proporcjonalnie, więc uważam, że problem dotyczy offset_x i offset_y, które zdefiniowałem w sekcji ACTION_DOWN. Następnie, w ACTION_UP, potrzebuję createMarker(x,y) na poprawne współrzędne pod moim palcem, ale jest to również nieprawidłowe przesunięcie.

Próbowałem różnych sposobów, aby go wyśrodkować, a niektóre są lepsze od innych. Do tej pory nie byłem w stanie sprawić, by działał bez użycia magicznych liczb.

W rzeczywistości używam lokalizacji kliknięcia i odejmuję połowę rozmiaru obrazu. To ma dla mnie sens. Jest bliżej do poprawienia, jeśli odejmuję cały rozmiar obrazu. Próbowałem różnych przykładów z internetu, wszystkie one cierpią na niedokładności w lokalizacji View.

Czy możesz udzielić mi porady?

crosshair = (ImageView)findViewById(R.id.crosshair); 
frameLayout = (FrameLayout)findViewById(R.id.mapframe); 
params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
     LayoutParams.WRAP_CONTENT); 
crosshairImage = BitmapFactory.decodeResource(getResources(), R.drawable.crosshair); 
crosshair.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      dragStatus = START_DRAGGING; 

      // hide the button and grab a mobile crosshair 
      v.setVisibility(View.INVISIBLE); 
      image = new ImageView(getBaseContext()); 
      image.setImageBitmap(crosshairImage); 

      // set the image offsets to center the crosshair under my touch 
      offset_x = (int)event.getRawX() - (int)(crosshairImage.getWidth()/2) ; 
      offset_y = (int)event.getRawY() - (int)(crosshairImage.getHeight()/2) ; 

      // set the image location on the screen using padding 
      image.setPadding(offset_x, offset_y, 0, 0); 

      // add it to the screen so it shows up 
      frameLayout.addView(image, params); 
      frameLayout.invalidate(); 

      if(LOG_V) Log.v(TAG, "Pressed Crosshair"); 
      return true; 
     } else if (event.getAction() == MotionEvent.ACTION_UP) { 
      if(dragStatus == START_DRAGGING) { 
       dragStatus = STOP_DRAGGING; 

       // place a marker on this spot 
       makeMarker((int)event.getX() + offset_x, (int)event.getY() + offset_y); 

       // make the button visible again 
       v.setVisibility(View.VISIBLE); 

       // remove the mobile crosshair 
       frameLayout.removeView(image); 

       if(LOG_V) Log.v(TAG, "Dropped Crosshair"); 
       return true; 
      } 
     } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 
      if (dragStatus == START_DRAGGING) { 
       // compute how far it has moved from 'start' offset 
       int x = (int)event.getX() + offset_x; 
       int y = (int)event.getY() + offset_y; 

       // check that it's in bounds 
       int w = getWindowManager().getDefaultDisplay().getWidth(); 
       int h = getWindowManager().getDefaultDisplay().getHeight(); 
       if(x > w) 
        x = w; 
       if(y > h) 
        y = h; 

       // set the padding to change the image loc 
       image.setPadding(x, y, 0 , 0); 

       // draw it 
       image.invalidate(); 
       frameLayout.requestLayout(); 

       if(LOG_V) Log.v(TAG, "(" + offset_x + ", " + offset_y + ")"); 

       return true; 
      } 
     }    
     return false; 
    } 

}); 
+0

jestem wciąż szukają rozwiązania tego z jakiegoś powodu nie mogę znaleźć sposób, aby utrzymać celownik skoncentrowany pod moim palcem. Wiem, że AbsoluteLayout jest przestarzałe, ale mam zamiar rozpocząć odkrywanie tej trasy. Będę publikować, jeśli coś znajdę. – Lea

+0

Czy obraz jest obrócony do pozycji dotykowej? – Vivekanand

+1

@Darunda Znalazłeś rozwiązanie. Potrzebuję pomocy, proszę o tym? –

Odpowiedz

0

Napisałeś ten kod w ciągu touch_Down.

offset_x = (int)event.getRawX() - (int)(crosshairImage.getWidth()/2) ; 
offset_y = (int)event.getRawY() - (int)(crosshairImage.getHeight()/2) ; 

// set the image location on the screen using padding 
image.setPadding(offset_x, offset_y, 0, 0); 

Spróbuj napisać tekst poza warunkami if, aby się upewnić, że zostanie zastosowany do wszystkich zdarzeń dotyku.

0

Można użyć nowego API dostępne w przeciągnij i upuść

public boolean onTouch(View view, MotionEvent motionEvent) { 
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
view.startDrag(null, shadowBuilder, view, 0); 
view.setVisibility(View.INVISIBLE); 
return true; 
} else { 
return false; 
} 
} 
Powiązane problemy