Android 幸運轉盤實現邏輯

一、前言

幸運轉盤在很多app中都有,也有很多現實的例子,不過這個難度并不是如何讓轉盤轉起來,真正的難度是如何統一個方向轉動,且轉到指定的目標區域(中獎概率從來不是隨機的),當然還不能太假,需要有一定的位置偏移。

效果預覽

二、邏輯實現

2.1 平分區域

由于圓周是360度,品分每個站preDegree,那么起點和終點

但是為了讓X軸初始化時對準第一個區域的中心,我們做一下小偏移,逆時針旋轉一下

//圓點起始角度 ,可以理解為index=0的起始角度,我們以index=0位參考點
float zeroStartDegree = 0 + -perDegree / 2;
float endStartDegree = 0 + perDegree / 2;

那么每個區域的起始角度如下

float startDegree = i* perDegree  - perDegree / 2;
float endDegree = i * perDegree + perDegree / 2 ;

2.2 畫弧

很簡單,不需要計算每個分區的大小,因為平分的是同一個大圓,因此Rect是大圓的范圍,但也要記住 ,弧的起始角+繪制角度不能大于等于360,最大貌似是359.9998399

canvas.drawArc(rectF, startDegree, endDegree - startDegree, true, mDrawerPaint);

2.3 文字繪制

由于Canvas.drawText不能設置角度,那么意味著不能直接繪制,需要做一定的角度轉換,要么旋轉Canvas坐標,要們旋轉Path,這次我們選后者吧。

使用Path的原因是,他不僅具備矢量性質(不失真),而且還能轉動文字的方向和從有到左繪制。

           //計算出中心角度 float centerRadius = (float) Math.toRadians((startDegree + endDegree)/2F);float measuredTextWidth = mDrawerPaint.measureText(item.text);float measuredTextHeight = getTextHeight(mDrawerPaint,item.text);float innerRadius = maxRadius - 2* measuredTextHeight;float cx = (float) ((innerRadius - measuredTextHeight)   * Math.cos(centerRadius));float cy = (float) ((innerRadius - measuredTextHeight)  * Math.sin(centerRadius));double degreeOffset = Math.asin((measuredTextWidth/2F)/innerRadius);float startX= (float) (innerRadius * Math.cos(centerRadius - degreeOffset));float startY = (float) (innerRadius * Math.sin(centerRadius - degreeOffset));float endX= (float) ((innerRadius) * Math.cos(centerRadius + degreeOffset));float endY = (float) ((innerRadius) * Math.sin(centerRadius + degreeOffset));path.reset();path.moveTo(startX,startY);path.lineTo(endX,endY);//這里使用Path的原因是文本角度無法設置canvas.drawTextOnPath(item.text,path,0,0,mDrawerPaint);

2.4 核心邏輯

老板不會讓中獎率隨機的,萬一你中大獎了,老板還得出錢或者花部門經費,因此,必須指定中獎物品,可以讓你100%中獎,也能讓你100%不中獎,要看老板心情,所以掘金的轉盤你玩不玩都已經固定好你的勝率了。

計算出目標物品與初始角度的,注意時初始角度,而不是轉過后的角度算起,為什么呢?原因是你按轉過的角度計算復雜度就會提升,而從起始點計算,按照圓的三角函數定理,轉一圈就能繞過你轉動的角度 ,也就是?rotateDegree 最終會大于你當前的所停留的角度, 如果你在30度,那么要轉到20度的位置,肯定不會倒轉 需要 360 + 20,而360 +20大于30,所以,莫有必要從當前角度計算。

//圓點起始角度 ,可以理解為index=0的起始角度,我們以index=0位參考點
float zeroStartDegree = 0 + -perDegree / 2;
float endStartDegree = 0 + perDegree / 2;
//從圓點計算,要旋轉的角度
float targetDegree = (perDegree * (index - 1) + perDegree / 2);
float rotateDegree = zeroStartDegree - targetDegree;

算出來之后緊接著計算落點位置,這里需要隨機一下,不然看著很假,樣子還是要做的。

但是這里我們一氣呵成:

【1】計算旋轉速度,主要是防止逆時針旋轉,其詞轉一下就到了,也不太真實,次數利用了三角函數定理

三角函數定理 n*360 + degree 和 degree三角函數值最終夾角是等價的

