parent
bc6326d7e0
commit
4899a76909
@ -0,0 +1,240 @@
|
|||||||
|
package wificar;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.Paint;
|
||||||
|
import android.graphics.RectF;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
public class RockerView extends View{
|
||||||
|
Paint backPaint = new Paint(); // 背景画笔
|
||||||
|
Paint bubblePaint = new Paint(); // 气泡画笔
|
||||||
|
Paint rectfPaint = new Paint();
|
||||||
|
float bubbleX = 250, bubbleY = 300; // 定义气泡的位置
|
||||||
|
float backX = 250, backY = 300; // 背景圆的位置
|
||||||
|
int radiusBack = 200, radiusBubble = 100; // 定义背景圆以及气泡的半径
|
||||||
|
RectF mRectF = new RectF(backX-radiusBack,backY-radiusBack,backX+radiusBack,backY+radiusBack);
|
||||||
|
Context mContext;
|
||||||
|
OutputStream socketWriter;
|
||||||
|
String status = "STOP";
|
||||||
|
public RockerView(Context context, @Nullable AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
this.mContext = context;
|
||||||
|
}
|
||||||
|
private void initPaint() {
|
||||||
|
backPaint.setAntiAlias(true);
|
||||||
|
backPaint.setColor(Color.parseColor("blue"));
|
||||||
|
|
||||||
|
bubblePaint.setAntiAlias(true);
|
||||||
|
bubblePaint.setColor(Color.parseColor("red"));
|
||||||
|
|
||||||
|
rectfPaint.setAntiAlias(true);
|
||||||
|
rectfPaint.setColor(Color.parseColor("red"));
|
||||||
|
rectfPaint.setAlpha(144);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void onDraw(Canvas canvas) {
|
||||||
|
super.onDraw(canvas);
|
||||||
|
initPaint();
|
||||||
|
canvas.drawCircle(backX, backY, radiusBack, backPaint);
|
||||||
|
|
||||||
|
switch (status) {
|
||||||
|
case "GO":
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
try {
|
||||||
|
socketWriter.write(new byte[]{(byte) 0xff, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0xff});
|
||||||
|
socketWriter.flush();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
canvas.drawArc(mRectF, -45, -90, true, rectfPaint);
|
||||||
|
break;
|
||||||
|
case "RETURN":
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
try {
|
||||||
|
socketWriter.write(new byte[]{(byte) 0xff, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0xff});
|
||||||
|
socketWriter.flush();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
canvas.drawArc(mRectF, 45, 90, true, rectfPaint);
|
||||||
|
break;
|
||||||
|
case "LEFT":
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
try {
|
||||||
|
socketWriter.write(new byte[]{(byte) 0xff, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0xff});
|
||||||
|
socketWriter.flush();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
canvas.drawArc(mRectF, 135, 90, true, rectfPaint);
|
||||||
|
break;
|
||||||
|
case "RIGHT":
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
try {
|
||||||
|
socketWriter.write(new byte[]{(byte) 0xff, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0xff});
|
||||||
|
socketWriter.flush();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
canvas.drawArc(mRectF, -45, 90, true, rectfPaint);
|
||||||
|
break;
|
||||||
|
case "STOP":
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
try {
|
||||||
|
socketWriter.write(new byte[]{(byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff});
|
||||||
|
socketWriter.flush();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
rectfPaint.setAlpha(0);
|
||||||
|
canvas.drawArc(mRectF, -90, 360, true, rectfPaint);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.drawCircle(bubbleX, bubbleY, radiusBubble, bubblePaint);
|
||||||
|
}
|
||||||
|
private float getDistance(float x1, float y1, float x2, float y2) {
|
||||||
|
float dis;
|
||||||
|
dis = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
|
||||||
|
return dis;
|
||||||
|
}
|
||||||
|
private void getOrientation(float x,float y){
|
||||||
|
if (y<backY&&(x<backX+backX*0.707&&x>backY-backY*0.707)){
|
||||||
|
status = "GO";
|
||||||
|
}else if (x>backX&&(y<backY+backY*0.707&&y>backY-backY*0.707)){
|
||||||
|
status = "RIGHT";
|
||||||
|
}else if (y>backY&&(x<backX+backX*0.707&&x>backY-backY*0.707)){
|
||||||
|
status = "RETURN";
|
||||||
|
}else if (x<backX&&(y<backY+backY*0.707&&y>backY-backY*0.707)){
|
||||||
|
status = "LEFT";
|
||||||
|
}else {
|
||||||
|
status = "STOP";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
private float[] getXY(float x1, float y1, float x2, float y2, float dis) {
|
||||||
|
float[] xAndy = new float[2];
|
||||||
|
float scaleDis;
|
||||||
|
float xDis;
|
||||||
|
float yDis;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 表示在第一象限之内
|
||||||
|
*/
|
||||||
|
if (x1 > x2 && y1 < y2) {
|
||||||
|
scaleDis = radiusBack / dis;
|
||||||
|
xDis = Math.abs(x1 - x2);
|
||||||
|
yDis = Math.abs(y1 - y2);
|
||||||
|
xAndy[0] = x2 + xDis * scaleDis;
|
||||||
|
xAndy[1] = y2 - yDis * scaleDis;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 表示在第二象限之内
|
||||||
|
*/
|
||||||
|
else if (x1 < x2 && y1 < y2) {
|
||||||
|
scaleDis = radiusBack / dis;
|
||||||
|
xDis = Math.abs(x1 - x2);
|
||||||
|
yDis = Math.abs(y1 - y2);
|
||||||
|
xAndy[0] = x2 - xDis * scaleDis;
|
||||||
|
xAndy[1] = y2 - yDis * scaleDis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*表示在第三象限之内
|
||||||
|
*/
|
||||||
|
else if (x1 < x2 && y1 > y2) {
|
||||||
|
scaleDis = radiusBack / dis;
|
||||||
|
xDis = Math.abs(x1 - x2);
|
||||||
|
yDis = Math.abs(y1 - y2);
|
||||||
|
xAndy[0] = x2 - xDis * scaleDis;
|
||||||
|
xAndy[1] = y2 + yDis * scaleDis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 表示在第四象限之内
|
||||||
|
*/
|
||||||
|
else if (x1 > x2 && y1 > y2) {
|
||||||
|
scaleDis = radiusBack / dis;
|
||||||
|
xDis = Math.abs(x1 - x2);
|
||||||
|
yDis = Math.abs(y1 - y2);
|
||||||
|
xAndy[0] = x2 + xDis * scaleDis;
|
||||||
|
xAndy[1] = y2 + yDis * scaleDis;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 角度为零度
|
||||||
|
*/
|
||||||
|
else if (x1 > x2 && y1 == y2) {
|
||||||
|
xAndy[0] = x2 + radiusBack;
|
||||||
|
xAndy[1] = y2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 角度为90度
|
||||||
|
*/
|
||||||
|
else if (x1 == x2 && y1 < y2) {
|
||||||
|
xAndy[0] = x2;
|
||||||
|
xAndy[1] = y2 - radiusBack;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 角度为180度
|
||||||
|
*/
|
||||||
|
else if (x1 < x2 && y1 == y2) {
|
||||||
|
xAndy[0] = x2 - radiusBack;
|
||||||
|
xAndy[1] = y2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 表示为270度
|
||||||
|
*/
|
||||||
|
else if (x1 == x2 && y1 > y2) {
|
||||||
|
xAndy[0] = x2;
|
||||||
|
xAndy[1] = y2 + radiusBack;
|
||||||
|
}
|
||||||
|
return xAndy;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
|
switch (event.getAction()) {
|
||||||
|
case MotionEvent.ACTION_MOVE:
|
||||||
|
float x = (int) event.getX();
|
||||||
|
float y = (int) event.getY();
|
||||||
|
|
||||||
|
if (getDistance(x, y, backX, backY) < radiusBack) {
|
||||||
|
bubbleX = x;
|
||||||
|
bubbleY = y;
|
||||||
|
} else if (getDistance(x, y, backX, backY) >= radiusBack) {
|
||||||
|
float xAndy[];
|
||||||
|
xAndy = getXY(x, y, backX, backY, getDistance(x, y, backX, backY));
|
||||||
|
bubbleX = xAndy[0];
|
||||||
|
bubbleY = xAndy[1];
|
||||||
|
getOrientation(x,y);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MotionEvent.ACTION_UP:
|
||||||
|
bubbleX = backX;
|
||||||
|
bubbleY = backY;
|
||||||
|
status = "STOP";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
invalidate();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
>
|
||||||
|
<corners
|
||||||
|
android:radius="100dp"
|
||||||
|
/>
|
||||||
|
<solid
|
||||||
|
android:color="#1EFFFFFF"
|
||||||
|
/>
|
||||||
|
<size
|
||||||
|
android:width="100dp"
|
||||||
|
android:height="100dp"
|
||||||
|
/>
|
||||||
|
</shape>
|
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<shape
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
>
|
||||||
|
<corners
|
||||||
|
android:radius="100dp"
|
||||||
|
/>
|
||||||
|
<solid
|
||||||
|
android:color="#6BFFFFFF"
|
||||||
|
/>
|
||||||
|
<size
|
||||||
|
android:width="50dp"
|
||||||
|
android:height="50dp"
|
||||||
|
/>
|
||||||
|
</shape>
|
Loading…
Reference in new issue