我尝试了所有我认为应该工作的一切,为什么绘图线不遵循我的触摸坐标?它不准确

发布于 2025-01-31 15:30:00 字数 2502 浏览 2 评论 0原文

请参阅下面的视频,以下是我当前的代码:

increate 方法:

       rootLayout = findViewById(R.id.relativemain);

此方法是我将GIF图像作为芯片的地方,我也需要帮助进入这个,因为当我添加 addrule 以更改芯片的位置时,事实证明我无法再拖动它:

private void mvb_chip(ImageView view, int a) {
    view = rootLayout.findViewById(a);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(300, 300);
    view.setLayoutParams(layoutParams);
    view.setVisibility(View.VISIBLE);
    view.setOnTouchListener(new mvb_direct.ChoiceTouchListener());

}

最后是触摸侦听器来拖动芯片!在动作内部。 A/28489989/19026761 ,我的问题是为什么绘图线与我的触摸x和y并不完全相似,请参见下面的视频

float startX;
float startY;
private final class ChoiceTouchListener implements View.OnTouchListener {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();


        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;

                startX = event.getX();
                startY = event.getY();



                break;
            case MotionEvent.ACTION_UP:
                Paint paint = new Paint();
                paint.setColor(Color.CYAN);
                paint.setAntiAlias(true);
                paint.setDither(true);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeJoin(Paint.Join.ROUND);
                paint.setStrokeCap(Paint.Cap.ROUND);
                paint.setStrokeWidth(5);

                drawbleView.getCanvas().drawLine(startX,startY,event.getRawX(),event.getRawY(),paint);
                drawbleView.invalidate();
                break;



            case MotionEvent.ACTION_POINTER_DOWN:

            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250

。 gif“ rel =“ nofollow noreferrer”>

Refer to the video below, here's my current code :

Inside of Oncreate method :

       rootLayout = findViewById(R.id.relativemain);

And this method is where I put the Gif images to act as the chips, I also need help into this one because when I add the addRule to change the location of the chip, It turns out that I can no longer drag it:

private void mvb_chip(ImageView view, int a) {
    view = rootLayout.findViewById(a);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(300, 300);
    view.setLayoutParams(layoutParams);
    view.setVisibility(View.VISIBLE);
    view.setOnTouchListener(new mvb_direct.ChoiceTouchListener());

}

And Lastly here Is the touch listener to drag the chips! Inside of the ACTION.UP is where I drag the chips and put a drawline (credits to this guys answer https://stackoverflow.com/a/28489989/19026761, and my problem is why the drawLine is not exactly similar to my touch X and Y, see the video below.

float startX;
float startY;
private final class ChoiceTouchListener implements View.OnTouchListener {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();


        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;

                startX = event.getX();
                startY = event.getY();



                break;
            case MotionEvent.ACTION_UP:
                Paint paint = new Paint();
                paint.setColor(Color.CYAN);
                paint.setAntiAlias(true);
                paint.setDither(true);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeJoin(Paint.Join.ROUND);
                paint.setStrokeCap(Paint.Cap.ROUND);
                paint.setStrokeWidth(5);

                drawbleView.getCanvas().drawLine(startX,startY,event.getRawX(),event.getRawY(),paint);
                drawbleView.invalidate();
                break;



            case MotionEvent.ACTION_POINTER_DOWN:

            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文