日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发 ShapeDrawable详解

發布時間:2024/4/17 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。