123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package com.xunao.effectdemo.activity;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.content.res.ResourcesCompat;
- import android.app.Activity;
- import android.content.ClipData;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.SystemClock;
- import android.util.Log;
- import android.view.DragEvent;
- import android.view.HapticFeedbackConstants;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.TranslateAnimation;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.xunao.effectdemo.R;
- public class DragActivity extends Activity{
- private static final String RED = "RED";
- private String selectedContent = "";
- private final String correctAnswer = "选项2";
- private int wrongNum = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_drag);
- TextView ll_red = findViewById(R.id.ll_red);
- TextView tv1 = findViewById(R.id.tv_1);
- TextView tv2 = findViewById(R.id.tv_2);
- tv1.setOnLongClickListener(v -> {
- selectedContent = tv1.getText().toString();
- // Intent intent = new Intent();
- // ClipData clipData = ClipData.newIntent("label", intent);
- View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
- v.startDrag(null, shadowBuilder, tv1, 0);
- //震动反馈
- v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
- return true;
- });
- tv2.setOnLongClickListener(v -> {
- selectedContent = tv2.getText().toString();
- // Intent intent = new Intent();
- // ClipData clipData = ClipData.newIntent("label", intent);
- View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
- v.startDrag(null, shadowBuilder, tv2, 0);
- //震动反馈
- v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
- return true;
- });
- ll_red.setOnDragListener((v, event) -> {
- String simpleName = v.getClass().getSimpleName();
- Log.w(RED, "view name:" + simpleName);
- int action = event.getAction();
- switch (action) {
- case DragEvent.ACTION_DRAG_STARTED:
- Log.i(RED, "开始拖拽");
- // tv1.setVisibility(View.INVISIBLE);
- break;
- case DragEvent.ACTION_DRAG_ENDED:
- Log.i(RED, "结束拖拽");
- // tv1.setVisibility(View.VISIBLE);
- break;
- case DragEvent.ACTION_DRAG_ENTERED:
- Log.i(RED, "拖拽的view进入监听的view时");
- break;
- case DragEvent.ACTION_DRAG_EXITED:
- Log.i(RED, "拖拽的view离开监听的view时");
- break;
- case DragEvent.ACTION_DRAG_LOCATION:
- float x = event.getX();
- float y = event.getY();
- Log.i(RED, "拖拽的view在RED中的位置:x =" + x + ",y=" + y);
- break;
- case DragEvent.ACTION_DROP:
- if(correctAnswer.equals(selectedContent)){
- Log.i(RED, "释放拖拽的view");
- TextView localState = (TextView) event.getLocalState();
- ((ViewGroup) localState.getParent()).removeView(localState);
- ll_red.setText(selectedContent);
- ll_red.setBackground(ResourcesCompat.getDrawable(getResources(),R.drawable.drag_success_bg,null));
- }else{
- wrongNum = wrongNum + 1;
- if(wrongNum >= 2){
- tv2.setBackground(ResourcesCompat.getDrawable(getResources(),R.drawable.drag_hint_bg,null));
- }
- }
- break;
- }
- return true;
- });
- }
- }
|