gpt4 book ai didi

java - 移动不适合手指的 View

转载 作者:行者123 更新时间:2023-11-30 00:58:51 25 4
gpt4 key购买 nike

我正在做一个用户可以用手指触摸移动 View 的项目。它有效但对手指位置不准确。我不是在问你有什么好的代码,而是想知道为什么我的代码使 View 不适合手指。这就像 View 有大约 20dp 的更多边距。这是代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lyRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zihadrizkyef.dragviewwithfinger.MainActivity">

<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:text="Hello World!"/>
</LinearLayout>

MainActivity.java

package com.zihadrizkyef.dragviewwithfinger;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
ViewGroup lyRoot;
TextView tvText;
LinearLayout.LayoutParams layoutParams;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

lyRoot = (ViewGroup)findViewById(R.id.lyRoot);
tvText = (TextView)findViewById(R.id.tvText);
layoutParams = (LinearLayout.LayoutParams)tvText.getLayoutParams();

lyRoot.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
tvText.setLayoutParams(layoutParams);
}
lyRoot.invalidate();

if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("mouseX:"+x+" ; mouseY:"+y);
System.out.println("viewX:"+layoutParams.leftMargin+" ; viewY:"+layoutParams.topMargin);
}

return true;
}
}

最佳答案

您应该使用拖动的距离而不是屏幕上的坐标。

layoutParams.leftMargin = layoutParams.leftMargin + deltaX;
layoutParams.topMargin = layoutParams.leftMargin + deltaY;

您可以从 Android 开发者网站获得完整的解决方案 https://developer.android.com/training/gestures/scale.html#drag
您将不得不根据您的需要对其进行调整。我稍微调整了一下。

// The ‘active pointer’ is the one currently moving our object.
private int mActivePointerId = INVALID_POINTER_ID;

@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);

final int action = MotionEventCompat.getActionMasked(ev);

switch (action) {
case MotionEvent.ACTION_DOWN: {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);

// Remember where we started (for dragging)
mLastTouchX = x;
mLastTouchY = y;
// Save the ID of this pointer (for dragging)
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
break;
}
case MotionEvent.ACTION_MOVE: {
// Find the index of the active pointer and fetch its position
final int pointerIndex =
MotionEventCompat.findPointerIndex(ev, mActivePointerId);

final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);

// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;

layoutParams.leftMargin += dx;
layoutParams.topMargin += dy;
tvText.setLayoutParams(layoutParams);
lyRoot.invalidate();

// Remember this touch position for the next move event
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);

if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
}
break;
}
}
return true;
}

关于java - 移动不适合手指的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39745111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com