Commit 713264a5bc36b0686f8c7827b9fed3beb24b1c19

Authored by aarnaude
1 parent d55d5943

ajout drag n drop/bug drop mauvaise zone + manque precision drop

.idea/misc.xml
... ... @@ -24,7 +24,7 @@
24 24 </value>
25 25 </option>
26 26 </component>
27   - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
  27 + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
28 28 <output url="file://$PROJECT_DIR$/build/classes" />
29 29 </component>
30 30 <component name="ProjectType">
... ...
app/src/main/java/tonio/noa/TutorialScene1Activity.java
... ... @@ -2,7 +2,6 @@ package tonio.noa;
2 2  
3 3 import android.content.Intent;
4 4 import android.os.Bundle;
5   -import android.view.View;
6 5  
7 6 /**
8 7 * Created by tonio on 22/11/17.
... ...
app/src/main/java/tonio/noa/TutorialScene2Activity.java
1 1 package tonio.noa;
2 2  
  3 +import android.content.ClipData;
  4 +import android.os.Build;
3 5 import android.os.Bundle;
  6 +import android.view.DragEvent;
  7 +import android.view.MotionEvent;
4 8 import android.view.View;
  9 +import android.view.ViewGroup;
  10 +import android.widget.LinearLayout;
  11 +import android.widget.RelativeLayout;
  12 +import android.widget.Toast;
5 13  
6 14 /**
7 15 * Created by psyk on 09/01/18.
8 16 */
9 17  
10   -public class TutorialScene2Activity extends MyPlayActivity {
  18 +public class TutorialScene2Activity extends MyPlayActivity implements View.OnTouchListener,View.OnDragListener{
  19 +
11 20  
12 21 @Override
13 22 protected void onCreate(Bundle savedInstanceState) {
14 23  
15 24 super.onCreate(savedInstanceState);
16 25 setContentView(R.layout.tutorial2_display);
  26 +
  27 + findViewById(R.id.blue_rectangle).setOnTouchListener(this);
  28 +
  29 + findViewById(R.id.ts2l1).setOnDragListener(this);
  30 + findViewById(R.id.ts2r1).setOnDragListener(this);
  31 + }
  32 +
  33 + @Override
  34 + public boolean onTouch(View view, MotionEvent event) {
  35 + if (event.getAction() == MotionEvent.ACTION_DOWN) {
  36 + View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
  37 +
  38 + ClipData data = ClipData.newPlainText("id", view.getResources().getResourceEntryName(view.getId()));
  39 +
  40 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  41 + view.startDragAndDrop(data, shadowBuilder, view, 0);
  42 + } else {
  43 + view.startDrag(data, shadowBuilder, view, 0);
  44 + }
  45 +
  46 + view.setVisibility(View.INVISIBLE);
  47 + return true;
  48 + }
  49 + return false;
  50 + }
  51 +
  52 + @Override
  53 + public boolean onDrag(View v, DragEvent event) {
  54 + switch (event.getAction()) {
  55 + // signal for the start of a drag and drop operation
  56 + case DragEvent.ACTION_DRAG_STARTED:
  57 + // do nothing
  58 + break;
  59 +
  60 + // the drag point has entered the bounding box of the View
  61 + case DragEvent.ACTION_DRAG_ENTERED:
  62 + // do nothing
  63 + break;
  64 +
  65 + // the user has moved the drag shadow outside the bounding box of the View
  66 + case DragEvent.ACTION_DRAG_EXITED:
  67 + // do nothing
  68 + break;
  69 +
  70 + // the drag and drop operation has concluded
  71 + case DragEvent.ACTION_DRAG_ENDED:
  72 + this.next();
  73 + break;
  74 +
  75 + //drag shadow has been released,the drag point is within the bounding box of the View
  76 + case DragEvent.ACTION_DROP:
  77 + View view = (View) event.getLocalState();
  78 + // we want to make sure it is dropped only to left and right parent view
  79 + if (v.getId() == R.id.ts2l1 || v.getId() == R.id.ts2r1) {
  80 +
  81 + ViewGroup source = (ViewGroup) view.getParent();
  82 + source.removeView(view);
  83 +
  84 + RelativeLayout target = (RelativeLayout) v;
  85 + target.addView(view);
  86 +
  87 + String id = event.getClipData().getItemAt(0).getText().toString();
  88 + Toast.makeText(this, id + " dropped!", Toast.LENGTH_SHORT).show();
  89 +
  90 + bravoPage(view);
  91 + }
  92 + // make view visible as we set visibility to invisible while starting drag
  93 + view.setVisibility(View.VISIBLE);
  94 + break;
  95 + }
  96 +
  97 + return true;
17 98 }
18 99  
19 100 @Override
20 101 protected void next() {
21 102 finish();
22 103 }
  104 +
23 105 }
... ...
app/src/main/res/drawable/blue_rectangle.xml 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<shape xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:shape="rectangle">
  4 + <solid android:color="@android:color/holo_blue_light"/>
  5 + <corners android:radius="5dp"/>
  6 +</shape>
