Android中的Shape,RoundRectShape,ArcShape, OvalShape

來源:互聯網
上載者:User

標籤:androi   edr   int   com   ctf   ack   需要   http   tco   


   在做Android的項目的時候碰到一個在在代碼中動態給一個Group添加一個有些圓角的背景沒有用shape.xml檔案來搞用的代碼,看了好一會了才明白RoundRectShape各個參數的意思,記錄下來省的以後再忘。先看官網的一個圖,表示了這個幾個類之間的繼承關係

1. RoundRectShape

float[] outerRadii = {20, 20, 30, 30, 40, 40, 50, 50};//外矩形 左上、右上、右下、左下的圓角半徑RectF inset = new RectF(100, 100, 100, 100);//內矩形距外矩形,左上方x,y距離, 右下角x,y距離float[] innerRadii = {20, 20, 20, 20, 20, 20, 20, 20};//內矩形 圓角半徑RoundRectShape roundRectShape = new RoundRectShape(outerRadii, inset, innerRadii);ShapeDrawable drawable = new ShapeDrawable(roundRectShape);drawable.getPaint().setColor(Color.BLACK);drawable.getPaint().setAntiAlias(true);drawable.getPaint().setStyle(Paint.Style.STROKE);//描邊mImage.setBackground(drawable);


   代碼中RoundRectShape(float[] outerRadii, RectF inset,float[] innerRadii)有三個參數,第一個和第二個都是8個數字數組,表示的的矩形的4個角的圓形半徑,剛開始就疑惑在這個8個數字上了,怎麼也想不明白,測試了幾次弄懂了。 這8個數組分別從左上方開始表示各個弧度的半徑,比如說左上方 左上方有兩個邊組成,左邊和上邊,第一個數字表示的左邊這條邊最上面的半徑,第二個表示上邊串連處圓形的半徑,從左上方,右上方,右下角,左下角依次類推正好八個數字。第一個參數表示的外邊角 第三個參數表示的內邊角,也就是大矩形套小矩形。 第二個參數表示的內矩形的位置,距離大矩形左,上,右,下的距離。 如果後面兩個參數都為null的話就只有一個大矩形。結果如下:

2. ArcShape 繪製圓形或者扇形

ArcShape shape = new ArcShape(0, -300);ShapeDrawable drawable = new ShapeDrawable(shape);drawable.getPaint().setColor(Color.BLACK);drawable.getPaint().setAntiAlias(true);drawable.getPaint().setStyle(Paint.Style.STROKE);mImage.setBackground(drawable);

 

  ArcShape(float startAngle, float sweepAngle) 有兩個參數,起始弧度,需要跨越的弧度,上面的例子中寫的是負數,則逆時針畫弧,如果是正數,則順時針畫弧. 如果是360度,則是一個圈,圓的半徑即大小你的ImageView本身來決定。效果如下:

3. OvalShape 橢圓

OvalShape ovalShape = new OvalShape();ShapeDrawable drawable = new ShapeDrawable(ovalShape);drawable.getPaint().setColor(Color.BLACK);drawable.getPaint().setAntiAlias(true);drawable.getPaint().setStyle(Paint.Style.STROKE);mImage.setBackground(drawable);

  

畫一個橢圓,同樣橢圓的寬高由你的載體來決定,我這裡是ImageView,需要注意的是**如果ImageView的寬和高相等就是一個圓**,效果如下:

 

Android中的Shape,RoundRectShape,ArcShape, OvalShape

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.