【2】旋轉次數,這里我們用duration/speedTime,實際上還可以用圓周邊長除以duration,也是可以的,當然也要加一定的倍數。

【3】計算出隨機落點位置,不能騎線,也不能超過指定區域

        //防止逆時針旋轉 (三角函數定理  n*360 + degree 和 degree最終夾角是等價的 )while (rotateDegree < offsetDegree) {rotateDegree += 360;}if (speedTime == 0) {speedTime = 100L;}long count = duration / speedTime - 1;  //計算額外旋轉圈數while (count >= 0) {rotateDegree += 360;  //三角函數定理  n*360 + degree 和 degree最終夾角是等價的count--; //算出轉多少圈}float targetStartDegree = rotateDegree - perDegree / 2;float targetEndDegree = rotateDegree + perDegree / 2;float currentOffsetDegree = offsetDegree;// float targetOffsetDegree  = (targetStartDegree +  targetEndDegree)/2 ;//讓指針指向有一定的隨機性float targetOffsetDegree = (float) (targetStartDegree + (targetEndDegree - targetStartDegree) * Math.random());

2.5 全部代碼

public class LuckWheelView extends View {Path path  = new Path();private final DisplayMetrics mDM;private TextPaint mArcPaint;private TextPaint mDrawerPaint;private int maxRadius;private float perDegree;private long duration = 5000L;private List<Item> items = new ArrayList<>();private RectF rectF = new RectF();private float offsetDegree = 0;private TimeInterpolator timeInterpolator = new AccelerateDecelerateInterpolator();private long speedTime = 1000L; //旋轉一圈需要多少時間private ValueAnimator animator = null;public LuckWheelView(Context context) {this(context, null);}public LuckWheelView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public LuckWheelView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mDM = getResources().getDisplayMetrics();initPaint();}public void setRotateIndex(int index) {if (items == null || items.size() <= index) {return;}//圓點起始角度 ,可以理解為index=0的起始角度,我們以index=0位參考點float zeroStartDegree = 0 + -perDegree / 2;float endStartDegree = 0 + perDegree / 2;//從圓點計算,要旋轉的角度float targetDegree = (perDegree * (index - 1) + perDegree / 2);float rotateDegree = zeroStartDegree - targetDegree;//防止逆時針旋轉 (三角函數定理  n*360 + degree 和 degree最終夾角是等價的 )while (rotateDegree < offsetDegree) {rotateDegree += 360;}if (speedTime == 0) {speedTime = 100L;}long count = duration / speedTime - 1;  //計算額外旋轉圈數while (count >= 0) {rotateDegree += 360;  //三角函數定理  n*360 + degree 和 degree最終夾角是等價的count--;}float targetStartDegree = rotateDegree - perDegree / 2;float targetEndDegree = rotateDegree + perDegree / 2;float currentOffsetDegree = offsetDegree;// float targetOffsetDegree  = (targetStartDegree +  targetEndDegree)/2 ;//讓指針指向有一定的隨機性float targetOffsetDegree = (float) (targetStartDegree + (targetEndDegree - targetStartDegree) * Math.random());if (animator != null) {animator.cancel();}
//起點肯定要從當前角度算起,不然會閃一下回到原點animator = ValueAnimator.ofFloat(currentOffsetDegree, targetOffsetDegree).setDuration(duration);animator.setInterpolator(timeInterpolator);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {offsetDegree = (float) animation.getAnimatedValue();postInvalidate();}});animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);offsetDegree = offsetDegree % 360;}});animator.start();}private void initPaint() {// 實例化畫筆并打開抗鋸齒mArcPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);mArcPaint.setAntiAlias(true);mArcPaint.setStyle(Paint.Style.STROKE);mArcPaint.setStrokeCap(Paint.Cap.ROUND);mDrawerPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);mDrawerPaint.setAntiAlias(true);mDrawerPaint.setStyle(Paint.Style.FILL);mDrawerPaint.setStrokeCap(Paint.Cap.ROUND);mDrawerPaint.setStrokeWidth(5);mDrawerPaint.setTextSize(spTopx(14));}private float spTopx(float dp) {return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dp, getResources().getDisplayMetrics());}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);if (widthMode != MeasureSpec.EXACTLY) {widthSize = mDM.widthPixels / 2;}int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);if (heightMode != MeasureSpec.EXACTLY) {heightSize = widthSize / 2;}setMeasuredDimension(widthSize, heightSize);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width = getWidth();int height = getHeight();if (width == 0 || height == 0 || items == null || items.size() <= 0) {perDegree = 0;return;}maxRadius = Math.min(width / 2, height / 2);rectF.left = -maxRadius;rectF.top = -maxRadius;rectF.right = maxRadius;rectF.bottom = maxRadius;int size = items.size();int saveCount = canvas.save();canvas.translate(width * 1F / 2, height * 1F / 2); //平移坐標軸到view中心點canvas.rotate(-90); //逆時針旋轉坐標軸 90度perDegree = 360 * 1F / size;// rangeDegree = start ->end// rangeDegree.start = perDegree/2 + (i-1) * perDegree;// rangeDegree.end = perDegree/2 + (i) * perDegree;for (int i = 0; i < size; i++) {//由于我們讓第一個區域的中心點對準x軸了,所以(i-1)意味著從y軸負方向順時針轉動float startDegree = perDegree * (i - 1) + perDegree / 2 + offsetDegree;float endDegree = i * perDegree + perDegree / 2 + offsetDegree;Item item = items.get(i);mDrawerPaint.setColor(item.color);
//            double startDegreeRandians = Math.toRadians(startDegree); //x1
//            float x = (float) (maxRadius * Math.cos(startDegreeRandians));
//            float y = (float) (maxRadius * Math.sin(startDegreeRandians));
//            canvas.drawLine(0,0,x,y,mDrawerPaint);float centerRadius = (float) Math.toRadians((startDegree + endDegree)/2F);float measuredTextWidth = mDrawerPaint.measureText(item.text);float measuredTextHeight = getTextHeight(mDrawerPaint,item.text);float innerRadius = maxRadius - 2* measuredTextHeight;float cx = (float) ((innerRadius - measuredTextHeight)   * Math.cos(centerRadius));float cy = (float) ((innerRadius - measuredTextHeight)  * Math.sin(centerRadius));double degreeOffset = Math.asin((measuredTextWidth/2F)/innerRadius);float startX= (float) (innerRadius * Math.cos(centerRadius - degreeOffset));float startY = (float) (innerRadius * Math.sin(centerRadius - degreeOffset));float endX= (float) ((innerRadius) * Math.cos(centerRadius + degreeOffset));float endY = (float) ((innerRadius) * Math.sin(centerRadius + degreeOffset));path.reset();path.moveTo(startX,startY);path.lineTo(endX,endY);//這里使用Path的原因是文本角度無法設置canvas.drawArc(rectF, startDegree, endDegree - startDegree, true, mDrawerPaint);mDrawerPaint.setColor(Color.WHITE);canvas.drawCircle(cx,cy,5,mDrawerPaint);canvas.drawTextOnPath(item.text,path,0,0,mDrawerPaint);}canvas.drawLine(0, 0, maxRadius / 2F, 0, mDrawerPaint);canvas.restoreToCount(saveCount);}Rect textBounds = new Rect();//真實寬度 + 筆畫上下兩側間隙(符合文本繪制基線)private  int getTextHeight(Paint paint,String text) {paint.getTextBounds(text,0,text.length(),textBounds);return textBounds.height();}public void setItems(List<Item> items) {this.items.clear();this.items.addAll(items);invalidate();}public static class Item {Object tag;int color = Color.TRANSPARENT;String text;public Item(int color, String text) {this.color = color;this.text = text;}}}

2.6 使用方法

List<LuckWheelView.Item> items = new ArrayList<>();items.add(new LuckWheelView.Item(argb(random.nextFloat(),random.nextFloat(),random.nextFloat()), "金元寶"));items.add(new LuckWheelView.Item(argb(random.nextFloat(),random.nextFloat(),random.nextFloat()), "皮卡丘"));items.add(new LuckWheelView.Item(argb(random.nextFloat(),random.nextFloat(),random.nextFloat()), "1元紅包"));items.add(new LuckWheelView.Item(argb(random.nextFloat(),random.nextFloat(),random.nextFloat()), "全球旅行"));items.add(new LuckWheelView.Item(argb(random.nextFloat(),random.nextFloat(),random.nextFloat()), "K歌會員卡"));items.add(new LuckWheelView.Item(argb(random.nextFloat(),random.nextFloat(),random.nextFloat()), "雙肩包"));loopView.setItems(items);loopView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int index = (int) (Math.random() * items.size());Log.d("LuckWheelView", "setRotateIndex->" + items.get(index).text + ", index=" + index);loopView.setRotateIndex(index);}});

