简体   繁体   English

如何跟踪屏幕上的触摸

[英]How to track a touch on the screen

I want to write a penalty shootout application, now I have set up the background and added the ball and the goalkeeper to the map.我想编写一个点球大战应用程序,现在我已经设置了背景并将球和守门员添加到地图中。 How do I make it so that when I tap on the screen, the ball follows my touch on the screen.我如何做到这一点,当我点击屏幕时,球会跟随我在屏幕上的触摸。 How to do this?这该怎么做? How to track the touch on the screen and enter the ball after my touch.如何跟踪屏幕上的触摸并在我触摸后进球。

MainActivity.kt主活动.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_FULLSCREEN
    }

activity_main.xml活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_bg"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/ball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ball"
        app:layout_editor_absoluteX="150dp"
        app:layout_editor_absoluteY="500dp"
        tools:ignore="MissingConstraints" />

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        app:layout_editor_absoluteX="125dp"
        app:layout_editor_absoluteY="300dp"
        android:src="@drawable/goalkeeper"


</androidx.constraintlayout.widget.ConstraintLayout>

Screen app屏幕应用

I found the answer to my question, you need to use onTouchListener.我找到了我的问题的答案,您需要使用 onTouchListener。 My code looks like this:我的代码如下所示:

ImageView ballView;
float xDown = 0, yDown = 0;

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getSupportActionBar().hide();


    ballView = findViewById(R.id.ball);
    ballView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getActionMasked()) {

                ///пользователь просто прикоснулся к мячу
                case MotionEvent.ACTION_DOWN:
                    xDown = motionEvent.getX();
                    yDown = motionEvent.getY();
                    break;

                ///пользователь водит по экрану
                case MotionEvent.ACTION_MOVE:
                    float movedX, movedY;
                    movedX = motionEvent.getX();
                    movedY = motionEvent.getY();

                    float distanceX = movedX - xDown;
                    float distanceY = movedY - yDown;

                    ballView.setX(ballView.getX() + distanceX);
                    ballView.setY(ballView.getY() + distanceY);

                    break;
            }


            return true;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM