手指滑动实现连线题

      做个笔记      

      不废话,先看效果是不是你想要的。

 recyclerView实现的布局 ,然后在布局上面覆盖了个VIew

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/dp_100">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:padding="@dimen/dp_20"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <haozhong.com.diandu.activity.view.DrawView
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
            
        </RelativeLayout>
在适配器中必须要等页面加载之后在获取坐标,不然是0,分表获取到左边和右边数据的位置,获取的是左上角和右下角,这样我们就可以确定大小了。

holder.itemView.post(new Runnable() {
            @Override
            public void run() {
                int[] location = new  int[2] ;
                int[] location2 = new  int[2] ;
                holder.layout1.getLocationOnScreen(location);
                holder.layout2.getLocationOnScreen(location2);

                int width = holder.layout1.getWidth();
                int height = holder.layout1.getHeight();
                Bean bean = new Bean(location[0],location[1],location[0]+width,location[1]+height,false);


                int width2 = holder.layout2.getWidth();
                int height2 = holder.layout2.getHeight();
                Bean bean2 = new Bean(location2[0],location2[1],location2[0]+width2,location2[1]+height2,false);

                coordList.add(bean);
                coordList2.add(bean2);

                setonclick.onclick(coordList,coordList2);
            }
        });

把获取的坐标传入DrawView

wAdapter.setonClickLiener(new WAdapter.setonclick() {
                @Override
                public void onclick(List<Bean> coordList, List<Bean> coordList2) {
                    felt = coordList;
                    right = coordList2;
                    DrawView.setData(felt,right);
                }
            });

 DrawView类   差不多就这么多了,有不足的地方欢迎大家补充

package haozhong.com.diandu.activity.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import haozhong.com.diandu.activity.homework.adapter.Bean;
import haozhong.com.diandu.common.util.LogUtils;


public class DrawView extends View {

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);



        canvas.drawLine(startX,startY,endX,endY,paint);
        for (int i = 0; i < list.size(); i++) {
            float [] data=list.get(i);
            canvas.drawLine(data[ 0],data[1],data[2],data[3],paint);
        }
    }

    float startX = 0;
    float startY = 0;

    float endX = 0;
    float endY = 0;


    float sX = 0;
    float sY = 0;

    float eX = 0;
    float eY = 0;

    ArrayList<float[]> list=new ArrayList<>();
    Map <String,String> map = new HashMap();
    public static String string= "";
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();
                sX = event.getRawX();
                sY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                endX = event.getX();
                endY = event.getY();
                eX = event.getRawX();
                eY = event.getRawY();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                float [] data={startX,startY,endX,endY};


                for (int i = 0; i < felt .size() ; i++) {
                    if (!felt.get(i).isaBoolean()){
                    if (sX>felt.get(i).getX()&&sY>felt.get(i).getY()){
                        if (sX < felt.get(i).getX2()&&sY <felt.get(i).getY2()){

                            for (int j = 0; j <right.size()  ; j++) {
                                if (!right.get(j).isaBoolean()){
                                    if (eX>right.get(j).getX()&&eY > right.get(j).getY()){
                                        if (eX <right.get(j).getX2()&&eY <right.get(j).getY2()){
                                            if (!felt.get(i).isaBoolean() && !right.get(j).isaBoolean()){
                                                right.get(j).setaBoolean(true);
                                                felt.get(i).setaBoolean(true);
                                                list.add(data);

                                                
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    }
                }
                startY = 0;
                startX = 0;
                endY = 0;
                endX = 0;
                invalidate();

                break;
        }

        return true;

    }

    private static List<Bean> felt = new ArrayList<>();
    private static List<Bean> right = new ArrayList<>();
    public static void setData(List<Bean> list ,List<Bean> lis2){
        string ="";
        felt = list;
        right =lis2;

    }

    public  setonclick setonclick;

    public void setonclickLiener(setonclick setonclick){
        this.setonclick = setonclick;
    }

    public interface setonclick{
        void click(String string);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值