三、總結

本篇簡單而快捷的實現了幸運轉盤,難點主要是角度的轉換,一定要分析出初始角度和目標位置的夾角這一個定性標準,其詞作一些優化,就能實現幸運轉盤效果。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/209876.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/209876.shtml
英文地址,請注明出處:http://en.pswp.cn/news/209876.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

AI全棧大模型工程師(二十二)什么是模型訓練

文章目錄 ?? 這節課會帶給你還是寫在前面Fine-Tuning 有什么用:先看一個例子我有很多問題一、什么是:二、什么是模型2.1、通俗(不嚴謹)的說、模型是一個函數:2.2、一個最簡單的神經網絡三、什么是模型訓練3.1、模型訓練本質上是一個求解最優化問題的過程3.2、怎么求解3.…

人類的耳朵:聽覺的動態范圍

作者&#xff1a;聽覺健康 聽覺的動態范圍即可用的聽力范圍。在坐標系中&#xff0c;它可以表示為以聽閾和最大舒適級為界形成的區域&#xff0c;其坐標軸分別為頻率和聲壓級&#xff08;刺激持續時間在某種程度上對其產生影響&#xff09;。是什么因素決定了人類聽力的極限&am…

隨機森林回歸模型,SHAP庫可視化

隨機森林回歸模型 創建一個隨機森林回歸模型&#xff0c;訓練模型&#xff0c;然后使用SHAP庫解釋模型的預測結果&#xff0c;并將結果可視化。 具體步驟如下&#xff1a; 首先&#xff0c;代碼導入了所需的庫&#xff0c;包括matplotlib、shap、numpy和sklearn.ensemble。ma…

