Computer >> Máy Tính >  >> Lập trình >> Android

Làm cách nào để lấy tọa độ tuyệt đối của một chế độ xem trong Android?

Trước khi đi vào ví dụ, chúng ta nên biết tọa độ tuyệt đối là gì. Nó có nghĩa là vị trí tuyệt đối (x, y) của một chế độ xem trên trình quản lý cửa sổ. Ví dụ này minh họa về cách lấy tọa độ tuyệt đối của một chế độ xem.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout
   xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:padding = "16dp"
   tools:context = ".MainActivity"
   android:background = "#dde4dd">
   <TextView
      android:id = "@+id/text"
      android:layout_marginLeft = "100dp"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Hello World!" />
</RelativeLayout>

Trong xml ở trên, chúng tôi đã đưa ra một Textview. khi người dùng nhấp vào textview, nó sẽ hiển thị vị trí của chế độ xem trên Toast.

Bước 3 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;

import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   TextView textView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = findViewById(R.id.text);
      textView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int[] location = new int[2];
            textView.getLocationOnScreen(location);
            Toast.makeText(MainActivity.this,"X axis is "+location[0] +"and Y axis is "+location[1],Toast.LENGTH_LONG).show();
         }
      });
   }
   public static Point getLocationOnScreen(View view) {
      int[] location = new int[2];
      view.getLocationOnScreen(location);
      return new Point(location[0], location[1]);
   }
}

Trong đoạn mã trên, khi người dùng nhấp vào textview, nó sẽ hiển thị tọa độ tuyệt đối trên màn hình. Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm cách nào để lấy tọa độ tuyệt đối của một chế độ xem trong Android?

Ở kết quả trên là màn hình ban đầu, khi người dùng click vào textview sẽ hiện ra kết quả như hình bên dưới -

Làm cách nào để lấy tọa độ tuyệt đối của một chế độ xem trong Android?