您的位置:首页 > 其它

如何使用shape来画半圆和画虚线

2016-05-20 16:23 274 查看
一、很多人对于使用shape画圆可能会比较熟悉,这里也奉上代码

shape可以绘制矩形环形以及椭圆,所以只需要用椭圆即可,在使用的时候将控件比如imageview或textview的高度设置成一样就是正圆,solid表示填充色,stroke则表示边框线,所以两者结合可以实现带边缘的圆,当然也可以直接加上size控制高宽。

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

android:shape:"oval"
android:useLevel="false">
<solid android:color="@color/red"/>
<stroke android:width="1dp"
android:color="@color/white"/>
<size android:width="20dp"
android:height="20dp"/>
</shape>

二、现在就说下如何画半圆,代码如下:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

android:shape = "rectangle">
<soild android:color = "@color/white"/>
<corners
android:bottomLeftRadius = "0dp"
android:bottomRightRadius = "10dp"
android:topLeftRadius = "0dp"
android:topRightRadius = "10dp"/>
使用的地方:
<imageview
android:layout_width = "10dp" //这边的宽高要和shape当中的corners配合使用
android:layout_height = "20dp"
android:background = "@drawable/shape"/> //上面的shape
画半圆的关键所在是shape当中的corners和使用的控件(imageview)的width和height配合使用,而且比较坑的是在xml的预览界面当中看不出效果,需要跑真机才能看出效果。

三、最后讲下如何画虚线
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line"> //这句话一定不要漏了
<stroke
android:width = "1dp"
android:color = "#cfcfcf"
android:dashGap = "5dp"
android:dashWidth = "4dp"/>
<size android:height = "1dp"/> //虚线的高度
</shape>
需要注意的是在使用的时候需要关掉硬件加速:android:hardwareAccelerated = "false"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: