Android开发 ShapeDrawable详解
生活随笔
收集整理的這篇文章主要介紹了
Android开发 ShapeDrawable详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android開發 ShapeDrawable詳解
前言
ShapeDrawable一開始我以為它是對應xml文件屬性里的shape的畫圖,后來發現我錯了... 其實ShapeDrawable更像是一共自由度更大跟偏向與實現draw()方法的一共圖像繪制類.所以,它的優點就是可以有更大的自由在代碼里繪制一個你想要的圖形,缺點是它搞起來有點不太方便,對于只需要簡單的圖形還不如GradientDrawable方便.
ShapeDrawable是靠new一個繼承Shape的類,來實現方便你的繪制的(其實底層原理就是View的draw()那套東西).我們可以查到一共有多少個shape,看如下圖片:
畫圓形?OvalShape
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());shapeDrawable.getPaint().setColor(Color.BLACK);Rect rect = new Rect();rect.top = 0;rect.left = 0;rect.bottom = 50;rect.right = 50;shapeDrawable.setBounds(rect);mTextView.setBackground(shapeDrawable);效果圖:
?
畫半圓?ArcShape
ShapeDrawable shapeDrawable = new ShapeDrawable(new ArcShape(0,180));//ArcShape參數 開始角度startAngle 要畫多少角度sweepAngle shapeDrawable.getPaint().setColor(Color.BLACK);Rect rect = new Rect();rect.top = 0;rect.left = 0;rect.bottom = 50;rect.right = 50;shapeDrawable.setBounds(rect);mTextView.setBackground(shapeDrawable);效果圖:
畫矩形?RectShape
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());shapeDrawable.getPaint().setColor(Color.BLACK);Rect rect = new Rect();rect.top = 0;rect.left = 0;rect.bottom = 50;rect.right = 50;shapeDrawable.setBounds(rect);mTextView.setBackground(shapeDrawable);效果圖:
畫內外雙層矩形,并且有圓角 RoundRectShape
float[] externalRound = {8, 8, 8, 8, 8, 8, 8, 8};//外部矩形的8個圓角半徑,為什么是8個? 因為這個居然是一個角2個半圓組成的(太精細了...)RectF distanceRectF = new RectF(10, 10, 10, 10); //內部矩形與外部矩形的距離float[] insideRound = {10, 10, 10, 10, 10, 10, 10, 10}; //內部矩形的8個圓角半徑值ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(externalRound, distanceRectF, insideRound));shapeDrawable.getPaint().setColor(Color.BLACK);Rect rect = new Rect();rect.top = 0;rect.left = 0;rect.bottom = 50;rect.right = 50;shapeDrawable.setBounds(rect);mTextView.setBackground(shapeDrawable);public RoundRectShape(@Nullable float[] outerRadii, @Nullable RectF inset, @Nullable float[] innerRadii)? 這個類一共有3個參數,我在上面的注解已經說明了.
注意!這3個參數都是可以為null的.意思就是,你可以取消任意一個.獲得一些其他效果,比如設置第二個和第三個參數為null,你就可以得到一個實心的圓角矩形.
效果圖:
?
畫任意形狀 PathShape
? 待續....
posted on 2019-07-06 14:18?觀心靜 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/guanxinjing/p/11142590.html
總結
以上是生活随笔為你收集整理的Android开发 ShapeDrawable详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FastJsonUtils 需要fast
- 下一篇: Android进阶知识:绘制流程(上)