Compilation failureFailure executing javac, but could not parse the error

記一次maven編譯錯誤導致的打包失敗問題。錯誤如下 Compilation failure Failure executing javac, but could not parse the error: javac: Ч ? : ? ? : javac <options> <source files> -help г ? ? 排查路徑如下&#xff1a; 1&#xff…

[原創] FPGA的JTAG燒錄不穩定或燒錄失敗原因分析

一、電路故障背景 打板回來常會出現燒錄不良&#xff0c;調試是一個技術活&#xff0c;如果燒錄不過關&#xff0c;一切白搭。 二、常見JTAG故障原因如下&#xff1a; 1、ESD防護器件焊接不良&#xff1b; 電路板給生產部分焊接&#xff0c;發現元器件虛焊&#xff0c;特別是…

【MySQL】MySQL的varchar字段最大長度是65535?

在MySQL建表sql里,我們經常會有定義字符串類型的需求。 CREATE TABLE `user` ( `name` varchar(100) NOT NULL DEFAULT COMMENT 名字) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ; 比方說user表里的名字,就是個字符串。MySQL里有兩個類型比較適合這個場景。 char和varchar。…

我嘗試用 AI 來做數據分析,結果差強人意!

大家好&#xff0c;我是木川 工作中經常會需要分析數據 1、統計分析&#xff0c;計算某項指標的均值、分位數、標準差等 2、相關性分析&#xff0c;比如分析銷售額與顧客年齡、顧客性別、促銷活動等的相關性 3、可視化分析&#xff0c;比如繪制柱狀圖、折線圖、散點圖等 有了 A…

幾種排序的實現

直接插入排序 直接插入排序是一種簡單的插入排序法&#xff0c;其基本思想是&#xff1a; 把待排序的記錄按其關鍵碼值的大小逐個插入到一個已經排好序的有序序列中&#xff0c;直到所有的記錄插入完為止&#xff0c;得到一個新的有序序列 。 實際中我們玩撲克牌時&#xff…

交付《啤酒游戲經營決策沙盤》的項目

感謝首富客戶連續兩年的邀請&#xff0c;交付《啤酒游戲經營決策沙盤》的項目&#xff0c;下周一JSTO首席學習官Luna想讓我分享下系統思考與投資理財&#xff0c;想到曾經看過的一本書《深度思維》&#xff0c;看到一些結構來預判未來。不僅僅可以應用在企業經營和組織發展上&a…

