MotionEvent.ACTION_MOVE 触发事件刷新频率太低

Android 码拜 8年前 (2016-05-19) 2995次浏览

需求:获取手指触碰屏幕过程中实时的向量,方向和距离。并发送给服务端。
思路:手指在屏幕移动 触发 MotionEvent.ACTION_MOVE 事件,获取当前‘瞬间’坐标,并与上个‘瞬间’的坐标计算,得到向量。
问题:switch  MotionEvent.ACTION_MOVE 时,频率太低,手指在屏幕上移动大约400个dp时,numb才+到10,即只触发了10次,获取向量不够实时,怎么样才能提高?

package org.backtea.test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {

	DatagramSocket socket = null;
	InetAddress serverAddress = null; 

	int downX = 0;
	int downY = 0;

	int numb = 0;

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

		// 强制主线程socket
		if (android.os.Build.VERSION.SDK_INT > 9) {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		}

		try {
			socket = new  DatagramSocket (8084);
			serverAddress = InetAddress.getByName("192.168.1.113");
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}




		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub



		EditText etX = (EditText) findViewById(R.id.editTextX);
		EditText etY = (EditText) findViewById(R.id.editTextY);
		EditText etHow = (EditText) findViewById(R.id.editTextHow);

		switch(event.getAction()){

			// 按下
			case MotionEvent.ACTION_DOWN:

				downX = (int) event.getX();

				downY = (int) event.getY();

				numb = 0;

			// 移动
			case MotionEvent.ACTION_MOVE:

				numb++; System.out.println(numb);

				int thisX = (int) event.getX();

				int thisY = (int) event.getY();

				int moveX = downX-thisX;

				int moveY = downY-thisY;

				etX.setText(thisX+"");

				etY.setText(thisY+"");

				etHow.setText("touch");

				if(moveX!=0&&moveY!=0){

					String str = moveX+"~"+moveY;

					byte data[] = str.getBytes();

					DatagramPacket  packages = new DatagramPacket (data , data.length , serverAddress , 8084);


					try {
						socket.send(packages);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				downX = thisX;

				downY = thisY;



		}

		return super.onTouchEvent(event);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		public PlaceholderFragment() {
		}
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}
}
解决方案

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明MotionEvent.ACTION_MOVE 触发事件刷新频率太低
喜欢 (0)
[1034331897@qq.com]
分享 (0)