Android开发 ShapeDrawable详解
前言
ShapeDrawable一開始我以為它是對應(yīng)xml文件屬性里的shape的畫圖,后來發(fā)現(xiàn)我錯(cuò)了... 其實(shí)ShapeDrawable更像是一共自由度更大跟偏向與實(shí)現(xiàn)draw()方法的一共圖像繪制類.所以,它的優(yōu)點(diǎn)就是可以有更大的自由在代碼里繪制一個(gè)你想要的圖形,缺點(diǎn)是它搞起來有點(diǎn)不太方便,對于只需要簡單的圖形還不如GradientDrawable方便.
ShapeDrawable是靠new一個(gè)繼承Shape的類,來實(shí)現(xiàn)方便你的繪制的(其實(shí)底層原理就是View的draw()那套東西).我們可以查到一共有多少個(gè)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參數(shù) 開始角度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);效果圖:
畫內(nèi)外雙層矩形,并且有圓角 RoundRectShape
float[] externalRound = {8, 8, 8, 8, 8, 8, 8, 8};//外部矩形的8個(gè)圓角半徑,為什么是8個(gè)? 因?yàn)檫@個(gè)居然是一個(gè)角2個(gè)半圓組成的(太精細(xì)了...)RectF distanceRectF = new RectF(10, 10, 10, 10); //內(nèi)部矩形與外部矩形的距離float[] insideRound = {10, 10, 10, 10, 10, 10, 10, 10}; //內(nèi)部矩形的8個(gè)圓角半徑值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)? 這個(gè)類一共有3個(gè)參數(shù),我在上面的注解已經(jīng)說明了.
注意!這3個(gè)參數(shù)都是可以為null的.意思就是,你可以取消任意一個(gè).獲得一些其他效果,比如設(shè)置第二個(gè)和第三個(gè)參數(shù)為null,你就可以得到一個(gè)實(shí)心的圓角矩形.
效果圖:
?
畫任意形狀 PathShape
? 待續(xù)....
posted on 2019-07-06 14:18?觀心靜 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/guanxinjing/p/11142590.html
總結(jié)
以上是生活随笔為你收集整理的Android开发 ShapeDrawable详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FastJsonUtils 需要fast
- 下一篇: 几篇文章