日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android虚拟摇杆

發布時間:2023/12/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android虚拟摇杆 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

虛擬搖桿一

資源下載

效果如下:


第一步:attr 中定義自定義參數,如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
???
<!--
??? areaBackground
設置區域背景
??? rockerBackground
設置搖桿的樣式
??? rockerScale 設置搖桿的相對于背景的比例
??? rockerSpeedLevel 設置當前位置相對于中心點的距離的比例? 如:10? 則中心點到邊緣的距離分為 10 分越靠外數值越大 0-10
??? rockerCallBackMode 有變化就回調,或者是方向改變才會回調-->
??? <declare-styleable name="RockerView">
??????? <attr
name="areaBackground"format="color|reference" />
??????? <attr
name="rockerBackground"format="color|reference" />
??????? <attr
name="rockerScale"format="float"/>
??????? <attr
name="rockerSpeedLevel"format="integer" />
??????? <attr
name="rockerCallBackMode">
??????????? <flag
name="CALL_BACK_MODE_MOVE" value="0" />
??????????? <flag
name="CALL_BACK_MODE_STATE_CHANGE" value="1" />
??????? </attr>
??? </declare-styleable>
</resources>


第二步:新建java類,繼承View,代碼較多,附帶注釋,不一一講解了,不多700行吧:

