2015-07-20 62 views
1

在這裏,我想拖放正確的圖像,即左側的字母A到右側的圖像,並將分數設置爲1。但是,在編寫下面的代碼後,所有圖像在視圖外面都會消失。而且所有的圖像都會使得分數與正確的分數一起上升。任何人都可以幫助我?如何使正確的圖像放置在各自的圖像視圖?

我對Gameactivity代碼:

img1 = (ImageView) findViewById(R.id.ic_one); 
    img2 = (ImageView) findViewById(R.id.ic_two); 
    img3 = (ImageView) findViewById(R.id.ic_three); 
    img4 = (ImageView) findViewById(R.id.ic_four); 
    imgmain = (ImageView) findViewById(R.id.ic_main); 

    img1.setImageResource(R.mipmap.a1); 
img2.setImageResource(R.mipmap.a2); 
img3.setImageResource(R.mipmap.a3); 
img4.setImageResource(R.mipmap.a4); 
imgmain.setImageResource(R.mipmap.a1); 

    //setting touch listener 

    img1.setOnTouchListener(listenTouch); 
    img2.setOnTouchListener(listenTouch); 
    img3.setOnTouchListener(listenTouch); 
    img4.setOnTouchListener(listenTouch); 
    imgmain.setOnDragListener(listenDrag); 

    display = (TextView) findViewById(R.id.txtScore); 

} 

public View.OnTouchListener listenTouch = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      //setup drag 
      ClipData data = ClipData.newPlainText("", ""); 
      View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); 
      v.startDrag(data, shadowBuilder, v, 0); 
      v.setVisibility(View.INVISIBLE); 

      return true; 
     } else { 
      return false; 
     } 
    } 
}; 
public View.OnDragListener listenDrag = new View.OnDragListener() { 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 

     switch (event.getAction()) { 
      case DragEvent.ACTION_DRAG_STARTED: 


      case DragEvent.ACTION_DRAG_ENTERED: 
       //no action necessary 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       //no action necessary 

       break; 
      case DragEvent.ACTION_DROP: 
       //handle the dragged view being dropped over a drop view 
       if(v.getId() == R.id.ic_main) { 
        View view = (View) event.getLocalState(); 
        ViewGroup viewgroup = (ViewGroup) view.getParent(); 
       viewgroup.removeView(view); 

        //change the text 
        score++; 
        display.setText("Your score is : "+score); 


        view.setVisibility(View.VISIBLE); 
       } else { 
        View view = (View) event.getLocalState(); 
        view.setVisibility(View.VISIBLE); 
        Context context = getApplicationContext(); 
        Toast.makeText(context, "You can't drop the image here", 
          Toast.LENGTH_LONG).show(); 
        break; 
       } 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       //no action necessary 
       break; 
      default: 
       break; 
     } 

     return true; 
    } 
}; 

}

我的佈局代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.example.bhuwan.firstproject.GameActivity" 
android:id="@+id/layout_game"> 

<ImageView 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:id="@+id/ic_main" 
    android:layout_alignBottom="@+id/ic_two" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_marginBottom="36dp" /> 

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/ic_one" 
    android:layout_alignParentTop="true" /> 

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/ic_two" 
    android:layout_marginTop="143dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/ic_three" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/ic_one" 
    android:layout_toEndOf="@+id/ic_one" /> 

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/ic_four" 
    android:layout_alignTop="@+id/ic_two" 
    android:layout_toRightOf="@+id/ic_two" 
    android:layout_toEndOf="@+id/ic_two" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="@string/empty" 
    android:id="@+id/txtScore" 
    android:layout_marginBottom="60dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginLeft="42dp" 
    android:layout_marginStart="42dp" /> 

+0

請更好地說明你擁有什麼。你有五個圖像我們可以看到。它們包含什麼?字母A-E的圖片?現在,如果你將A拖到D那麼會發生什麼?你沒有告訴。爲什麼兩個圖像包含相同的圖片? – greenapps

