今天和大家分享一下AndroID Studio实现摇杆,原理很简单,代码中有对应的注释。@H_419_6@实现效果图
@H_419_6@
@H_419_6@@H_419_6@实现代码
package com.example.fragmentcar1.vIEw;import androID.content.Context;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.RectF;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import androIDx.annotation.Nullable;/** * 摇杆视图 */public class RemoteVIEw extends VIEw { Paint backPaint = new Paint();//背景画笔 Paint bubblePaint = new Paint();//气泡画笔 Paint rectfPaint = new Paint(); /** * 气泡的位置 */ float bubbleX = 300, bubbleY = 300; /** * 背景圆的位置 */ float backX = 300, backY = 300; /** * 气泡和背景的半径 */ int radiusBack = 200, radiusBubble = 100; RectF mRectF = new RectF(backX-radiusBack,backY-radiusBack,backX+radiusBack,backY+radiusBack); Context mContext; /** * Stop 停止 * RETURN 后退 * left 左转 * RIGHT 右转 * GO 前进 * 默认为停止 */ String orIEntation="Stop"; public RemoteVIEw(Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.mContext = context; } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); initPaint(); canvas.drawCircle(backX, backY, radiusBack, backPaint); if (orIEntation=="GO") { canvas.drawArc(mRectF, -45, -90, true, rectfPaint); }else if (orIEntation=="RETURN"){ canvas.drawArc(mRectF, 45, 90, true, rectfPaint); }else if (orIEntation=="left"){ canvas.drawArc(mRectF, 135, 90, true, rectfPaint); }else if (orIEntation=="RIGHT"){ canvas.drawArc(mRectF, -45, 90, true, rectfPaint); }else if (orIEntation=="Stop"){ rectfPaint.setAlpha(0); canvas.drawArc(mRectF, -90, 360, true, rectfPaint); } canvas.drawCircle(bubbleX, bubbleY, radiusBubble, bubblePaint); } private voID initPaint() { backPaint.setAntiAlias(true); backPaint.setcolor(color.parsecolor("#60ffffff")); bubblePaint.setAntiAlias(true); bubblePaint.setcolor(color.parsecolor("#90ffffff")); rectfPaint.setAntiAlias(true); rectfPaint.setcolor(color.parsecolor("#ffffff")); rectfPaint.setAlpha(144); } @OverrIDe public boolean ontouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: float x = (int) event.getX(); float y = (int) event.getY(); if (getdistance(x, y, backX, backY) < radiusBack) { bubbleX = x; bubbleY = y; } else if (getdistance(x, y, backX, backY) >= radiusBack) { float xAndy[]; xAndy = getXY(x, y, backX, backY, getdistance(x, y, backX, backY)); bubbleX = xAndy[0]; bubbleY = xAndy[1]; getorIEntation(x,y); } break; case MotionEvent.ACTION_UP: bubbleX = backX; bubbleY = backY; orIEntation="Stop"; break; } invalIDate(); return true; } /** * 得到手指触控点与圆点中心的距离 * * @param x1 * @param y1 * @param x2 * @param y2 * @return */ private float getdistance(float x1, float y1, float x2, float y2) { float dis; dis = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); return dis; } /** * 当手指触控点在大圆外面时 * 需要重新得到气泡的位置 * * @param x1 * @param y1 * @param x2 * @param y2 * @param dis * @return */ private float[] getXY(float x1, float y1, float x2, float y2, float dis) { float[] xAndy = new float[2]; float scaledis; float xdis; float ydis; /** * 表示在第一象限之内 */ if (x1 > x2 && y1 < y2) { scaledis = radiusBack / dis; xdis = Math.abs(x1 - x2); ydis = Math.abs(y1 - y2); xAndy[0] = x2 + xdis * scaledis; xAndy[1] = y2 - ydis * scaledis; } /** * 表示在第二象限之内 */ else if (x1 < x2 && y1 < y2) { scaledis = radiusBack / dis; xdis = Math.abs(x1 - x2); ydis = Math.abs(y1 - y2); xAndy[0] = x2 - xdis * scaledis; xAndy[1] = y2 - ydis * scaledis; } /** *表示在第三象限之内 */ else if (x1 < x2 && y1 > y2) { scaledis = radiusBack / dis; xdis = Math.abs(x1 - x2); ydis = Math.abs(y1 - y2); xAndy[0] = x2 - xdis * scaledis; xAndy[1] = y2 + ydis * scaledis; } /** * 表示在第四象限之内 */ else if (x1 > x2 && y1 > y2) { scaledis = radiusBack / dis; xdis = Math.abs(x1 - x2); ydis = Math.abs(y1 - y2); xAndy[0] = x2 + xdis * scaledis; xAndy[1] = y2 + ydis * scaledis; } /** * 角度为零度 */ else if (x1 > x2 && y1 == y2) { xAndy[0] = x2 + radiusBack; xAndy[1] = y2; } /** * 角度为90度 */ else if (x1 == x2 && y1 < y2) { xAndy[0] = x2; xAndy[1] = y2 - radiusBack; } /** * 角度为180度 */ else if (x1 < x2 && y1 == y2) { xAndy[0] = x2 - radiusBack; xAndy[1] = y2; } /** * 表示为270度 */ else if (x1 == x2 && y1 > y2) { xAndy[0] = x2; xAndy[1] = y2 + radiusBack; } return xAndy; } /** * 更具摇杆 *** 作的方向来控制小车的运动方向 */ private voID getorIEntation(float x,float y){ if (y<backY&&(x<backX+backX*0.707&&x>backY-backY*0.707)){ orIEntation = "GO"; }else if (x>backX&&(y<backY+backY*0.707&&y>backY-backY*0.707)){ orIEntation="RIGHT"; }else if (y>backY&&(x<backX+backX*0.707&&x>backY-backY*0.707)){ orIEntation="RETURN"; }else if (x<backX&&(y<backY+backY*0.707&&y>backY-backY*0.707)){ orIEntation="left"; }else { orIEntation="Stop"; } }}
总结 以上是内存溢出为你收集整理的Android Studio中实现摇杆全部内容,希望文章能够帮你解决Android Studio中实现摇杆所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)