Uncaught SyntaxError: Unexpected end of input (at manage.html:1:21) 的一個解

關于Uncaught SyntaxError: Unexpected end of input (at manage.html:1:21)的一個解 問題復現 <button onclick"deleteItem(${order.id},hire,"Orders")" >delete</button>報錯 原因 函數參數的雙引號和外面的雙引號混淆了&#xff0c;改成…

【vuex】

vuex 1 理解vuex1.1 vuex是什么1.2 什么時候使用vuex1.3 vuex工作原理圖1.4 搭建vuex環境1.5 求和案例1.5.1 vue方式1.5.2 vuex方式 2 vuex核心概念和API2.1 getters配置項2.2 四個map方法的使用2.2.1 mapState方法2.2.2 mapGetters方法2.2.3 mapActions方法2.2.4 mapMutations…

買賣股票的最佳時機算法(leetcode第121題)

題目描述&#xff1a; 給定一個數組 prices &#xff0c;它的第 i 個元素 prices[i] 表示一支給定股票第 i 天的價格。你只能選擇 某一天 買入這只股票&#xff0c;并選擇在 未來的某一個不同的日子 賣出該股票。設計一個算法來計算你所能獲取的最大利潤。返回你可以從這筆交易…

“HALCON error #2454:HALCON handle was already cleared in operator set_draw“

分析&#xff1a;錯誤提示是窗口句柄已經被刪除&#xff0c;這是因為前邊的一句 HOperatorSet.CloseWindow(hWindowControl1.HalconWindow); 關掉了窗口&#xff0c;屏蔽或刪除即可。

UDS診斷 10服務的肯定響應碼后面跟著一串數據的含義,以及診斷報文格式定義介紹

一、首先看一下10服務的請求報文和肯定響應報文格式 a.診斷儀發送的請求報文格式 b.ECU回復的肯定響應報文格式 c.肯定響應報文中參數定義 二、例程數據解析 a.例程數據 0.000000 1 725 Tx d 8 02 10 03 00 00 00 00 00 0.000806 1 7A5 Rx d 8 06 50 03 00 32 01 F4 CC …

Brushed DC mtr--PIC

PIC use brushed DC mtr fundmental. Low-Cost Bidirectional Brushed DC Motor Control Using the PIC16F684 DC mtr & encoder

《opencv實用探索·八》圖像模糊之均值濾波、高斯濾波的簡單理解

1、前言 什么是噪聲&#xff1f; 該像素與周圍像素的差別非常大&#xff0c;導致從視覺上就能看出該像素無法與周圍像素組成可識別的圖像信息&#xff0c;降低了整個圖像的質量。這種“格格不入”的像素就被稱為圖像的噪聲。如果圖像中的噪聲都是隨機的純黑像素或者純白像素&am…

TailwindCSS 如何設置 placeholder 的樣式

前言 placeholder 在前端多用于 input、textarea 等任何輸入或者文本區域的標簽&#xff0c;它用戶在用戶輸入內容之前顯示一些提示。瀏覽器自帶的 placeholder 樣式可能不符合設計規范&#xff0c;此時就需要通過 css 進行樣式美化。 當項目中使用 TailwindCSS 處理樣式時&a…

JAVA程序如何打jar和war問題解決

背景: 近期研究一個代碼審計工具 需要jar包 jar太多了 可以將jar 打成war包 首先看下程序目錄結構 pom.xml文件內容 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"ht…

Android12 WIFI 無法提供互聯網連接

平臺 RK3588 Android 12 問題描述 ConnectivityService是Android系統中負責處理網絡連接的服務之一。它負責管理設備的網絡連接狀態&#xff0c;包括Wi-Fi、移動數據、藍牙等。 在Android系統中&#xff0c;ConnectivityService提供了一些關鍵功能&#xff0c;包括但不限于…

Spring Boot Async:從入門到精通,原理詳解與最佳實踐

Spring Boot 的異步功能&#xff08;Async&#xff09;允許我們將某些任務異步執行&#xff0c;而不會阻塞主線程。這對于處理耗時的操作非常有用&#xff0c;如發送電子郵件、生成報表、調用外部 API 等。通過異步處理&#xff0c;我們可以釋放主線程&#xff0c;讓它繼續處理…