这段时间研究自定义控件,遂想起游戏里的摇杆的实现挺有意思,于是来自己写一套熟悉熟悉,关于SurfaceView的特性网上也有很多,故不赘述,反正绘图用起来挺爽就是了,永远的告别了JAVA GUI手动实现双缓冲的时代了……
rudderSurfaceView .java
package com.wificar;
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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class rudderSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{
private SurfaceHolder mHolder;
private boolean isStop = false;
private Thread mThread;
private Paint mPaint;
private Point mRockerPosition; //摇杆位置
private Point mCtrlPoint = new Point(180,280);//摇杆起始位置
private int mRudderRadius = 20;//摇杆半径
private int mWheelRadius = 60;//摇杆活动范围半径
private RudderListener listener = null; //事件回调接口
public static final int ACTION_RUDDER = 1 , ACTION_ATTACK = 2; // 1:摇杆事件 2:按钮事件(未实现)
public rudderSurfaceView(Context context) {
super(context);
}
public rudderSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.setKeepScreenOn(true);
mHolder = getHolder();
mHolder.addCallback(this);
mThread = new Thread(this);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setAntiAlias(true);抗锯齿
mRockerPosition = new Point(mCtrlPoint);
setFocusable(true);
setFocusableInTouchMode(true);
mHolder.setFormat(PixelFormat.TRANSPARENT);// 设置背景透明
}
//设置回调接口
public void setRudderListener(RudderListener rudderListener){
listener = rudderListener;
}
@Override
public void run() {
// TODO Auto-generated method stub
Canvas canvas = null;
while(!isStop){
try{
canvas = mHolder.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);//清除屏幕
mPaint.setColor(Color.CYAN);
canvas.drawCircle(mCtrlPoint.x, mCtrlPoint.y, mWheelRadius, mPaint);//绘制范围
mPaint.setColor(Color.RED);
canvas.drawCircle(mRockerPosition.x, mRockerPosition.y, mRudderRadius, mPaint);//绘制摇杆
}catch(Exception e){
e.printStackTrace();
}finally{
if(canvas != null){
mHolder.unlockCanvasAndPost(canvas);
}
}
try{
Thread.sleep(30);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
mThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
isStop = true;
// mThread.stop();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int len = MathUtils.getLength(mCtrlPoint.x, mCtrlPoint.y, event.getX(), event.getY());
if(event.getAction() == MotionEvent.ACTION_DOWN){
//如果屏幕接触点不在摇杆挥动范围内,则不处理
if(len > mWheelRadius){
return true;
}
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
if(len <= mWheelRadius){
//如果手指在摇杆活动范围内,则摇杆处于手指触摸位置
mRockerPosition.set((int)event.getX(), (int)event.getY());
}else{
//设置摇杆位置,使其处于手指触摸方向的 摇杆活动范围边缘
mRockerPosition = MathUtils.getBorderPoint(mCtrlPoint, new Point((int)event.getX(), (int)event.getY()), mWheelRadius);
}
if(listener != null){
float radian = MathUtils.getRadian(mCtrlPoint, new Point((int)event.getX(), (int)event.getY()));
listener.onSteeringWheelChanged(ACTION_RUDDER, rudderSurfaceView.this.getAngleCouvert(radian));
}
}
//如果手指离开屏幕,则摇杆返回初始位置
if(event.getAction()== MotionEvent.ACTION_UP){
mRockerPosition = new Point(mCtrlPoint);
}
return true;
}
//回调接口
public interface RudderListener{
public void onSteeringWheelChanged(int action, int angle);
}
//获取摇杆偏移角度 0-360°
public int getAngleCouvert(float raian){
int tmp = (int)Math.round(raian/Math.PI*180);
if(tmp<0){
return -tmp;
}else{
return 180 + (180 - tmp);
}
}
}
为了以后计算这些角度啊弧度什么的数学相关的计算,定义了一个Math工具类方便以后使用
package com.wificar;
import android.graphics.Point;
public class MathUtils {
//获取两点间直线距离
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));
}
/**
* 获取线段上某个点的坐标,长度为a.x - cutRadius
* @param a 点A
* @param b 点B
* @param cutRadius 截断距离
* @return 截断点
*/
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.y + (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;
}
}
将自定义的控件加入到Layout文件里,在这里定义摇杆的大小和位置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<!-- <com.wificar.videoSurfaceView android:id="@+id/view3d"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/> -->
<com.wificar.rudderSurfaceView android:id="@+id/rudder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"// 上面固定了坐标点,,这里测试冒似没用,
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
Activity代码,OnCreate方法里的实现,Android屏幕右方是0°,逆时针一圈360°,跟Java绘图还是一样的
rudder = (rudderSurfaceView)findViewById(R.id.rudder);
rudder.setRudderListener(new RudderListener() {
@Override
public void onSteeringWheelChanged(int action, int angle) {
// TODO Auto-generated method stub
}
});
from:http://www.cnblogs.com/freedom-elf/archive/2011/08/08/2130749.html