我尝试了所有我认为应该工作的一切,为什么绘图线不遵循我的触摸坐标?它不准确
请参阅下面的视频,以下是我当前的代码:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论