1,在Activity中,只要在oncreat方法进行如下定义
public class BallActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ball);
Rudder rud = (Rudder)findViewById(R.id.rudder);
rud.setRudderListener(new RudderListener() {
public void onSteeringWheelChanged(int action, int angle) {
if(action == Rudder.ACTION_RUDDER) {
//TODO:事件实现
}
}
});
}
2,类Rudder继承SurfaceView和实现Runnable, Callback接口,具体如下:
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff.Mode;
import android.graphics.RectF;
import android.view.SurfaceHolder.Callback;
import android.support.v4.app.NotificationCompat.Action;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Rudder extends SurfaceView implements Runnable, Callback {
public static final int ACTION_RUDDER = 1, ACTION_ATTACK = 2; // 1:摇杆事件
// private ArrayList<Ball> list = new ArrayList<Ball>(); //
// 2:按钮事件(未实现)
private SurfaceHolder MyHolder;
private boolean isStop = false;
private Thread MyThread;
private Paint MyPaint;
private Point MyRockerPosition; // 摇杆位置
private Point MyBallPosition;
private Point MyCtrlPoint = new Point(200, 800);// 摇杆起始位置
private RudderListener listener = null; // 事件回调接口
private Canvas canvas = null;
private Paint paint = new Paint();
private Ball ball;
private int MyRudderRadius = 40;// 摇杆半径
private int MyBallRadius = 70; // 小球半径
private int MyWheelRadius = 120;// 摇杆活动范围半径
private int x, x2, y, y2, x3, y3;
public Rudder(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Rudder(Context context, AttributeSet as) {
super(context, as);
this.setKeepScreenOn(true);
MyHolder = getHolder();
MyHolder.addCallback((android.view.SurfaceHolder.Callback) this);
MyThread = new Thread(this);
MyPaint.setColor(Color.CYAN); // 设置摇杆背景颜色
MyPaint.setAntiAlias(true); // 抗锯齿
MyRockerPosition = new Point(MyCtrlPoint);
setFocusable(true);
setFocusableInTouchMode(true);
// setZOrderOnTop(true);
MyHolder.setFormat(PixelFormat.TRANSLUCENT); // 设置背景透明
}
public void setRudderListener(RudderListener rockerListener) {
listener = rockerListener;
}
public void run() {
// TODO Auto-generated method stub
while (!isStop) {
try {
canvas = MyHolder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);// 清除屏幕
MyPaint.setColor(Color.WHITE); // 设置背景
// RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
// canvas.drawRect(rect, paint);
// ball=new
// Ball((this.getWidth())/2,(this.getHeight())/2,80,Color.GREEN);
// list.add(ball);
//
// for(int i=0;i<list.size();i++){
// Ball ball2=list.get(i);
// ball2.drawBall(paint, canvas);}
MyPaint.setColor(Color.CYAN);
canvas.drawCircle(MyCtrlPoint.x, MyCtrlPoint.y, MyWheelRadius,
MyPaint);// 绘制范围
MyPaint.setColor(Color.RED);
canvas.drawCircle(MyRockerPosition.x, MyRockerPosition.y,
MyRudderRadius, MyPaint);// 绘制摇杆
// 绘制初始小球
MyBallPosition = new Point((this.getWidth() / 2),
this.getHeight() / 2);
MyPaint.setColor(Color.GREEN);
canvas.drawCircle(MyBallPosition.x, MyBallPosition.y,
MyBallRadius, MyPaint);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
MyHolder.unlockCanvasAndPost(canvas);
}
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
MyThread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
isStop = true;
}
public boolean onTouchEvent(MotionEvent event) {
// Ball ball3=list.get(0);
int length = MathSave.getLength(MyCtrlPoint.x, MyCtrlPoint.y,
event.getX(), event.getY());
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 如果屏幕接触点不在摇杆挥动范围内,则不处理
if (length > MyWheelRadius) {
return true;
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (length <= MyWheelRadius) {
// 如果手指在摇杆活动范围内,则摇杆处于手指触摸位置
MyRockerPosition.set((int) event.getX(), (int) event.getY());
} else {
// 设置摇杆位置,使其处于手指触摸方向的 摇杆活动范围边缘
MyRockerPosition = MathSave.getBorderPoint(MyCtrlPoint,
new Point((int) event.getX(), (int) event.getY()),
MyWheelRadius);
}
if (listener != null) {
float radian = MathSave.getRadian(MyCtrlPoint, new Point(
(int) event.getX(), (int) event.getY()));
listener.onSteeringWheelChanged(ACTION_RUDDER,
Rudder.this.getAngleCouvert(radian));
}
}
// 如果手指离开屏幕,则摇杆返回初始位置
if (event.getAction() == MotionEvent.ACTION_UP) {
MyRockerPosition = new Point(MyCtrlPoint);
}
return true;
}
// 获取摇杆偏移角度 0-360°
private int getAngleCouvert(float radian) {
int tmp = (int) Math.round(radian / Math.PI * 180);
if (tmp < 0) {
return -tmp;
} else {
return 180 + (180 - tmp);
}
}
public interface RudderListener {
void onSteeringWheelChanged(int action, int angle);
}
}
3.自己定义了MathsSave类,里面有两个方法,分别为获得两点之间距离和和获取水平夹角弧度
import android.graphics.Point;
public class MathSave {
//获取两点间直线距离
public static int getLength(float x1,float y1,float x2,float y2) {
return (int)Math.sqrt(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2));
}
public static Point getBorderPoint(Point a, Point b,int cutRadius) {
float radian = getRadian(a, b);
return new Point(a.x + (int)(cutRadius * Math.cos(radian)), a.x + (int)(cutRadius * Math.sin(radian)));
}
//获取水平线夹角弧度
public static float getRadian (Point a, Point b) {
float lenA = b.x-a.x;
float lenB = b.y-a.y;
float lenC = (float)Math.sqrt(lenA*lenA+lenB*lenB);
float ang = (float)Math.acos(lenA/lenC);
ang = ang * (b.y < a.y ? -1 : 1);
return ang;
}
}
4,在.xml下的布局如下实现
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
/>
<RelativeLayout
android:id="@+id/ctrls"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.ball.Rudder
android:id="@+id/rudder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</FrameLayout>
最后就可以进行测试了,可以自己试着定义这个摇杆的背景颜色等,以及整个Activity的背景。