2011-12-05 50 views
9

十字線をMapViewにドラッグアンドドロップしようとしています。画面にImageViewボタンがあります。タッチするとボタンが消え、新しいImageViewが表示されます。新しいImageViewはどこかにリリースするまで私の指の真中に座っているはずですが、なんらかの理由でオフセットが間違っています。私が触っている場所の右下に十字線が表示されます。Android 2.2画像をクリックしてドラッグしてドラッグする

画像は比例して動くので、問題はACTION_DOWNセクションで定義したoffset_xoffset_yであると私は信じています。次に、ACTION_UPで私の指の下の正しい座標にcreateMarker(x,y)が必要ですが、それも正しくオフセットされています。

私はそれを中心にするためにさまざまな方法を試しましたが、いくつかは他のものより優れています。私は今までマジックナンバーを使わずに動作させることができませんでした。

これはそのままですが、クリック位置を使用して画像サイズの半分を差し引いています。これは私には意味がある。全体の画像サイズを引くと正解に近づきます。私はウェブからさまざまな例を試しましたが、そのすべてはViewの場所では不正確です。

アドバイスをいただけますか?

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

ドロップすることができ、いくつかの理由のために私は私の指の下中央に十字線を維持する方法を見つけることができません。 私が知っているAbsoluteLayoutは推奨されていませんが、私はそのルートの探索を開始するつもりです。私が何かを見つけたら投稿します。 – Lea

+0

あなたの画像はタッチ位置にピボットされていますか? – Vivekanand

+1

@ダルンダ解決策は見つかりましたか?私はそれについて何か助けてください必要がありますか? –

答えて

0

このコードは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); 

すべてのタッチイベントに適用されることを確認するために、if条件の外で作成してみてください。

0

あなたはドラッグで利用できる新しいAPIを使用して、私はまだこれに対する解決策を探しています

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; 
} 
} 
関連する問題