2014-07-23 2 views
0

사용자는 일단 구입하면 동적으로 생성되는 건물을 구입할 수있는 Android 게임을 프로그래밍하고 있습니다. 그들이 생성 될 때, 사용자는 그들이 땅에있는 한 원하는 곳에 드래그 앤 드롭 할 수 있습니다. 하늘과 땅이 두 가지 프레임 레이아웃으로되어 있지만 하늘이 상위 25 %를 차지하고 지상이 하위 75 %를 차지합니다. 사용자가 건물을 드래그하면 사용자가 하늘 영역으로 끌어 올 때까지 필요한대로 동작합니다. 그냥 그 곳으로 돌아가는 대신에, 내가 좋아하는 것처럼 하늘 영역 뒤에서 사라집니다.끌어서 놓기 imagebutton 다른보기 뒤에있는 균열

public class ColonyHutClick implements OnTouchListener 
{ 
    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     ClipData.Item item = new ClipData.Item((CharSequence)v.getTag()); 

     String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN }; 
     ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item); 
     DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); 

     v.startDrag(data, shadowBuilder, v, 0); 
     v.setVisibility(View.INVISIBLE); 

     return false; 
    }//end onTouch function 
}//end ColonyHutClick class 
:

여기
frame = (FrameLayout) findViewById(R.id.bottomHalf); 
newColonyHut = new ImageButton(runGraphics.this); 
newColonyHut.setBackgroundResource(R.drawable.mainhut);  
newColonyHut.setTag("NewColonyHut"); 
newColonyHut.setOnTouchListener(new ColonyHutClick()); 
findViewById(R.id.bottomHalf).setOnDragListener(new ColonyHutDrag()); 
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); 
frame.addView(newColonyHut, param); 

가 ColonyHutClick 클래스 경우 :하여 ImageButton 생성하는 코드 여기

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/mars" 
    android:id="@+id/gameBackground" > 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.75" 
     android:orientation="horizontal" 
     android:id="@+id/topHalf" > 

    </FrameLayout> 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.25" 
     android:orientation="horizontal" 
     android:id="@+id/bottomHalf" > 

     <ImageButton 
      android:id="@+id/polarCapButton1" 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top|left" 
      android:background="@drawable/polarcap" /> 

     <ImageButton 
      android:id="@+id/polarCapButton2" 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top|right" 
      android:background="@drawable/polarcap" /> 

    </FrameLayout> 

</LinearLayout> 

된다 : 여기

는 상부 및 하부에 대한 XML이고

다음은 ColonyHutDrag 클래스입니다.

public class ColonyHutDrag extends Activity implements OnDragListener 
{ 

    @Override 
    public boolean onDrag(View v, DragEvent event) 
    { 
     int x = 0, y = 0; 
     switch(event.getAction()) 
     { 
      case DragEvent.ACTION_DRAG_STARTED: 
       //drag has started 
       break; 

      case DragEvent.ACTION_DRAG_ENTERED: 
       //being dragged 
       break; 

      case DragEvent.ACTION_DRAG_EXITED: 
       //stop drag 
       break; 

      case DragEvent.ACTION_DRAG_LOCATION: 
       //find drag location 
       break; 

      case DragEvent.ACTION_DROP: 
       if (v == v.findViewById(R.id.bottomHalf)) 
       { 
        //find position where dropped 
        x = (int) event.getX(); 
        y = (int) event.getY(); 


        View view = (View) event.getLocalState(); 
        ViewGroup group = (ViewGroup) view.getParent(); 
        group.removeView(view); 
        FrameLayout contain = (FrameLayout) v; 
        contain.addView(view); 
        view.setX(x - (view.getWidth()/2)); 
        view.setY(y - (view.getHeight()/2)); 
        view.setVisibility(View.VISIBLE); 
       }//end if 
       else 
       { 
        View view = (View) event.getLocalState(); 
        view.setVisibility(View.VISIBLE); 
       }//end else 
       break; 

      default: 
        break; 
     }//end switch 
     return true; 
    }//end onDrag function 
}//end ColonyHutDrag class 

사전에 도움을 주셔서 감사합니다.

답변

1

드래그 이벤트를 처리하려면 "Sky"를 설정해야합니다. 의 ACTION_DROP을 처리 할 수있는 것도이없는 드래그 객체가 View를 통해 출시

findViewById(R.id.topHalf).setOnDragListener(new ColonyHutDrag()); 

OnDragListener가 설정되어 있지 않으며, 당신은 따라서 기회를 얻을하지 않습니다 : 당신은 한 줄이 작업을 수행 할 수 있습니다 드래그 한 내용을 View에서 VISIBLE으로 재설정합니다.

+0

왜 그렇게해야합니까? 나는 건물이 그 지역에 전혀 들어갈 수 없기를 바란다. 네 말이 맞다면 그 이유를 이해하고 싶을 뿐이야. 감사합니다 – sboehnke

+0

'DragListener'없이'View'를 통해 드래그를 놓을 때, 드롭 이벤트를 처리 할 아무 것도 없기 때문에 드래그 된'View'를 다시 볼 수있는 기회가 없습니다. –

+0

당신에게 효과가 없었습니까? 방금 테스트를 완료했습니다. 새 줄은'bottomHalf'에 대한'setOnDragListener()'호출 근처에 바로 추가되어야합니다. –