我对Android开发很新,我正在尝试连接通过蓝牙发送数据的操纵杆。操纵杆位于SurfaceView中,我希望能够在Activity类中的操纵杆上检索x和y坐标上的数据,以便能够通过蓝牙发送数据。 我试图在网上查找类似的问题,但无法使其工作。
这是我在SurfaceView中的onDraw方法:
protected void onDraw(Canvas canvas, float x, float y, float zx, float zy){
dx = x-zx;
dy = y-zy;
super.onDraw(canvas);
canvas.drawRGB(255, 255, 255);
canvas.drawBitmap(background, (canvas.getWidth() - canvas.getWidth() / 7) - background.getWidth() / 2, (canvas.getHeight() - canvas.getHeight() / 4) - background.getHeight() / 2, null);
canvas.drawText(Float.toString(x), 60, 60, paint1);
canvas.drawText(Float.toString(y), 60, 120, paint1);
if (x == 0 && y == 0) {
canvas.drawBitmap(ball, (canvas.getWidth()-canvas.getWidth() / 7) - ball.getWidth() / 2, (canvas.getHeight()-canvas.getHeight() / 4) - ball.getHeight() / 2, null);
}
else {
canvas.drawBitmap(ball, x - ball.getWidth() / 2, y - ball.getHeight() / 2, null);
}
}
它还运行AsyncTask:
public class MySurfaceThread extends AsyncTask<Void, Void, Void>{
SurfaceHolder mSurfaceHolder;
CustomSurfaceView cSurfaceView;
public MySurfaceThread(SurfaceHolder sh, CustomSurfaceView csv){
mSurfaceHolder = sh;
cSurfaceView = csv;
x = y = 0;
cSurfaceView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
System.out.println(x + " " + y);
calculateValues(x,y);
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
x = y = 0;
dx = dy = 0;
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
private float calculateValues(float xx, float yy){
dx = xx-zeroX;
dy = yy-zeroY;
angle = (float)Math.atan(Math.abs(dy / dx));
c = (float)Math.sqrt(dx * dx + dy * dy);
if(c > radius){
if (dx > 0 && dy > 0) { //lower right corner
xx = (float) (zeroX + radius * Math.cos(angle));
yy = (float) (zeroY + radius * Math.sin(angle));
}
else if(dx > 0 && dy < 0){ //top right corner
xx = (float) (zeroX + radius * Math.cos(angle));
yy = (float) (zeroY - radius * Math.sin(angle));
}
else if(dx < 0 && dy < 0){ //top left corner
xx = (float) (zeroX - radius * Math.cos(angle));
yy = (float) (zeroY - radius * Math.sin(angle));
}
else if(dx < 0 && dy > 0){ //lower left corner
xx = (float) (zeroX - radius * Math.cos(angle));
yy = (float) (zeroY + radius * Math.sin(angle));
}
}
else {
xx = zeroX + dx;
yy = zeroY + dy;
}
System.out.println("dx: " + dx);
System.out.println("dy: " + dy);
System.out.println(angle);
x = xx;
y = yy;
return angle;
}
});
}
这是我的活动类。
public class SendInput extends Activity {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice btDevice;
BluetoothSocket btSocket;
OutputStream os;
InputStream in;
private SensorManager mSensorManager;
private Sensor mAcc;
private int BTSTATE = 0, SensorStatus=0;
int MouseReq=0, read_values=0;
private static final UUID MY_UUID =UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String address = getIntent().getStringExtra("address").trim();
btDevice=btAdapter.getRemoteDevice(address);
BluetoothConnect.start();
}
感谢我能得到的所有帮助。
提前致谢!