安卓 获取屏幕坐标(点击屏幕获取坐标)

时间:2024-03-15 18:43:35

安卓 获取屏幕坐标(点击屏幕获取坐标)

安卓 获取屏幕坐标(点击屏幕获取坐标)

安卓 获取屏幕坐标(点击屏幕获取坐标)

工具下载:安卓 获取屏幕坐标(点击屏幕获取坐标)


实现原理:创建一个背景透明的Activity, 点击屏幕时获取坐标信息并显示。在悬浮窗中调用该Activity,可以获取所有界面的坐标信息。


package sc.tool.screen;

import sc.tool.component.ActivityComponent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.View;


/** 获取屏幕点击处的坐标 */
public class GetClickPostion extends ActivityComponent
{
	/** 显示当前Activity */
	public static void Show(Context context)
	{
//		context.finish();	// 关闭context对应的Activity
		
//		final Class cls = GetClickPostion.class;
//		new Handler(Looper.getMainLooper()).post(new Runnable()
//		{
//			@Override
//			public void run()
//			{
//				
//			}
//		});
		
		Intent intent = new Intent(context, GetClickPostion.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
		
		
		// context.startActivityForResult(intent, CODE_ClickPostion);
	}
	
	// ----------------
	
	@Override
	public void Init(Bundle savedInstanceState)
	{
		this.setContentView("screen_postion");
		Click("buttonReset");
	}
	
	@Override
	public void Click(String viewId)
	{
		if (viewId.equals("buttonReset"))
		{
			this.LinearLayout("linear1").setVisibility(View.INVISIBLE);
			this.LinearLayout("linear2").setVisibility(View.INVISIBLE);
			isSet = false;
		}
		else if (viewId.equals("buttonFinish"))
		{
			Intent data = new Intent();
			data.putExtra("x", x + "");
			data.putExtra("y", y + "");
			this.setResult(CODE_ClickPostion, data);	// 返回选取的坐标
			this.finish();
		}
	}
	
	public boolean dispatchTouchEvent(MotionEvent event)
	{
		switch (event.getAction())
		{
			case MotionEvent.ACTION_DOWN:
				setPosition((int) event.getX(), (int) event.getY());
				break;
			
			case MotionEvent.ACTION_UP:
				break;
		}
		return super.dispatchTouchEvent(event);
		
	}
	
	boolean isSet = false;
	int x;
	int y;
	public static final int CODE_ClickPostion = 50001;
	
	public void setPosition(int x, int y)
	{
		if (!isSet)
		{
			isSet = true;
			LinearLayout("linear1").setVisibility(View.VISIBLE);
			LinearLayout("linear2").setVisibility(View.VISIBLE);
			this.EditText("editText1").setText("" + x + ", " + y);
		}
	}
	
}

AndroidManifest.xml添加配置:

<activity
            android:name="sc.tool.screen.GetClickPostion"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/AppTheme2" />

styles.xml配置:

<resources>

    <style name="AppTheme2" parent="android:Theme.Translucent.NoTitleBar.Fullscreen">
        <item name="android:windowFullscreen">false</item>
    </style>
    
</resources>

完整源码下载