UI

Android多个ImageView拖拽互换图片

2022-06-06  本文已影响0人  沫沫么么哒Die

一、代码

layout_one.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

       <LinearLayout
          android:id="@+id/release_view_two_1_layout_1"
          android:layout_width="match_parent"
          android:layout_height="245dp">

            <ImageView
                 android:id="@+id/release_iv1_view_two_1"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:scaleType="centerCrop"
                 app:rRadius="4dp"
                 android:src="@mipmap/defualt" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/release_view_two_1_layout_2"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="150dp"
            android:layout_marginEnd="10dp">

               <ImageView
                  android:id="@+id/release_iv2_view_two_1"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  app:rRadius="4dp"
                  android:scaleType="centerCrop"
                  android:src="@mipmap/defualt" />
          </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

注意,两个ImageView要分别用Layout单独装起来

ViewOne.java

public class ViewOne extends ConstraintLayout {
 private ArrayList<String> list;
 public PosterStyleView(@NonNull Context context, ArrayList<String> list) {
        super(context);
        this.list = list;
        initView(context);
    }

private void initView(Context context){
  ...
  R.layout.layout_one
  ...
  ImageView iv1= findViewById(R.id.release_iv1_view_two_1);
  ImageView  iv2 = findViewById(R.id.release_iv2_view_two_1);

  LinearLayout  layout1 = findViewById(R.id.release_view_two_1_layout_1);
  LinearLayout  layout2 = findViewById(R.id.release_view_two_1_layout_2);

  //添加ImageView的触摸监听和各自父布局的拖拽监听
  iv1.setOnTouchListener(ImgUtils.MyTouchEvent);
  layout1.setOnDragListener(ImgUtils.MyDragListener);

  iv2..setOnTouchListener(ImgUtils.MyTouchEvent);
  layout2.setOnDragListener(ImgUtils.MyDragListener);

  iv1.setTag(list.get(0));
  Glide.with(context).load(list.get(0)).apply(new RequestOptions().override(800, 800)).dontAnimate().into(iv1);
  iv2.setTag(list.get(1));
  Glide.with(context).load(list.get(1)).apply(new RequestOptions().override(800, 800)).dontAnimate().into(iv2);

private void refresh() {
  Glide.with(context).load((String)iv1.getTag()).apply(new RequestOptions().override(800, 800)).dontAnimate().into(iv1);
  Glide.with(context).load((String)iv2.getTag()).apply(new RequestOptions().override(800, 800)).dontAnimate().into(iv2);
}
}

ImgUtils.java

import android.content.ClipData;
import android.os.Build;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;
import org.greenrobot.eventbus.EventBus;

public class ImgUtils {

    public interface onTouchListener {
        void touch();
    }

    public static onTouchListener mOnTouchListener;

    public static void setOnTouchListener(onTouchListener onTouchListener) {
        mOnTouchListener = onTouchListener;
    }

    private static long lastTime;
    public static View.OnTouchListener MyTouchEvent = new View.OnTouchListener() {

        private float x1;
        private float y1;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (mOnTouchListener != null) {
                mOnTouchListener.touch();
            }


            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                x1 = event.getX();
                y1 = event.getY();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                long time = System.currentTimeMillis();
                if ((time - lastTime) < 500) { // 间隔小于500毫秒,视为点击事件
                    return false;
                }
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                float x2 = event.getX();
                float y2 = event.getY();
                if (y1 - y2 > 2 || y2 - y1 > 2 || x1 - x2 > 2 || x2 - x1 > 2) {//滑动一定距离
                    long lastTime1 = System.currentTimeMillis();
                    lastTime = lastTime1;
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                    v.startDrag(data, shadowBuilder, v, 0);
                    if ((lastTime1 - lastTime) < 200) {
                        v.setAlpha((float) 1.0);
                        return false;
                    }
                    v.setAlpha((float) 0.5);
                }
            }
            return false;
        }
    };

    public static View.OnDragListener MyDragListener = new View.OnDragListener() {

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public boolean onDrag(View v, DragEvent event) {
            View visitorView = (View) event.getLocalState();
            if (visitorView == null) return true;
            ViewGroup visitorOwner = (ViewGroup) visitorView.getParent();
            ViewGroup visitedOwner = (ViewGroup) v;
            View visitedImage = visitedOwner.getChildAt(0);
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    v.setAlpha((float) 0.7);
                    break;
                case DragEvent.ACTION_DROP:
                    visitedImage.setAlpha((float) 1.0);
                    if (visitorOwner != visitedOwner) {
                        visitedOwner.removeView(visitedImage);
                        visitorOwner.removeView(visitorView);
                        visitorOwner.addView(visitedImage);
                        LinearLayout container = (LinearLayout) v;
                        container.addView(visitorView);
                        EventBus.getDefault().post(new EventView());
                    } else {
//                        visitedImage.performClick(); // 如果归复原位  响应点击事件
                    }
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    v.setAlpha((float) 1.0);
                    visitedImage.setAlpha((float) 1.0);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setAlpha((float) 1.0);
                    break;
            }
            return true;
        }
    };
}


Activity.java

onCreate(...){
    EventBus.getDefault().register(this);
 }

private void setView(){
      ViewOne viewone = new ViewOne(this,list);
      frameLayout.removeAllViews();
      frameLayout.addView(viewone);
}

    @Subscribe
    public void onMessageEvent(EventView event) {
        if(viewone !=null){
        viewone.refresh();
        }

    @Override
    protected void onDestroy() {
         if (EventBus.getDefault().isRegistered(this)) {
              EventBus.getDefault().unregister(this);
          }
    }

上一篇下一篇

猜你喜欢

热点阅读