Android中獲得一個扇形的剪裁區

來源:互聯網
上載者:User

 

這兩天因為項目有個地方需要用到扇形的剪裁區

 

在網上找相關資料

 

找到一個方法,但是用起來確實錯的,而且不能隨意的指定圓心的很縱座標

 

我查相關Api和看ApiDemo發現有畫扇形的方法

 

但是關於剪裁的沒有剪裁扇形的方法

 

有一個剪裁弧形的方法,

 

addArc(RectF oval, float startAngle, float sweepAngle)

 

Add the specified arc to the path as a new contour.

第一個參數是你弧形的畫的矩形範圍,就是你的圓弧畫滿後是內切的

 

第二個參數是起始的角度

 

第三個參數是移動的度數  ,所以終點度數是第二個參數加上第三個參數

 

效果如

 

 

我們需要的是個扇形,所以要加上一個從圓形到圓邊起始點再到圓邊終點的三角形 的剪裁區拼成一個剪裁區

 

就是三個點相連的路徑

 

用Path裡面的moveTo和linkTo方法便可以完成

 

我寫了一個方法 傳入圓心座標和半徑 加上起始和終點角度  代碼如下:

 

 

   /**

      * 返回一個扇形的剪裁區

      * @param canvas  //畫筆

      * @param center_X  //圓心X座標

      * @param center_Y  //圓心Y座標

      * @param r         //半徑

      * @param startAngle  //起始角度

      * @param sweepAngle  //終點角度

      * 

      */ 

    private void getSectorClip(Canvas canvas,float center_X,float center_Y,float r,float startAngle,float sweepAngle) 

    { 

        Path path = new Path(); 

         //下面是獲得一個三角形的剪裁區 

        path.moveTo(center_X, center_Y);  //圓心 

        path.lineTo((float)(center_X+r*Math.cos(startAngle* Math.PI / 180)),   //起始點角度在圓上對應的橫座標 

                (float)(center_Y+r*Math.sin(startAngle* Math.PI / 180)));    //起始點角度在圓上對應的縱座標 

        path.lineTo((float)(center_X+r*Math.cos(sweepAngle* Math.PI / 180)),  //終點角度在圓上對應的橫座標 

                (float)(center_Y+r*Math.sin(sweepAngle* Math.PI / 180)));   //終點點角度在圓上對應的縱座標 

        path.close(); 

//      //設定一個正方形,內切圓 

        RectF rectF = new RectF(center_X-r,center_Y-r,center_X+r,center_Y+r); 

        //下面是獲得弧形剪裁區的方法     

        path.addArc(rectF, startAngle, sweepAngle - startAngle);   

        canvas.clipPath(path); 

    } 

 

如下:

 

摘自 kbest的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.