android 虚拟摇杆图片版
在之前A single virtual joystick development使用的圆盘和摇杆都是使用颜色来实现的,但是在实际的开发过程中,这并不美观,所有在这使用图片替换之前使用颜色的圆盘和摇杆,两者之间的区别在于,使用颜色绘制使用的是中心点作为坐标,而使用图片则使用的是左上角作为坐标,所以这里需要进行转换,同时注意的就是图片的宽度问题,里面计算的都是使用的像素,而你一开始将图片转化为bitmap的时候并不是你期望的尺寸,这里也需要转换,最后一点就是摇杆图片移动的问题,这里有两个方案,一个是改变坐标(要实现这种方法则可以使用组件显示图片,然后移动组件),第二种方法就是我现在使用的canvas绘制每次都是先清空颜色然后重新绘制的方法。
实现圆盘和摇杆功能的类---AppSingleRocker.java:
[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.Su易做图ceHolder;
import android.view.Su易做图ceView;
import android.view.Su易做图ceHolder.Callback;
public class AppSingleRocker extends Su易做图ceView implements Callback{
private Su易做图ceHolder 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);//设置背景透明 <P> bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.j4); </P><P><SPAN> bitmap = Bitmap.createScaledBitmap(bitmap, </SPAN><SPAN class=number>120</SPAN><SPAN>, </SPAN><SPAN class=number>120</SPAN><SPAN>, </SPAN><SPAN class=keyword>false</SPAN><SPAN>); </SPAN></P><P class=alt><SPAN> bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.j5); </SPAN></P><P class=alt><SPAN></SPAN><SPAN> bitmap2 = Bitmap.createScaledBitmap(bitmap2, </SPAN><SPAN class=number>40</SPAN><SPAN>, </SPAN><SPAN class=number>40</SPAN><SPAN>, </SPAN><SPAN class=keyword>false</SPAN><SPAN>); </SPAN></P> }
public AppSingleRocker(Context context, AttributeSet attribute) {
super(context, attribute);
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); }
//回调接口
public inte易做图ce SingleRudderListener {
void onSteeringWheelChanged(int action,int angle);
}
//设置回调接口
public void setSingleRudderListener(SingleRudderListener rockerListener) {
listener = rockerListener;
}
int len;
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
if (isHide == 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
len = MathUtils.getLength(mCtrlPoint.x, mCtrlPoint.y, event.getX(
补充:移动开发 , Android ,