2011-12-05 23 views
9

Bir crosshair'i MapView ürününe sürükleyip bırakmaya çalışıyorum. Ekranda bir ImageView düğmem var. Ona dokunduğumda, düğme kaybolur ve yeni bir ImageView görünür. Yeni ImageView'un, parmağımın altında bir yerde serbest kalana kadar ortalanmış olması gerekiyordu, ancak bir nedenden dolayı sapma yanlış. Artı işareti, dokunduğum yerin sağında görünür. Sorun I ACTION_DOWN bölümünde tanımlayan offset_x ve offset_y ile göre deAndroid 2.2 Dokunarak Görüntü Merkezileştirilmiş Görüntüye Tıklayın ve Tıklayın

görüntü orantılı hareket etmez. Sonra, ACTION_UP içinde parmağımın altındaki doğru koordinatlarda createMarker(x,y)'a ihtiyacım var, ancak bu da yanlış bir şekilde ofset.

Bunu merkezi hale getirmenin çeşitli yollarını denedim ve bazıları diğerlerinden daha iyi. Şimdiye kadar büyü numaralarını kullanmadan çalışmayı başaramadım.

Bu nedenle, tıklama konumunu kullanıyorum ve resmin boyutunu yarıya indiriyorum. Bu bana mantıklı geliyor. Tüm resim boyutunu çıkarırsam düzeltmek daha yakındır. Web'den çeşitli örnekler denedim, hepsi View konumu ile yanlışlıklardan muzdarip.

Bana biraz öneride bulunabilir misiniz?

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

bırakın, nedense benim parmak altında merkezli crosshair tutmak için bir yol bulamıyorum. AbsoluteLayout'un kullanımdan kaldırıldığını biliyorum ancak bu rotayı keşfetmeye başlayacağım. Bir şey bulursam gönderirim. – Lea

+0

Görüntünüzün dokunma konumuna getirildiğinden mi – Vivekanand

+1

@Darunda Çözüm buldunuz mu? Bu konuda biraz yardıma ihtiyacım var mı? –

cevap

0

Sen touch_Down içinde bu kod yazdım.

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

Koşulların dışında yazmayı deneyin, yalnızca tüm dokunma olaylarına uygulandığından emin olun.

0

Sen sürükle bulunan yeni API kullanmak ve hala bu çözüm arıyorum

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; 
} 
} 
İlgili konular