From 4d5786fb9ff9a7ee71a0448c5da0809cb2e36b22 Mon Sep 17 00:00:00 2001 From: aarnaude Date: Mon, 15 Jan 2018 15:01:14 +0100 Subject: [PATCH] ajout scene 3 : drag n drop multiples --- app/src/main/AndroidManifest.xml | 10 +++++++++- app/src/main/java/tonio/noa/TutorialScene2Activity.java | 4 +++- app/src/main/java/tonio/noa/TutorialScene3Activity.java | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/src/main/res/drawable/green_rectangle.xml | 6 ++++++ app/src/main/res/layout/tutorial2_display.xml | 2 +- app/src/main/res/layout/tutorial3_display.xml | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/tonio/noa/TutorialScene3Activity.java create mode 100644 app/src/main/res/drawable/green_rectangle.xml create mode 100644 app/src/main/res/layout/tutorial3_display.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f880dfd..30c26aa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -64,10 +64,18 @@ + + + \ No newline at end of file diff --git a/app/src/main/java/tonio/noa/TutorialScene2Activity.java b/app/src/main/java/tonio/noa/TutorialScene2Activity.java index 0786e8b..683619f 100644 --- a/app/src/main/java/tonio/noa/TutorialScene2Activity.java +++ b/app/src/main/java/tonio/noa/TutorialScene2Activity.java @@ -1,6 +1,7 @@ package tonio.noa; import android.content.ClipData; +import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.view.DragEvent; @@ -68,7 +69,7 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou case DragEvent.ACTION_DROP: //handle the dragged view being dropped over a target view View view = (View) event.getLocalState(); - if(v==findViewById(R.id.red_rectangle)) { + if (v == findViewById(R.id.red_rectangle)) { //stop displaying the view where it was before it was dragged view.setVisibility(View.INVISIBLE); //view dragged item is being dropped on @@ -86,6 +87,7 @@ public class TutorialScene2Activity extends MyPlayActivity implements View.OnTou @Override protected void next() { + startActivity(new Intent(this, TutorialScene3Activity.class)); finish(); } diff --git a/app/src/main/java/tonio/noa/TutorialScene3Activity.java b/app/src/main/java/tonio/noa/TutorialScene3Activity.java new file mode 100644 index 0000000..ca14bee --- /dev/null +++ b/app/src/main/java/tonio/noa/TutorialScene3Activity.java @@ -0,0 +1,102 @@ +package tonio.noa; + +import android.content.ClipData; +import android.os.Build; +import android.os.Bundle; +import android.view.DragEvent; +import android.view.MotionEvent; +import android.view.View; +import android.widget.ImageView; + +/** + * Created by psyk on 15/01/18. + */ + +public class TutorialScene3Activity extends MyPlayActivity implements View.OnTouchListener, View.OnDragListener { + + @Override + protected void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + setContentView(R.layout.tutorial3_display); + + findViewById(R.id.blue_rectangle31).setOnTouchListener(this); + findViewById(R.id.blue_rectangle32).setOnTouchListener(this); + findViewById(R.id.red_rectangle31).setOnTouchListener(this); + findViewById(R.id.red_rectangle32).setOnTouchListener(this); + findViewById(R.id.green_rectangle31).setOnTouchListener(this); + findViewById(R.id.green_rectangle32).setOnTouchListener(this); + + findViewById(R.id.blue_rectangle31).setOnDragListener(this); + findViewById(R.id.blue_rectangle32).setOnDragListener(this); + findViewById(R.id.red_rectangle31).setOnDragListener(this); + findViewById(R.id.red_rectangle32).setOnDragListener(this); + findViewById(R.id.green_rectangle31).setOnDragListener(this); + findViewById(R.id.green_rectangle32).setOnDragListener(this); + } + + @Override + public boolean onTouch(View view, MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + + View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); + ClipData data = ClipData.newPlainText("id", view.getResources().getResourceEntryName(view.getId())); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + view.startDragAndDrop(data, shadowBuilder, view, 0); + } else { + view.startDrag(data, shadowBuilder, view, 0); + } + return true; + } else + return false; + } + + @Override + public boolean onDrag(View v, DragEvent event) { + switch (event.getAction()) { + // signal for the start of a drag and drop operation + case DragEvent.ACTION_DRAG_STARTED: + // do nothing + break; + + // the drag point has entered the bounding box of the View + case DragEvent.ACTION_DRAG_ENTERED: + // do nothing + break; + + // the user has moved the drag shadow outside the bounding box of the View + case DragEvent.ACTION_DRAG_EXITED: + // do nothing + break; + + // the drag and drop operation has concluded + case DragEvent.ACTION_DRAG_ENDED: + break; + + //drag shadow has been released,the drag point is within the bounding box of the View + case DragEvent.ACTION_DROP: + //handle the dragged view being dropped over a target view + View view = (View) event.getLocalState(); + if (v == findViewById(R.id.red_rectangle)) { + //stop displaying the view where it was before it was dragged + view.setVisibility(View.INVISIBLE); + //view dragged item is being dropped on + ImageView dropTarget = (ImageView) v; + //view being dragged and dropped + ImageView dropped = (ImageView) view; + dropTarget.setBackgroundColor(dropped.getSolidColor()); + bravoPage(v); + } + break; + } + + return true; + } + + @Override + protected void next() { + finish(); + } + +} diff --git a/app/src/main/res/drawable/green_rectangle.xml b/app/src/main/res/drawable/green_rectangle.xml new file mode 100644 index 0000000..856acd7 --- /dev/null +++ b/app/src/main/res/drawable/green_rectangle.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/tutorial2_display.xml b/app/src/main/res/layout/tutorial2_display.xml index c16f07d..634f3f8 100644 --- a/app/src/main/res/layout/tutorial2_display.xml +++ b/app/src/main/res/layout/tutorial2_display.xml @@ -34,7 +34,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" - android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_margin="5dp" android:onClick="backHome" android:text="@string/home" /> diff --git a/app/src/main/res/layout/tutorial3_display.xml b/app/src/main/res/layout/tutorial3_display.xml new file mode 100644 index 0000000..3d698dc --- /dev/null +++ b/app/src/main/res/layout/tutorial3_display.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + +