手指滑动实现连线题

 

      做个笔记      

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

手指滑动实现连线题_第1张图片

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



            

            
            
        
在适配器中必须要等页面加载之后在获取坐标,不然是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 coordList, List 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 list=new ArrayList<>();
    Map  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 right.get(j).getX()&&eY > right.get(j).getY()){
                                        if (eX  felt = new ArrayList<>();
    private static List right = new ArrayList<>();
    public static void setData(List list ,List lis2){
        string ="";
        felt = list;
        right =lis2;

    }

    public  setonclick setonclick;

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

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


}

 

你可能感兴趣的:(手指滑动实现连线题)