更新摇杆功能

chenghonghao_branch
Marcus 2 years ago
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>

@ -1,9 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<wificar.RockerView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="200dp"
android:layout_marginTop="200dp"
android:layout_marginEnd="200dp"
android:layout_marginBottom="200dp"
android:visibility="visible"
tools:visibility="visible" />
<wificar.MySurfaceView
android:id="@+id/mySurfaceViewVideo"
android:layout_width="match_parent"
@ -31,13 +42,14 @@
android:layout_width="100dip"
android:text="前"
/>
<Button
android:layout_centerInParent="true"
android:layout_below="@+id/button_forward"
<Button
android:id="@+id/button_backward"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@+id/button_forward"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/button_left"
android:layout_width="100dip"
android:text="后" />
<Button
@ -66,4 +78,32 @@
android:layout_toRightOf="@+id/button_right"
android:text="停" />
<!-- <ImageView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_alignParentStart="true"-->
<!-- android:layout_alignParentTop="true"-->
<!-- android:layout_marginStart="40dp"-->
<!-- android:layout_marginLeft="40dp"-->
<!-- android:layout_marginTop="260dp"-->
<!-- android:src="@drawable/rocket_circle1" />-->
<!-- <ImageView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_alignParentStart="true"-->
<!-- android:layout_alignParentTop="true"-->
<!-- android:layout_marginStart="65dp"-->
<!-- android:layout_marginLeft="65dp"-->
<!-- android:layout_marginTop="285dp"-->
<!-- android:src="@drawable/rocket_circle2" />-->
<wificar.RockerView
android:id="@+id/rockerView"
android:layout_width="265dp"
android:layout_height="300dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="167dp" />
</RelativeLayout>
Loading…
Cancel
Save