+0

我有五個圖像視圖之一是主視圖上的圖像形成其他四個圖像視圖應該放棄,在四個圖像視圖我有圖像的信件從一個 - E和主視圖我有四個圖像之一,所以我需要拖放正確的一個,錯誤的仍然照常。 @greenapps –

+0

'所有圖像在視圖外面時都會消失。 」。那是因爲你馬上讓它們在onToch中不可見。不要這樣做。 – greenapps

回答

0

你的邏輯有一些問題。我做了一些改變。請嘗試並告訴我是否更好:

img1 = (ImageView) findViewById(R.id.ic_one); 
    img2 = (ImageView) findViewById(R.id.ic_two); 
    img3 = (ImageView) findViewById(R.id.ic_three); 
    img4 = (ImageView) findViewById(R.id.ic_four); 
    imgmain = (ImageView) findViewById(R.id.ic_main); 

    img1.setImageResource(R.mipmap.a1); 
    img2.setImageResource(R.mipmap.a2); 
    img3.setImageResource(R.mipmap.a3); 
    img4.setImageResource(R.mipmap.a4); 
    imgmain.setImageResource(R.mipmap.a1); 

    //setting touch listener 

    img1.setOnTouchListener(listenTouch); 
    img2.setOnTouchListener(listenTouch); 
    img3.setOnTouchListener(listenTouch); 
    img4.setOnTouchListener(listenTouch); 
    imgmain.setOnDragListener(listenDrag); 

    display = (TextView) findViewById(R.id.txtScore); 

} 

public View.OnTouchListener listenTouch = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      //setup drag 
      ClipData data = ClipData.newPlainText("", ""); 
      View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); 
      v.startDrag(data, shadowBuilder, v, 0); 
      v.setVisibility(View.INVISIBLE); 
      img1.setOnTouchListener(null); 
      img2.setOnTouchListener(null); 
      img3.setOnTouchListener(null); 
      img4.setOnTouchListener(null); 
     } 
     return false; 
    } 
}; 
public View.OnDragListener listenDrag = new View.OnDragListener() { 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 

     switch (event.getAction()) { 
      case DragEvent.ACTION_DRAG_STARTED: 
      case DragEvent.ACTION_DRAG_ENTERED: 
      case DragEvent.ACTION_DRAG_EXITED: 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       img1.setOnTouchListener(listenDrag); 
       img2.setOnTouchListener(listenDrag); 
       img3.setOnTouchListener(listenDrag); 
       img4.setOnTouchListener(listenDrag); 
       View view = (View) event.getLocalState(); 
       view.setVisibility(View.VISIBLE); 
       if (!getResoult()) { 
        Context context = getApplicationContext(); 
        Toast.makeText(context, "You can't drop the image here", Toast.LENGTH_LONG).show(); 
       } 
       break; 
      case DragEvent.ACTION_DROP: 
       //handle the dragged view being dropped over a drop view 
        View viewCorrect = (View) event.getLocalState(); 
        ViewGroup viewgroup = (ViewGroup) viewCorrect.getParent(); 
        viewgroup.removeView(viewCorrect); 
        //change the text 
        score++; 
        display.setText("Your score is : "+score); 
       break; 
     } 

     return true; 
    } 
}; 
+0

我很抱歉,但我無法真正弄清楚「if(!getResoult())」是真的嗎? @yshahak –

+0

當查看註冊到DragEvent的事件時獲取事件DragEvent.ACTION_DROP他決定要做什麼並返回布爾值。這將在DragEvent.ACTION_DRAG_ENDED內向所有註冊到DragEvents的視圖發送,並通過調用getResults()來訪問它。在你的情況下,當ACTION_DROP永遠不會調用(這一點在你的註冊視圖之外),這意味着getResults()返回false – yshahak

+0

你當然應該發佈'getResoult()'的代碼,而不是談論它。此外,你可以解釋爲什麼你刪除觸摸監聽器。沒有意義。 – greenapps

相關問題