public class MyRockerView extends View {private static final String TAG = "RockerView";private static final int DEFAULT_SIZE = 400;private static final float DEFAULT_ROCKER_SCALE = 0.5f;//默認半徑為背景的1/2private Paint mAreaBackgroundPaint;private Paint mRockerPaint;private Point mRockerPosition;private Point mCenterPoint;private int mAreaRadius;private float mRockerScale;private int mRockerRadius;private CallBackMode mCallBackMode = CallBackMode.CALL_BACK_MODE_MOVE;private OnAngleChangeListener mOnAngleChangeListener;private OnShakeListener mOnShakeListener;private OnDistanceLevelListener mOnDistanceLevelListener;private DirectionMode mDirectionMode;private Direction tempDirection = Direction.DIRECTION_CENTER;private float lastDistance = 0;private boolean hasCall = false;private float baseDistance = 0;private int mDistanceLevel = 10;//分成10分// 角度private static final double ANGLE_0 = 0;private static final double ANGLE_360 = 360;// 360°水平方向平分2份的邊緣角度private static final double ANGLE_HORIZONTAL_2D_OF_0P = 90;private static final double ANGLE_HORIZONTAL_2D_OF_1P = 270;// 360°垂直方向平分2份的邊緣角度private static final double ANGLE_VERTICAL_2D_OF_0P = 0;private static final double ANGLE_VERTICAL_2D_OF_1P = 180;// 360°平分4份的邊緣角度private static final double ANGLE_4D_OF_0P = 0;private static final double ANGLE_4D_OF_1P = 90;private static final double ANGLE_4D_OF_2P = 180;private static final double ANGLE_4D_OF_3P = 270;// 360°平分4份的邊緣角度(旋轉45度)private static final double ANGLE_ROTATE45_4D_OF_0P = 45;private static final double ANGLE_ROTATE45_4D_OF_1P = 135;private static final double ANGLE_ROTATE45_4D_OF_2P = 225;private static final double ANGLE_ROTATE45_4D_OF_3P = 315;// 360°平分8份的邊緣角度private static final double ANGLE_8D_OF_0P = 22.5;private static final double ANGLE_8D_OF_1P = 67.5;private static final double ANGLE_8D_OF_2P = 112.5;private static final double ANGLE_8D_OF_3P = 157.5;private static final double ANGLE_8D_OF_4P = 202.5;private static final double ANGLE_8D_OF_5P = 247.5;private static final double ANGLE_8D_OF_6P = 292.5;private static final double ANGLE_8D_OF_7P = 337.5;// 搖桿可移動區域背景private static final int AREA_BACKGROUND_MODE_PIC = 0;private static final int AREA_BACKGROUND_MODE_COLOR = 1;private static final int AREA_BACKGROUND_MODE_XML = 2;private static final int AREA_BACKGROUND_MODE_DEFAULT = 3;private int mAreaBackgroundMode = AREA_BACKGROUND_MODE_DEFAULT;private Bitmap mAreaBitmap;private int mAreaColor;// 搖桿背景private static final int ROCKER_BACKGROUND_MODE_PIC = 4;private static final int ROCKER_BACKGROUND_MODE_COLOR = 5;private static final int ROCKER_BACKGROUND_MODE_XML = 6;private static final int ROCKER_BACKGROUND_MODE_DEFAULT = 7;private int mRockerBackgroundMode = ROCKER_BACKGROUND_MODE_DEFAULT;private Bitmap mRockerBitmap;private int mRockerColor;public MyRockerView(Context context, AttributeSet attrs) {super(context, attrs);// 獲取自定義屬性initAttribute(context, attrs);if (isInEditMode()) {}// 移動區域畫筆mAreaBackgroundPaint = new Paint();mAreaBackgroundPaint.setAntiAlias(true);// 搖桿畫筆mRockerPaint = new Paint();mRockerPaint.setAntiAlias(true);// 中心點mCenterPoint = new Point();// 搖桿位置mRockerPosition = new Point();}/*** 獲取屬性** @param context context* @param attrs attrs*/private void initAttribute(Context context, AttributeSet attrs) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RockerView);// 可移動區域背景Drawable areaBackground = typedArray.getDrawable(R.styleable.RockerView_areaBackground);if (null != areaBackground) {// 設置了背景if (areaBackground instanceof BitmapDrawable) {// 設置了一張圖片mAreaBitmap = ((BitmapDrawable) areaBackground).getBitmap();mAreaBackgroundMode = AREA_BACKGROUND_MODE_PIC;} else if (areaBackground instanceof GradientDrawable) {// XMLmAreaBitmap = drawable2Bitmap(areaBackground);mAreaBackgroundMode = AREA_BACKGROUND_MODE_XML;} else if (areaBackground instanceof ColorDrawable) {// 色值mAreaColor = ((ColorDrawable) areaBackground).getColor();mAreaBackgroundMode = AREA_BACKGROUND_MODE_COLOR;} else {// 其他形式mAreaBackgroundMode = AREA_BACKGROUND_MODE_DEFAULT;}} else {// 沒有設置背景mAreaBackgroundMode = AREA_BACKGROUND_MODE_DEFAULT;}// 搖桿背景Drawable rockerBackground = typedArray.getDrawable(R.styleable.RockerView_rockerBackground);if (null != rockerBackground) {// 設置了搖桿背景if (rockerBackground instanceof BitmapDrawable) {// 圖片mRockerBitmap = ((BitmapDrawable) rockerBackground).getBitmap();mRockerBackgroundMode = ROCKER_BACKGROUND_MODE_PIC;} else if (rockerBackground instanceof GradientDrawable) {// XMLmRockerBitmap = drawable2Bitmap(rockerBackground);mRockerBackgroundMode = ROCKER_BACKGROUND_MODE_XML;} else if (rockerBackground instanceof ColorDrawable) {// 色值mRockerColor = ((ColorDrawable) rockerBackground).getColor();mRockerBackgroundMode = ROCKER_BACKGROUND_MODE_COLOR;} else {// 其他形式mRockerBackgroundMode = ROCKER_BACKGROUND_MODE_DEFAULT;}} else {// 沒有設置搖桿背景mRockerBackgroundMode = ROCKER_BACKGROUND_MODE_DEFAULT;}// 搖桿半徑mRockerScale = typedArray.getFloat(R.styleable.RockerView_rockerScale, DEFAULT_ROCKER_SCALE);//距離級別mDistanceLevel = typedArray.getInt(R.styleable.RockerView_rockerSpeedLevel, 10);//回調模式mCallBackMode = getCallBackMode(typedArray.getInt(R.styleable.RockerView_rockerCallBackMode, 0));typedArray.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int measureWidth, measureHeight;int widthMode = MeasureSpec.getMode(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);if (widthMode == MeasureSpec.EXACTLY) {// 具體的值和match_parentmeasureWidth = widthSize;} else {// wrap_contentmeasureWidth = DEFAULT_SIZE;}if (heightMode == MeasureSpec.EXACTLY) {measureHeight = heightSize;} else {measureHeight = DEFAULT_SIZE;}setMeasuredDimension(measureWidth, measureHeight);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int measuredWidth = getMeasuredWidth();int measuredHeight = getMeasuredHeight();int cx = measuredWidth / 2;int cy = measuredHeight / 2;// 中心點mCenterPoint.set(cx, cy);// 可移動區域的半徑mAreaRadius = (measuredWidth <= measuredHeight) ? (int) (cx / (mRockerScale + 1)) : (int) (cy / (mRockerScale + 1));mRockerRadius = (int) (mAreaRadius * mRockerScale);// 搖桿位置if (0 == mRockerPosition.x || 0 == mRockerPosition.y) {mRockerPosition.set(mCenterPoint.x, mCenterPoint.y);}// 畫可移動區域if (AREA_BACKGROUND_MODE_PIC == mAreaBackgroundMode || AREA_BACKGROUND_MODE_XML == mAreaBackgroundMode) {// 圖片Rect src = new Rect(0, 0, mAreaBitmap.getWidth(), mAreaBitmap.getHeight());Rect dst = new Rect(mCenterPoint.x - mAreaRadius, mCenterPoint.y - mAreaRadius, mCenterPoint.x + mAreaRadius, mCenterPoint.y + mAreaRadius);canvas.drawBitmap(mAreaBitmap, src, dst, mAreaBackgroundPaint);} else if (AREA_BACKGROUND_MODE_COLOR == mAreaBackgroundMode) {// 色值mAreaBackgroundPaint.setColor(mAreaColor);canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, mAreaRadius, mAreaBackgroundPaint);} else {// 其他或者未設置mAreaBackgroundPaint.setColor(Color.GRAY);canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, mAreaRadius, mAreaBackgroundPaint);}// 畫搖桿if (ROCKER_BACKGROUND_MODE_PIC == mRockerBackgroundMode || ROCKER_BACKGROUND_MODE_XML == mRockerBackgroundMode) {// 圖片Rect src = new Rect(0, 0, mRockerBitmap.getWidth(), mRockerBitmap.getHeight());Rect dst = new Rect(mRockerPosition.x - mRockerRadius, mRockerPosition.y - mRockerRadius, mRockerPosition.x + mRockerRadius, mRockerPosition.y + mRockerRadius);canvas.drawBitmap(mRockerBitmap, src, dst, mRockerPaint);} else if (ROCKER_BACKGROUND_MODE_COLOR == mRockerBackgroundMode) {// 色值mRockerPaint.setColor(mRockerColor);canvas.drawCircle(mRockerPosition.x, mRockerPosition.y, mRockerRadius, mRockerPaint);} else {// 其他或者未設置mRockerPaint.setColor(Color.RED);canvas.drawCircle(mRockerPosition.x, mRockerPosition.y, mRockerRadius, mRockerPaint);}}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 按下// 回調 開始callBackStart();case MotionEvent.ACTION_MOVE:// 移動float moveX = event.getX();float moveY = event.getY();baseDistance = mAreaRadius+2;Log.e("baseDistance",baseDistance+"");mRockerPosition = getRockerPositionPoint(mCenterPoint, new Point((int) moveX, (int) moveY), mAreaRadius + mRockerRadius, mRockerRadius);moveRocker(mRockerPosition.x, mRockerPosition.y);break;case MotionEvent.ACTION_UP:// 抬起case MotionEvent.ACTION_CANCEL:// 移出區域// 回調 結束callBackFinish();if (mOnShakeListener != null) {mOnShakeListener.direction(Direction.DIRECTION_CENTER);}float upX = event.getX();float upY = event.getY();moveRocker(mCenterPoint.x, mCenterPoint.y);break;}return true;}/*** 獲取搖桿實際要顯示的位置(點)** @param centerPoint 中心點* @param touchPoint 觸摸點* @param regionRadius 搖桿可活動區域半徑* @param rockerRadius 搖桿半徑* @return 搖桿實際顯示的位置(點)*/private Point getRockerPositionPoint(Point centerPoint, Point touchPoint, float regionRadius, float rockerRadius) {// 兩點在X軸的距離float lenX = (float) (touchPoint.x - centerPoint.x);// 兩點在Y軸距離float lenY = (float) (touchPoint.y - centerPoint.y);// 兩點距離float lenXY = (float) Math.sqrt((double) (lenX * lenX + lenY * lenY));// 計算弧度double radian = Math.acos(lenX / lenXY) * (touchPoint.y < centerPoint.y ? -1 : 1);// 計算角度double angle = radian2Angle(radian);if (lenXY + rockerRadius <= regionRadius) { // 觸摸位置在可活動范圍內// 回調 返回參數callBack(angle, (int) lenXY);return touchPoint;} else { // 觸摸位置在可活動范圍以外// 計算要顯示的位置int showPointX = (int) (centerPoint.x + (regionRadius - rockerRadius) * Math.cos(radian));int showPointY = (int) (centerPoint.y + (regionRadius - rockerRadius) * Math.sin(radian));callBack(angle, (int) Math.sqrt((showPointX - centerPoint.x) * (showPointX - centerPoint.x) + (showPointY - centerPoint.y) * (showPointY - centerPoint.y)));return new Point(showPointX, showPointY);}}/*** 移動搖桿到指定位置** @param x x坐標* @param y y坐標*/private void moveRocker(float x, float y) {mRockerPosition.set((int) x, (int) y);invalidate();}/*** 弧度轉角度** @param radian 弧度* @return 角度[0, 360)*/private double radian2Angle(double radian) {double tmp = Math.round(radian / Math.PI * 180);return tmp >= 0 ? tmp : 360 + tmp;}/*** Drawable 轉 Bitmap** @param drawable Drawable* @return Bitmap*/private Bitmap drawable2Bitmap(Drawable drawable) {// 取 drawable 的長寬int width = drawable.getIntrinsicWidth();int height = drawable.getIntrinsicHeight();// 取 drawable 的顏色格式Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;// 建立對應 bitmapBitmap bitmap = Bitmap.createBitmap(width, height, config);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, width, height);drawable.draw(canvas);return bitmap;}/*** 回調* 開始*/private void callBackStart() {tempDirection = Direction.DIRECTION_CENTER;if (null != mOnAngleChangeListener) {mOnAngleChangeListener.onStart();}if (null != mOnShakeListener) {mOnShakeListener.onStart();}}/*** 回調* 返回參數** @param angle 搖動角度*/private void callBack(double angle, float distance) {Log.e("distance",distance+"");if (Math.abs(distance - lastDistance) >= (baseDistance / mDistanceLevel)) {lastDistance = distance;if (null != mOnDistanceLevelListener) {int level = (int) (distance/(baseDistance/mDistanceLevel));mOnDistanceLevelListener.onDistanceLevel(level);}}if (null != mOnAngleChangeListener) {mOnAngleChangeListener.angle(angle);}if (null != mOnShakeListener) {if (CallBackMode.CALL_BACK_MODE_MOVE == mCallBackMode) {switch (mDirectionMode) {case DIRECTION_2_HORIZONTAL:// 左右方向if (ANGLE_0 <= angle && ANGLE_HORIZONTAL_2D_OF_0P > angle || ANGLE_HORIZONTAL_2D_OF_1P <= angle && ANGLE_360 > angle) {// 右mOnShakeListener.direction(Direction.DIRECTION_RIGHT);} else if (ANGLE_HORIZONTAL_2D_OF_0P <= angle && ANGLE_HORIZONTAL_2D_OF_1P > angle) {// 左mOnShakeListener.direction(Direction.DIRECTION_LEFT);}break;case DIRECTION_2_VERTICAL:// 上下方向if (ANGLE_VERTICAL_2D_OF_0P <= angle && ANGLE_VERTICAL_2D_OF_1P > angle) {// 下mOnShakeListener.direction(Direction.DIRECTION_DOWN);} else if (ANGLE_VERTICAL_2D_OF_1P <= angle && ANGLE_360 > angle) {// 上mOnShakeListener.direction(Direction.DIRECTION_UP);}break;case DIRECTION_4_ROTATE_0:// 四個方向if (ANGLE_4D_OF_0P <= angle && ANGLE_4D_OF_1P > angle) {// 右下mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);} else if (ANGLE_4D_OF_1P <= angle && ANGLE_4D_OF_2P > angle) {// 左下mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);} else if (ANGLE_4D_OF_2P <= angle && ANGLE_4D_OF_3P > angle) {// 左上mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);} else if (ANGLE_4D_OF_3P <= angle && ANGLE_360 > angle) {// 右上mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);}break;case DIRECTION_4_ROTATE_45:// 四個方向 旋轉45度if (ANGLE_0 <= angle && ANGLE_ROTATE45_4D_OF_0P > angle || ANGLE_ROTATE45_4D_OF_3P <= angle && ANGLE_360 > angle) {// 右mOnShakeListener.direction(Direction.DIRECTION_RIGHT);} else if (ANGLE_ROTATE45_4D_OF_0P <= angle && ANGLE_ROTATE45_4D_OF_1P > angle) {// 下mOnShakeListener.direction(Direction.DIRECTION_DOWN);} else if (ANGLE_ROTATE45_4D_OF_1P <= angle && ANGLE_ROTATE45_4D_OF_2P > angle) {// 左mOnShakeListener.direction(Direction.DIRECTION_LEFT);} else if (ANGLE_ROTATE45_4D_OF_2P <= angle && ANGLE_ROTATE45_4D_OF_3P > angle) {// 上mOnShakeListener.direction(Direction.DIRECTION_UP);}break;case DIRECTION_8:// 八個方向if (ANGLE_0 <= angle && ANGLE_8D_OF_0P > angle || ANGLE_8D_OF_7P <= angle && ANGLE_360 > angle) {// 右mOnShakeListener.direction(Direction.DIRECTION_RIGHT);} else if (ANGLE_8D_OF_0P <= angle && ANGLE_8D_OF_1P > angle) {// 右下mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);} else if (ANGLE_8D_OF_1P <= angle && ANGLE_8D_OF_2P > angle) {// 下mOnShakeListener.direction(Direction.DIRECTION_DOWN);} else if (ANGLE_8D_OF_2P <= angle && ANGLE_8D_OF_3P > angle) {// 左下mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);} else if (ANGLE_8D_OF_3P <= angle && ANGLE_8D_OF_4P > angle) {// 左mOnShakeListener.direction(Direction.DIRECTION_LEFT);} else if (ANGLE_8D_OF_4P <= angle && ANGLE_8D_OF_5P > angle) {// 左上mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);} else if (ANGLE_8D_OF_5P <= angle && ANGLE_8D_OF_6P > angle) {// 上mOnShakeListener.direction(Direction.DIRECTION_UP);} else if (ANGLE_8D_OF_6P <= angle && ANGLE_8D_OF_7P > angle) {// 右上mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);}break;default:break;}} else if (CallBackMode.CALL_BACK_MODE_STATE_CHANGE == mCallBackMode) {switch (mDirectionMode) {case DIRECTION_2_HORIZONTAL:// 左右方向if ((ANGLE_0 <= angle && ANGLE_HORIZONTAL_2D_OF_0P > angle || ANGLE_HORIZONTAL_2D_OF_1P <= angle && ANGLE_360 > angle) && tempDirection != Direction.DIRECTION_RIGHT) {// 右tempDirection = Direction.DIRECTION_RIGHT;mOnShakeListener.direction(Direction.DIRECTION_RIGHT);} else if (ANGLE_HORIZONTAL_2D_OF_0P <= angle && ANGLE_HORIZONTAL_2D_OF_1P > angle && tempDirection != Direction.DIRECTION_LEFT) {// 左tempDirection = Direction.DIRECTION_LEFT;mOnShakeListener.direction(Direction.DIRECTION_LEFT);}break;case DIRECTION_2_VERTICAL:// 上下方向if (ANGLE_VERTICAL_2D_OF_0P <= angle && ANGLE_VERTICAL_2D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN) {// 下tempDirection = Direction.DIRECTION_DOWN;mOnShakeListener.direction(Direction.DIRECTION_DOWN);} else if (ANGLE_VERTICAL_2D_OF_1P <= angle && ANGLE_360 > angle && tempDirection != Direction.DIRECTION_UP) {// 上tempDirection = Direction.DIRECTION_UP;mOnShakeListener.direction(Direction.DIRECTION_UP);}break;case DIRECTION_4_ROTATE_0:// 四個方向if (ANGLE_4D_OF_0P <= angle && ANGLE_4D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN_RIGHT) {// 右下tempDirection = Direction.DIRECTION_DOWN_RIGHT;mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);} else if (ANGLE_4D_OF_1P <= angle && ANGLE_4D_OF_2P > angle && tempDirection != Direction.DIRECTION_DOWN_LEFT) {// 左下tempDirection = Direction.DIRECTION_DOWN_LEFT;mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);} else if (ANGLE_4D_OF_2P <= angle && ANGLE_4D_OF_3P > angle && tempDirection != Direction.DIRECTION_UP_LEFT) {// 左上tempDirection = Direction.DIRECTION_UP_LEFT;mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);} else if (ANGLE_4D_OF_3P <= angle && ANGLE_360 > angle && tempDirection != Direction.DIRECTION_UP_RIGHT) {// 右上tempDirection = Direction.DIRECTION_UP_RIGHT;mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);}break;case DIRECTION_4_ROTATE_45:// 四個方向 旋轉45度if ((ANGLE_0 <= angle && ANGLE_ROTATE45_4D_OF_0P > angle || ANGLE_ROTATE45_4D_OF_3P <= angle && ANGLE_360 > angle) && tempDirection != Direction.DIRECTION_RIGHT) {// 右tempDirection = Direction.DIRECTION_RIGHT;mOnShakeListener.direction(Direction.DIRECTION_RIGHT);} else if (ANGLE_ROTATE45_4D_OF_0P <= angle && ANGLE_ROTATE45_4D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN) {// 下tempDirection = Direction.DIRECTION_DOWN;mOnShakeListener.direction(Direction.DIRECTION_DOWN);} else if (ANGLE_ROTATE45_4D_OF_1P <= angle && ANGLE_ROTATE45_4D_OF_2P > angle && tempDirection != Direction.DIRECTION_LEFT) {// 左tempDirection = Direction.DIRECTION_LEFT;mOnShakeListener.direction(Direction.DIRECTION_LEFT);} else if (ANGLE_ROTATE45_4D_OF_2P <= angle && ANGLE_ROTATE45_4D_OF_3P > angle && tempDirection != Direction.DIRECTION_UP) {// 上tempDirection = Direction.DIRECTION_UP;mOnShakeListener.direction(Direction.DIRECTION_UP);}break;case DIRECTION_8:// 八個方向if ((ANGLE_0 <= angle && ANGLE_8D_OF_0P > angle || ANGLE_8D_OF_7P <= angle && ANGLE_360 > angle) && tempDirection != Direction.DIRECTION_RIGHT) {// 右tempDirection = Direction.DIRECTION_RIGHT;mOnShakeListener.direction(Direction.DIRECTION_RIGHT);} else if (ANGLE_8D_OF_0P <= angle && ANGLE_8D_OF_1P > angle && tempDirection != Direction.DIRECTION_DOWN_RIGHT) {// 右下tempDirection = Direction.DIRECTION_DOWN_RIGHT;mOnShakeListener.direction(Direction.DIRECTION_DOWN_RIGHT);} else if (ANGLE_8D_OF_1P <= angle && ANGLE_8D_OF_2P > angle && tempDirection != Direction.DIRECTION_DOWN) {// 下tempDirection = Direction.DIRECTION_DOWN;mOnShakeListener.direction(Direction.DIRECTION_DOWN);} else if (ANGLE_8D_OF_2P <= angle && ANGLE_8D_OF_3P > angle && tempDirection != Direction.DIRECTION_DOWN_LEFT) {// 左下tempDirection = Direction.DIRECTION_DOWN_LEFT;mOnShakeListener.direction(Direction.DIRECTION_DOWN_LEFT);} else if (ANGLE_8D_OF_3P <= angle && ANGLE_8D_OF_4P > angle && tempDirection != Direction.DIRECTION_LEFT) {// 左tempDirection = Direction.DIRECTION_LEFT;mOnShakeListener.direction(Direction.DIRECTION_LEFT);} else if (ANGLE_8D_OF_4P <= angle && ANGLE_8D_OF_5P > angle && tempDirection != Direction.DIRECTION_UP_LEFT) {// 左上tempDirection = Direction.DIRECTION_UP_LEFT;mOnShakeListener.direction(Direction.DIRECTION_UP_LEFT);} else if (ANGLE_8D_OF_5P <= angle && ANGLE_8D_OF_6P > angle && tempDirection != Direction.DIRECTION_UP) {// 上tempDirection = Direction.DIRECTION_UP;mOnShakeListener.direction(Direction.DIRECTION_UP);} else if (ANGLE_8D_OF_6P <= angle && ANGLE_8D_OF_7P > angle && tempDirection != Direction.DIRECTION_UP_RIGHT) {// 右上tempDirection = Direction.DIRECTION_UP_RIGHT;mOnShakeListener.direction(Direction.DIRECTION_UP_RIGHT);}break;default:break;}}}}/*** 回調* 結束*/private void callBackFinish() {tempDirection = Direction.DIRECTION_CENTER;if (null != mOnAngleChangeListener) {mOnAngleChangeListener.onFinish();}if (null != mOnShakeListener) {mOnShakeListener.onFinish();}}/*** 回調模式*/public enum CallBackMode {// 有移動就立刻回調CALL_BACK_MODE_MOVE,// 只有狀態變化的時候才回調CALL_BACK_MODE_STATE_CHANGE,//只有狀態變化或者距離變化的時候才回調CALL_BACK_MODE_STATE_DISTANCE_CHANGE}/*** 設置回調模式** @param mode 回調模式*/public void setCallBackMode(CallBackMode mode) {mCallBackMode = mode;}/*** 搖桿支持幾個方向*/public enum DirectionMode {DIRECTION_2_HORIZONTAL,// 橫向 左右兩個方向DIRECTION_2_VERTICAL, // 縱向 上下兩個方向DIRECTION_4_ROTATE_0, // 四個方向DIRECTION_4_ROTATE_45, // 四個方向 旋轉45度DIRECTION_8 // 八個方向}/*** 方向*/public enum Direction {DIRECTION_LEFT, // 左DIRECTION_RIGHT, // 右DIRECTION_UP, // 上DIRECTION_DOWN, // 下DIRECTION_UP_LEFT, // 左上DIRECTION_UP_RIGHT, // 右上DIRECTION_DOWN_LEFT, // 左下DIRECTION_DOWN_RIGHT, // 右下DIRECTION_CENTER // 中間}/*** 添加搖桿搖動角度的監聽** @param listener 回調接口*/public void setOnAngleChangeListener(OnAngleChangeListener listener) {mOnAngleChangeListener = listener;}/*** 添加搖動的監聽** @param directionMode 監聽的方向* @param listener 回調*/public void setOnShakeListener(DirectionMode directionMode, OnShakeListener listener) {mDirectionMode = directionMode;mOnShakeListener = listener;}/*** 添加搖動的距離變化*/public void setOnDistanceLevelListener(OnDistanceLevelListener listenr) {mOnDistanceLevelListener = listenr;}/*** 搖動方向監聽接口*/public interface OnShakeListener {// 開始void onStart();/*** 搖動方向** @param direction 方向*/void direction(Direction direction);// 結束void onFinish();}/*** 搖動角度的監聽接口*/public interface OnAngleChangeListener {// 開始void onStart();/*** 搖桿角度變化** @param angle 角度[0,360)*/void angle(double angle);// 結束void onFinish();}/*** 搖動距離*/public interface OnDistanceLevelListener {void onDistanceLevel(int level);}private CallBackMode getCallBackMode(int mode) {switch (mode) {case 0:return CallBackMode.CALL_BACK_MODE_MOVE;case 1:return CallBackMode.CALL_BACK_MODE_STATE_CHANGE;}return mCallBackMode;} }

第三步:添加到布局:

特別注意,需要先導入自定義的屬性:xmlns:rocker="http://schemas.android.com/apk/res-auto"

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:rocker="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.liang.myrockerview_view.MainActivity"><!--數據顯示--><LinearLayoutandroid:layout_width="130dp"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="XY軸數據"android:textAlignment="center" /><TextViewandroid:id="@+id/directionXY_Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="當前方向:"/><TextViewandroid:id="@+id/angleXY_Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="當前角度:"/><TextViewandroid:id="@+id/levelXY_Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="當前偏移級別:"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Z軸數據"android:textAlignment="center"/><TextViewandroid:id="@+id/directionZ_Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="當前方向:"/><TextViewandroid:id="@+id/angleZ_Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="當前角度:"/><TextViewandroid:id="@+id/levelZ_Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="當前偏移級別:"/></LinearLayout><!--搖桿--><com.liang.myrockerview_view.MyRockerViewandroid:id="@+id/rockerXY_View"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="25dp"android:layout_alignParentBottom="true"rocker:areaBackground="@mipmap/rocker_base"rocker:rockerBackground="@mipmap/rocker"rocker:rockerSpeedLevel="10"rocker:rockerCallBackMode="CALL_BACK_MODE_STATE_CHANGE"rocker:rockerScale="0.5"/><com.liang.myrockerview_view.MyRockerViewandroid:id="@+id/rockerZ_View"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="25dp"android:layout_alignParentRight="true"android:layout_alignParentBottom="true"rocker:areaBackground="@mipmap/rocker_base"rocker:rockerBackground="@mipmap/rocker"rocker:rockerSpeedLevel="10"rocker:rockerCallBackMode="CALL_BACK_MODE_STATE_CHANGE"rocker:rockerScale="0.5"/></RelativeLayout>

?

第四部:實例化并添加點擊事件

//方向有改變時回調 mRockerViewXY = (MyRockerView) findViewById(R.id.rockerXY_View);//8方向 mRockerViewZ = (MyRockerView) findViewById(R.id.rockerZ_View);//2方向

?

//xy軸 //方向 mRockerViewXY.setOnShakeListener(MyRockerView.DirectionMode.DIRECTION_8, new MyRockerView.OnShakeListener() {@Overridepublic void onStart() {}@Overridepublic void direction(MyRockerView.Direction direction) {if (direction == MyRockerView.Direction.DIRECTION_CENTER){directionXY = ("當前方向:中心");}else if (direction == MyRockerView.Direction.DIRECTION_DOWN){directionXY = ("當前方向:下");}else if (direction == MyRockerView.Direction.DIRECTION_LEFT){directionXY = ("當前方向:左");}else if (direction == MyRockerView.Direction.DIRECTION_UP){directionXY = ("當前方向:上");}else if (direction == MyRockerView.Direction.DIRECTION_RIGHT){directionXY = ("當前方向:右");}else if (direction == MyRockerView.Direction.DIRECTION_DOWN_LEFT){directionXY = ("當前方向:左下");}else if (direction == MyRockerView.Direction.DIRECTION_DOWN_RIGHT){directionXY = ("當前方向:右下");}else if (direction == MyRockerView.Direction.DIRECTION_UP_LEFT){directionXY = ("當前方向:左上");}else if (direction == MyRockerView.Direction.DIRECTION_UP_RIGHT){directionXY = ("當前方向:右上");}Log.e(TAG, "XY軸"+directionXY);Log.e(TAG, "-----------------------------------------------" );directionXY_Text.setText(directionXY);}@Overridepublic void onFinish() {} }); //角度 mRockerViewXY.setOnAngleChangeListener(new MyRockerView.OnAngleChangeListener() {@Overridepublic void onStart() {}@Overridepublic void angle(double angle) {angleXY = ("當前角度:"+angle);Log.e(TAG, "XY軸"+angleXY);angleXY_Text.setText(angleXY);}@Overridepublic void onFinish() {} }); //級別 mRockerViewXY.setOnDistanceLevelListener(new MyRockerView.OnDistanceLevelListener() {@Overridepublic void onDistanceLevel(int level) {levelXY = ("當前距離級別:"+level);Log.e(TAG, "XY軸"+levelXY);levelXY_Text.setText(levelXY);} });


總結

以上是生活随笔為你收集整理的android虚拟摇杆的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日本一级一片免费视频 | 成人精品三级av在线看 | 国产偷亚洲偷欧美偷精品 | 国产成人无码精品亚洲 | www.色妞 | 成人手机av | 中国黄色录像一级片 | 亚洲在线一区 | 日本在线观看一区 | 91免费视 | 欧美日韩国产成人在线 | 国产日韩欧美不卡 | 国产人成无码视频在线观看 | 国产三级短视频 | 在线观看天堂av | 美女91网站 | 青青草视频免费看 | 久草中文在线视频 | 中文字幕人妻一区二区在线视频 | 欧美一级特黄aa大片 | 色婷婷久久一区二区三区麻豆 | 亚洲成人免费看 | 尤物视频在线观看免费 | 一区二区三区在线视频播放 | 色播综合网 | 爱情岛论坛亚洲品质自拍视频 | 国产精品国产三级国产专播精品人 | 精品人妻无码一区二区三区 | 天天亚洲 | 国产手机在线播放 | 国产成人无码一二三区视频 | 毛片一区| 欧美性视频网站 | 欧美成欧美va | www.成人在线视频 | 久久一级黄色片 | 污片在线看| 朴麦妮原版视频高清资源 | 九九精品视频免费 | av天堂一区二区三区 | 麻豆网站在线免费观看 | 久久免费视频99 | 中文字幕av解说 | 日韩精品在线观看免费 | 99热热久久 | 日韩aⅴ视频 | 538任你躁在线精品免费 | 午夜视频日韩 | 男ji大巴进入女人的视频 | 久久精品视频在线 | 末发成年娇小性xxxxx | 中国老妇性视频 | www视频在线观看 | 久久久www成人免费毛片 | 久久不卡影院 | 中文字幕国产 | 黄色污污网站 | 看全黄大色黄大片 | 欧美激情天堂 | 91理论片| 西川结衣在线观看 | 欧美成人短视频 | 日韩欧美一级大片 | 婷婷综合色 | 国产精品久久久久久久免费 | 久久av一区二区 | 欧美偷拍少妇精品一区 | 中文人妻熟女乱又乱精品 | 黄色av免费 | 中文在线а√在线8 | 成人v精品蜜桃久一区 | 国产无套视频 | 又黄又爽一区二区三区 | 免费看一级黄色大全 | 婷婷成人综合网 | 自拍偷拍 国产 | 全部毛片永久免费看 | √天堂8资源中文在线 | 黄色香蕉网| 亚洲国产欧美精品 | 欧美国产一级片 | 亚洲 小说区 图片区 都市 | 99国产精品视频免费观看一公开 | 国产女人呻吟高潮抽搐声 | 欧美日韩aa | 久久精品女人毛片国产 | 日本一级片免费看 | 亚洲剧情在线 | 亚洲av综合色区无码一区 | 青青欧美 | 国产区视频在线观看 | 亚洲暴爽| 日韩美女免费线视频 | 尤物精品在线 | 欧美黄色一区 | 国产高潮国产高潮久久久91 | 麻豆91在线| 成人免费在线视频观看 | 老鸭窝久久 |