0 7 \ No newline at end of file
... ...
app/src/main/res/drawable/red_rectangle.xml 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<shape xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:shape="rectangle">
  4 + <solid android:color="@android:color/holo_red_light"/>
  5 + <corners android:radius="5dp"/>
  6 +</shape>
0 7 \ No newline at end of file
... ...
app/src/main/res/layout/tutorial2_display.xml
... ... @@ -4,20 +4,53 @@
4 4 android:layout_height="match_parent"
5 5 android:orientation="vertical">
6 6  
  7 +<LinearLayout
  8 + android:id="@+id/ts2l1"
  9 + android:layout_width="match_parent"
  10 + android:layout_height="wrap_content"
  11 + android:gravity="center"
  12 + android:orientation="vertical">
  13 +
7 14 <TextView
8 15 android:layout_width="wrap_content"
9 16 android:layout_height="wrap_content"
10   - android:layout_alignParentTop="true"
11   - android:layout_centerHorizontal="true"
12   - android:text="Tutoriel scène 2" />
  17 + android:layout_margin="5dp"
  18 + android:textSize="15sp"
  19 + android:text="Fais glisser le rectangle bleu sur le rectangle rouge." />
13 20  
14   - <Button
15   - android:id="@+id/button_id_home"
16   - android:layout_width="wrap_content"
17   - android:layout_height="wrap_content"
18   - android:layout_alignParentBottom="true"
19   - android:layout_alignParentLeft="true"
20   - android:onClick="backHome"
21   - android:text="@string/home" />
  21 + <ImageView
  22 + android:id="@+id/blue_rectangle"
  23 + android:layout_width="50dp"
  24 + android:layout_height="50dp"
  25 + android:layout_margin="50dp"
  26 + android:background="@drawable/blue_rectangle"/>
  27 +
  28 +</LinearLayout>
  29 +
  30 + <RelativeLayout
  31 + android:id="@+id/ts2r1"
  32 + android:layout_width="match_parent"
  33 + android:layout_height="match_parent"
  34 + android:layout_below="@id/ts2l1"
  35 + android:orientation="vertical">
  36 +
  37 + <ImageView
  38 + android:id="@+id/red_rectangle"
  39 + android:layout_width="50dp"
  40 + android:layout_height="50dp"
  41 + android:layout_centerInParent="true"
  42 + android:background="@drawable/red_rectangle" />
  43 +
  44 + <Button
  45 + android:id="@+id/button_id_home"
  46 + android:layout_width="wrap_content"
  47 + android:layout_height="wrap_content"
  48 + android:layout_alignParentBottom="true"
  49 + android:layout_alignParentLeft="true"
  50 + android:layout_margin="5dp"
  51 + android:onClick="backHome"
  52 + android:text="@string/home" />
  53 +
  54 + </RelativeLayout>
22 55  
23 56 </RelativeLayout>
24 57 \ No newline at end of file
... ...