網站首頁 編程語言 正文
Material Design 系列
- Material Design UI方案使用講解
- Material TextInputLayout使用詳解
- Material ShapeableImageView 使用詳解
ShapeableImageView
- 前言
- 屬性&方法
- 樣式控制
- 關于Stroke
- 總結
博客創建時間:2022.08.23
博客更新時間:2022.08.05
以Android studio build=7.0.0,SDKVersion 31來分析講解。如圖文和網上其他資料不一致,可能是別的資料版本較低而已。
前言
繼承自ImageView,可以為image添加描邊大小、顏色,以及圓角、裁切等,這得益于它新增了一個屬性shapeAppearance,具體實現在ShapeAppearanceModel,可以通過style來配置,也可以通過代碼實現。
屬性&方法
屬性 | 說明 | 參數及含義 |
---|---|---|
shapeAppearance | ShapeableImageView的形狀外觀樣式 | |
shapeAppearanceOverlay | ShapeableImageView的形狀外觀疊加樣式 | |
strokeColor | 描邊顏色 | |
strokeWidth | 描邊寬度 | |
cornerFamily | 四個角shape屬性/樣式 | -rounded: 圓角0、-cut: 切角1 |
cornerSize | 四個角弧度 | 可以是具體DP,也可以是%,以控件高為準 |
cornerFamilyTopLeft | 左上shape屬性/樣式 | -rounded: 圓角0、-cut: 切角1 |
cornerFamilyTopRight | 右上shape屬性/樣式 | -rounded: 圓角0、-cut: 切角1 |
cornerFamilyBottomRight | 右下shape屬性/樣式 | -rounded: 圓角0、-cut: 切角1 |
cornerFamilyBottomLeft | 左下shape屬性/樣式 | -rounded: 圓角0、-cut: 切角1 |
cornerSizeTopLeft | 左上弧度 | |
cornerSizeTopRight | 右上弧度 | |
cornerSizeBottomRight | 右下弧度 | |
cornerSizeBottomLeft | 左下弧度 |
樣式控制
ShapeableImageView 的樣式只要由shapeAppearanceOverlay或shapeAppearance控制。
ShapeableImageView本質是通過ViewOutlineProvider實現剪裁的。ViewOutlineProvider的效率比傳統的Xfermode進行剪裁的方式高很多。
<!-- 圓角圖片 -->
<!-- shapeAppearanceOverlay或shapeAppearance 加載style -->
<!-- strokeColo描邊顏色 -->
<!-- strokeWidth描邊寬度 -->
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="1dp"
android:src="@mipmap/home3"
app:shapeAppearanceOverlay="@style/roundedCornerImageStyle"
app:strokeColor="#F44336"
app:strokeWidth="2dp" />
1. 圓形圖
<!-- 圓形圖片 -->
<style name="circleImageStyle">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
2、 菱形
<!-- 菱形圖片 -->
<style name="diamondImageStyle">
<item name="cornerFamily">cut</item>
<item name="cornerSize">50%</item>
</style>
3. 水滴形
<!-- 水滴 -->
<style name="waterImageStyle">
<item name="cornerFamilyBottomLeft">rounded</item>
<item name="cornerFamilyBottomRight">rounded</item>
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyTopRight">rounded</item>
<item name="cornerSizeBottomLeft">25dp</item>
<item name="cornerSizeBottomRight">25dp</item>
<item name="cornerSizeTopLeft">70%</item>
<item name="cornerSizeTopRight">70%</item>
</style>
4. 火箭頭
<!-- 火箭頭圖片 -->
<style name="rocketImageStyle">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyTopRight">rounded</item>
<item name="cornerSizeTopLeft">70%</item>
<item name="cornerSizeTopRight">70%</item>
</style>
5. 葉子形
<!-- 葉子圖片 -->
<style name="leafImageStyle">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyBottomRight">rounded</item>
<item name="cornerSizeTopLeft">70%</item>
<item name="cornerSizeBottomRight">70%</item>
</style>
6. 八角形
<!-- 切角圖片 -->
<style name="cutImageStyle">
<item name="cornerFamily">cut</item>
<item name="cornerSize">28%</item>
</style>
7. 圓角
<!-- 圓角圖片 -->
<style name="roundedCornerImageStyle">
<item name="cornerFamily">rounded</item>
<!-- 可以是具體的Dp-->
<item name="cornerSize">25dp</item>
<!-- 也可以是%,以高為基準-->
<!-- <item name="cornerSize">15%</item>-->
</style>
8. tip形
<!-- tip圖片 -->
<style name="tipImageStyle">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerSizeTopLeft">50%</item>
<item name="cornerFamilyBottomLeft">rounded</item>
<item name="cornerSizeBottomLeft">50%</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerSizeTopRight">50%</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeBottomRight">50%</item>
</style>
9.代碼控制
mBinding.simageView.shapeAppearanceModel = ShapeAppearanceModel.builder()
.setAllCorners(CornerFamily.ROUNDED, 20f)
.setTopLeftCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
.setTopRightCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
.setBottomRightCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
.setBottomLeftCorner(CornerFamily.CUT, RelativeCornerSize(0.3f))
.setAllCornerSizes(ShapeAppearanceModel.PILL)
.setTopLeftCornerSize(20f)
.setTopRightCornerSize(RelativeCornerSize(0.5f))
.setBottomLeftCornerSize(10f)
.setBottomRightCornerSize(AbsoluteCornerSize(30f))
.build()
關于Stroke
ShapeableImageView指定strokeWidth描邊的時候,其描邊會被覆蓋掉一半,如strokeWidth=4dp,上下左右會被覆蓋,實際的效果是只有2dp被顯示。
ShapeableImageView指定strokeWidth描邊的時候,沒有設置padding,那筆畫可能一半看不見,需要設置padding, 設置描邊的時候,需要添加padding屬性,padding的值為strokeWidth的一半。
總結
有興趣可以查看源碼源碼Demo:https://gitee.com/luofaxin/CodeAanalysis.git
相關鏈接:
- Material Design UI方案使用講解
- Material TextInputLayout使用詳解
- Material ShapeableImageView 使用詳解
擴展鏈接:
- 強大無匹的自定義下拉列表
- Lottie動畫 輕松使用
- PNG、JPG等普通圖片高保真轉SVG圖
- Android 完美的蒙層方案
博客書寫不易,您的點贊收藏是我前進的動力,千萬別忘記點贊、 收藏 ^ _ ^ !
原文鏈接:https://blog.csdn.net/luo_boke/article/details/125879510
相關推薦
- 2022-03-08 C語言設計前中后隊列實例代碼_C 語言
- 2022-05-25 Flutter?StaggeredGridView實現瀑布流效果_Android
- 2022-02-14 同一個docker鏡像,如何拉起多個docker container?
- 2022-04-09 SpringBoot 動態過濾自動配置類
- 2022-07-24 .Net行為型設計模式之中介者模式(Mediator)_基礎應用
- 2022-07-15 ASP.NET?Core獲取正確查詢字符串參數示例_實用技巧
- 2022-03-04 如何在uni-app中選擇一個合適的UI組件庫
- 2022-12-22 C++?STL標準庫之std::list使用介紹及用法詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支