Android 自定義View 抽獎(jiǎng)大轉(zhuǎn)盤(3[完])

在這里將頭兩篇做了一個(gè)整合,兩個(gè)自定義View變成了一個(gè)自定義View

Android 自定義View 抽獎(jiǎng)大轉(zhuǎn)盤(1)
Android 自定義View 抽獎(jiǎng)大轉(zhuǎn)盤(2)
項(xiàng)目github鏈接 https://github.com/yukunkun/RotateView

鎮(zhèn)樓圖

S771011-14425058.jpg

主要是將外圍的圓環(huán)在內(nèi)部設(shè)置,增加了轉(zhuǎn)盤中間的點(diǎn)擊事件,部分修改如下

//大圓
 canvas.drawCircle(MinValue/2,MinValue/2,radius,backgroundPaint);

圓的繪制

  //繪制中間的圖
 Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.node);
 mRect = new Rect(MinValue/2-bitmap.getWidth()/2,MinValue/2-bitmap.getHeight()/2,MinValue/2+bitmap.getWidth()/2,MinValue/2+bitmap.getHeight()/2);
 canvas.drawBitmap(bitmap,null, mRect, null);
 drawSmallCircle(canvas,isYellow,MinValue); 

小圓

  private void drawSmallCircle(Canvas canvas, boolean FirstYellow, int minValue) {
         //位置要在內(nèi)部,每次的半徑相應(yīng)的變短一點(diǎn)
         int pointDistance = radius - Util.dip2px(context,9);
         //每次增加20度,也就是能夠得到18個(gè)點(diǎn)
         for(int i=0;i<=360;i+=20){
             //每次獲取到圓心的位置,由i控制位置
             int x = (int) (pointDistance * Math.sin(Util.change(i))) + minValue/2;
             int y = (int) (pointDistance * Math.cos(Util.change(i))) + minValue/2;
 
             //兩個(gè)不同顏色圓
             if(FirstYellow)
                 canvas.drawCircle(x,y,Util.dip2px(context,4),yellowPaint);
             else
                 canvas.drawCircle(x,y,Util.dip2px(context,4),whitePaint);
             FirstYellow = !FirstYellow;
         }
     }

這些方法被抽離出來(lái)了,圓盤的內(nèi)圓半徑有變化,每個(gè)圖片的定位有點(diǎn)變化

初始化半徑時(shí)變小了

   //內(nèi)邊距
    final int paddingLeft = getPaddingLeft()+Util.dip2px(context,ringLength);
    final int paddingRight = getPaddingRight()-Util.dip2px(context,ringLength);
    final int paddingTop = getPaddingTop()+Util.dip2px(context,ringLength);
    final int paddingBottom = getPaddingBottom()-Util.dip2px(context,ringLength);

初始化圖片的中心位置,有點(diǎn)偏移,否者位置不對(duì),有點(diǎn)遮擋文字

//確定圖片在圓弧中 中心點(diǎn)的位置
float x = (float) (xx + (mRadius /2 + mRadius/12-Util.dip2px(context,ringLength))* Math.cos(angle));
float y = (float) (yy + (mRadius /2 +mRadius/12-Util.dip2px(context,ringLength)) * Math.sin(angle));

下面是點(diǎn)擊中間的圖片的觸摸事件

/**
     * 點(diǎn)擊事件,點(diǎn)擊中心就開始動(dòng)畫
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getY();
                //點(diǎn)擊了中間位置
                if(x>mRect.left&&x<mRect.right&&y>mRect.top&&y<mRect.bottom){
                    startAnimation(pos);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
        }
        return super.onTouchEvent(event);
    }

如上所示,將中間的圖片位置獲取到,由MotionEvent.ACTION_DOWN來(lái)確定點(diǎn)擊

效果圖所開頭所示。有需要的話,還可以在這個(gè)上面修改。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容