DragActivity.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.xunao.effectdemo.activity;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.core.content.res.ResourcesCompat;
  4. import android.app.Activity;
  5. import android.content.ClipData;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.os.SystemClock;
  9. import android.util.Log;
  10. import android.view.DragEvent;
  11. import android.view.HapticFeedbackConstants;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.view.animation.Animation;
  16. import android.view.animation.TranslateAnimation;
  17. import android.widget.FrameLayout;
  18. import android.widget.ImageView;
  19. import android.widget.LinearLayout;
  20. import android.widget.RelativeLayout;
  21. import android.widget.TextView;
  22. import com.xunao.effectdemo.R;
  23. public class DragActivity extends Activity{
  24. private static final String RED = "RED";
  25. private String selectedContent = "";
  26. private final String correctAnswer = "选项2";
  27. private int wrongNum = 0;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_drag);
  32. TextView ll_red = findViewById(R.id.ll_red);
  33. TextView tv1 = findViewById(R.id.tv_1);
  34. TextView tv2 = findViewById(R.id.tv_2);
  35. tv1.setOnLongClickListener(v -> {
  36. selectedContent = tv1.getText().toString();
  37. // Intent intent = new Intent();
  38. // ClipData clipData = ClipData.newIntent("label", intent);
  39. View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
  40. v.startDrag(null, shadowBuilder, tv1, 0);
  41. //震动反馈
  42. v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
  43. return true;
  44. });
  45. tv2.setOnLongClickListener(v -> {
  46. selectedContent = tv2.getText().toString();
  47. // Intent intent = new Intent();
  48. // ClipData clipData = ClipData.newIntent("label", intent);
  49. View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
  50. v.startDrag(null, shadowBuilder, tv2, 0);
  51. //震动反馈
  52. v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
  53. return true;
  54. });
  55. ll_red.setOnDragListener((v, event) -> {
  56. String simpleName = v.getClass().getSimpleName();
  57. Log.w(RED, "view name:" + simpleName);
  58. int action = event.getAction();
  59. switch (action) {
  60. case DragEvent.ACTION_DRAG_STARTED:
  61. Log.i(RED, "开始拖拽");
  62. // tv1.setVisibility(View.INVISIBLE);
  63. break;
  64. case DragEvent.ACTION_DRAG_ENDED:
  65. Log.i(RED, "结束拖拽");
  66. // tv1.setVisibility(View.VISIBLE);
  67. break;
  68. case DragEvent.ACTION_DRAG_ENTERED:
  69. Log.i(RED, "拖拽的view进入监听的view时");
  70. break;
  71. case DragEvent.ACTION_DRAG_EXITED:
  72. Log.i(RED, "拖拽的view离开监听的view时");
  73. break;
  74. case DragEvent.ACTION_DRAG_LOCATION:
  75. float x = event.getX();
  76. float y = event.getY();
  77. Log.i(RED, "拖拽的view在RED中的位置:x =" + x + ",y=" + y);
  78. break;
  79. case DragEvent.ACTION_DROP:
  80. if(correctAnswer.equals(selectedContent)){
  81. Log.i(RED, "释放拖拽的view");
  82. TextView localState = (TextView) event.getLocalState();
  83. ((ViewGroup) localState.getParent()).removeView(localState);
  84. ll_red.setText(selectedContent);
  85. ll_red.setBackground(ResourcesCompat.getDrawable(getResources(),R.drawable.drag_success_bg,null));
  86. }else{
  87. wrongNum = wrongNum + 1;
  88. if(wrongNum >= 2){
  89. tv2.setBackground(ResourcesCompat.getDrawable(getResources(),R.drawable.drag_hint_bg,null));
  90. }
  91. }
  92. break;
  93. }
  94. return true;
  95. });
  96. }
  97. }