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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android Shape使用

發布時間:2025/3/20 Android 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Shape使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

說明

在Android開發中,使用shape可以很方便的幫我們畫出想要的背景,相對于png圖片來說,使用shape可以減少安裝包的大小,而且能夠更好的適配不同的手機。

使用

先貼出官網上的說明:

<?xml version="1.0" encoding="utf-8"?> <shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> <gradient android:angle="integer" android:centerX="integer" android:centerY="integer" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:useLevel=["true" | "false"] /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <size android:width="integer" android:height="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> </shape>

這里面已經列出了所有的shape屬性。


從 android:shape=["rectangle" | "oval" | "line" | "ring"]
這里可以看出,shape可以畫四種圖形,分別是:矩形(rectangle)、橢圓(oval)、線(line)、圓環(ring)。

先上效果圖:


這里寫圖片描述

矩形(rectangle)

直角矩形:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/colorPrimary"></solid> </shape>

solid:填充顏色

圓角矩形:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp"></corners> <solid android:color="@color/colorPrimary"></solid> <padding android:bottom="12dp" android:left="12dp" android:right="12dp" android:top="12dp"></padding> </shape>

corners:圓角大小。

android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer"

android:radius:表示4個角的圓角大小;
還可以分別設置四個角的,使用下面四個屬性:android:topLeftRadius、android:topRightRadius、android:bottomLeftRadius、android:bottomRightRadius分別表示:左上、右上、左下、右下。

<padding android:bottom="12dp"android:left="12dp"android:right="12dp" android:top="12dp"></padding>

padding:設置內邊距。

無填充帶邊框:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp"></corners> <padding android:bottom="12dp" android:left="12dp" android:right="12dp" android:top="12dp"></padding> <stroke android:width="5dp" android:color="@color/colorAccent"></stroke> </shape>

stroke
android:width:邊框大小
android:color:邊框顏色

漸變:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/colorPrimary"></solid> <padding android:bottom="12dp" android:left="12dp" android:right="12dp" android:top="12dp"></padding> <!--angle 漸變角度,0:左到右;90:下到上;180:右到左;270:上到下--> <gradient android:startColor="@android:color/white" android:endColor="@android:color/black" android:angle="0"></gradient> </shape>

gradient:
android:startColor:漸變起始顏色
android:endColor:漸變結束顏色
android:angle:漸變角度:0:左到右;90:下到上;180:右到左;270:上到下

橢圓(oval)

一般用來畫圓。

純色的圓:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/colorPrimary"></solid> <size android:height="100dp" android:width="100dp"></size> </shape>

size的height和width設置為一樣大小就是一個圓了。
然后直接使用solid填充顏色即可。

漸變效果:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:height="100dp" android:width="100dp"></size> <gradient android:centerX="0.5" android:centerY="0.5" android:type="sweep" android:startColor="@color/colorPrimary" android:endColor="@color/colorAccent"></gradient> </shape>

android:centerX:表示漸變的X軸起始位置,范圍0-1,0.5表示圓心。
android:centerY:表示漸變的Y軸起始位置,范圍0-1,0.5表示圓心。
android:type:漸變類型,有三種
分別是:
linear 線性漸變,默認的漸變類型
radial 放射漸變,設置該項時,android:gradientRadius也必須設置
sweep 掃描性漸變

線(line)

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"> <stroke android:width="1dp" android:color="@color/colorAccent" android:dashGap="3dp" android:dashWidth="4dp"></stroke> <size android:height="3dp"></size> </shape>

線是居中顯示的。
android:width:填充顏色的高度
android:dashGap:虛線間距寬度
android:dashWidth:虛線寬度
<size android:height="3dp"></size>:line的高度,size大小必須大于android:width

圓環(ring)

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:useLevel="false" android:thickness="10dp"> <!--useLevel需要設置為false--> <solid android:color="@color/colorAccent"></solid> </shape>

android:thickness:圓環寬度

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:useLevel="false" android:thickness="10dp"> <!--useLevel需要設置為false--> <solid android:color="@color/colorAccent"></solid> <gradient android:startColor="@color/colorAccent" android:endColor="@color/colorPrimary" android:type="sweep"></gradient> </shape>

以上只是簡單的介紹了一下shape的用戶,里面有很多屬性還沒有用到,需要大家自己去實踐一下,寫出來看到效果才能更好的理解。

完整代碼地址:https://github.com/fccaikai/ShapeDemo
以上如果有什么不對的地方希望大家能提出來。

總結

以上是生活随笔為你收集整理的Android Shape使用的全部內容,希望文章能夠幫你解決所遇到的問題。

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