您的位置:首页 > 移动开发 > Android开发

安卓自定义一个全角度的方向盘View

2018-01-04 18:48 176 查看
近日在制作一款坦克小游戏的过程中,想要实现方向盘控制坦克的移动



绞尽脑汁终于实现了这个小功能



应该有很多不合理的地方



但是楼主是一个才学安卓一年不到的小白



1xian贴MainActivity的代码

private DisplayMetrics displayMetrics;
private int windowWidth;
private int windowHeight;
private WheelView wheel;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.activity_main);
initWheelView();
}
public void initWheelView(){
wheel = (WheelView) findViewById(R.id.my_wheel);
wheel.setOnTouchListener(this);
wheel.setWindowSize(windowWidth,windowHeight);
wheel.rushSmallXYAndBigXY(30, windowHeight - wheel.bitmapBig.getHeight() - 30);
}

public void getWindowMeasure(){
displayMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
windowWidth = displayMetrics.widthPixels;
windowHeight = displayMetrics.heightPixels;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.my_wheel:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
wheel.removeBitmapSmall(event);
break;
case MotionEvent.ACTION_MOVE:
wheel.removeBitmapSmall(event);
break;
case MotionEvent.ACTION_UP:
wheel.rushSmallXYAndBigXY(30, windowHeight - wheel.bitmapBig.getHeight() - 30);
wheel.invalidate();
break;
}
}
return true;
}


2zai贴WheelView的代码;

public class WheelView extends View {
/*大圆图片*/
Bitmap bitmapBig;
/*小圆图片*/
Bitmap bitmapSmall;
/*大圆左上x坐标*/
int bitmapBigLeftTopX ;
/*大圆左上y坐标*/
int bitmapBigLeftTopY ;
/*小圆左上x坐标*/
int bitmapSmallLeftTopX;
/*小圆左上y坐标*/
int bitmapSmallLeftTopY;
/*圆心x*/
double centerX;
/*圆心y*/
double centerY;
/*点击的x坐标*/
float pointX;
/*点击的y坐标*/
float pointY;
/*屏幕的宽度*/
int windowWidth;
/*屏幕的高度*/
int windowHeight;

public WheelView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
/*初始化图片*/
bitmapBig=BitmapFactory.decodeResource(getResources(), R.drawable.wheel_big);
bitmapSmall=BitmapFactory.decodeResource(getResources(),R.drawable.wheel_small);

/*初始化所有坐标*/
rushSmallXYAndBigXY(0,0);
}

public void rushSmallXYAndBigXY(int bitmapBigLeftTopX, int bitmapBigLeftTopY){
this.bitmapBigLeftTopX=bitmapBigLeftTopX;
this.bitmapBigLeftTopY=bitmapBigLeftTopY;
centerX=bitmapBigLeftTopX+bitmapBig.getWidth()/2;
centerY=bitmapBigLeftTopY+bitmapBig.getHeight()/2;
bitmapSmallLeftTopX=(int)centerX-bitmapSmall.getWidth()/2;
bitmapSmallLeftTopY=(int)centerY-bitmapSmall.getHeight()/2;
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmapBig,bitmapBigLeftTopX,bitmapBigLeftTopY,null);
canvas.drawBitmap(bitmapSmall,bitmapSmallLeftTopX,bitmapSmallLeftTopY,null);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/*宽度设置为屏幕一半*/
int width=getMeasuredWidth()/2;
setMeasuredDimension(width,heightMeasureSpec);
}

/*根据触摸屏幕切换小圆的位置*/
public void removeBitmapSmall(MotionEvent event){
/*获的触屏事件的,X,Y坐标*/
pointX=event.getX();
pointY=event.getY();
/*超出屏幕一半时点击是没有效果的*/
if(pointX<windowWidth/2){
/*大圆半径*/
int r=bitmapBig.getWidth()/2;
/*触摸的点到圆心的距离*/
double R=Math.sqrt(Math.pow(pointX-centerX,2)+Math.pow(pointY-centerY,2));
/*如果触摸的点再圆内,则将小圆坐标设置为触摸的点*/
if (R<r){
bitmapSmallLeftTopX=((int)(pointX-bitmapSmall.getWidth()/2));
bitmapSmallLeftTopY=((int)(pointY-bitmapSmall.getHeight()/2));
} else{
/*根据比例计算出,刚好不超出大圆的小圆坐标*/
double x1=(Math.abs(pointX-centerX)*r)/R;
double y1=(Math.abs(pointY-centerY)*r)/R;
double x2=0;
double y2=0;
if (pointX>=centerX&&pointY>=centerY){
x2=x1+centerX;
y2=y1+centerY;
}
if (pointX<=centerX&&pointY>=centerY){
x2=centerX-x1;
y2=centerY+y1;
}
if (pointX<=centerX&&pointY<=centerY){
x2=centerX-x1;
y2=centerY-y1;
}
if (pointX>=centerX&&pointY<=centerY){
x2=centerX+x1;
y2=centerY-y1;
}
bitmapSmallLeftTopX=((int)x2-bitmapSmall.getWidth()/2);
bitmapSmallLeftTopY=((int)y2-bitmapSmall.getHeight()/2);
}
invalidate();
}else {
/*如果超过屏幕宽度一半,则回到,一开始设置的屏幕左下角30dp处*/
rushSmallXYAndBigXY(30, windowHeight - bitmapBig.getHeight() - 30);
}
}
public void setWindowSize(int windowWidth,int windowHeight){
/*获得屏幕的宽度和高度,在mainActivity中调用*/
this.windowWidth=windowWidth;
this.windowHeight=windowHeight;
}
}



2zuihou贴xml布局文件代码
<com.example.wo.tankgame.LeverMode.WheelView
android:id="@+id/my_wheel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"/>




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android
相关文章推荐