A single virtual joystick development使用的圆盘和摇杆都是使用颜色来实现的,但是在实际的开发过程中,这并不美观,所有在这使用图片替换之前使用颜色的圆盘和摇杆,两者之间的区别在于,使用颜色绘制使用的是中心点作为坐标,而使用图片则使用的是左上角作为坐标,所以这里需要进行转换,同时注意的就是图片的宽度问题,里面计算的都是使用的像素,而你一开始将图片转化为bitmap的时候并不是你期望的尺寸,这里也需要转换,最后一点就是摇杆图片移动的问题,这里有两个方案,一个是改变坐标(要实现这种方法则可以使用组件显示图片,然后移动组件),第二种方法就是我现在使用的canvas绘制每次都是先清空颜色然后重新绘制的方法。
实现圆盘和摇杆功能的类---AppSingleRocker.java:
package com.seuic.singlerocker;
import com.seuic.util.MathUtils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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;
import android.view.SurfaceHolder.Callback;
public class AppSingleRocker extends SurfaceView implements Callback{
private SurfaceHolder mHolder;
private Paint mPaint;
public Point mRockerPosition; // 摇杆位置
private Point mCtrlPoint = new Point(80, 80);// 摇杆起始位置
//private int mRudderRadius = 20;// 摇杆半径
private int mWheelRadius = 60;// 摇杆活动范围半径
int isHide = 0;
private SingleRudderListener listener = null; //事件回调接口
Bitmap bitmap,bitmap2;
public static final int ACTION_RUDDER = 1, ACTION_ATTACK_DEVICEMOVE = 2, ACTION_STOP = 3, ACTION_ATTACK_CAMERAMOVE = 4;
public AppSingleRocker(Context context) {
super(context);
this.setKeepScreenOn(true);
mHolder = getHolder();
mHolder.addCallback(this);
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
mPaint.setAntiAlias(true);//抗锯齿
mRockerPosition = new Point(mCtrlPoint);
setFocusable(true);
setFocusableInTouchMode(true);
setZOrderOnTop(true);
mHolder.setFormat(PixelFormat.TRANSPARENT);//设置背景透明
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.j4);
bitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.j5);
bitmap2 = Bitmap.createScaledBitmap(bitmap2, 40, 40, false);
package com.seuic.singlerocker;
import com.seuic.singlerocker.AppSingleRocker.SingleRudderListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import android.app.Activity;
public class SingleRocker extends Activity {
RelativeLayout Parent;
AppSingleRocker appSingleRocker;
public static final String Main = "SingleRocker";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Parent = new RelativeLayout(this);
RelativeLayout.LayoutParams RockerCanvasParams = new RelativeLayout.LayoutParams(160,160);
RockerCanvasParams.addRule(RelativeLayout.CENTER_VERTICAL);
RockerCanvasParams.leftMargin = 120;
appSingleRocker = new AppSingleRocker(this);
Parent.addView(appSingleRocker,RockerCanvasParams);
setContentView(Parent);
appSingleRocker.setSingleRudderListener(new SingleRudderListener() {
@Override
public void onSteeringWheelChanged(int action, int angle) {
Log.e(Main, angle+"");
}
});
}
}
在AppSingleRocker.java中将
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.j4);
bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.j5);
替换为
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.j4);
bitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.j5);
bitmap2 = Bitmap.createScaledBitmap(bitmap2, 40, 40, false);