搜索
您的当前位置:首页正文

Android 背景样式shape - oval椭圆、圆

来源:二三娱乐

概述

上一篇介绍了Rectangle矩形,下面介绍一下oval椭圆的使用。
oval用来绘制椭圆,而在实际应用中,更多的是用来绘制正圆,例如:消息个数提示背景,圆形按钮等。

效果

image

代码实现

<TextView  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:gravity="center"  
    android:text="1"  
    android:textSize="20sp"  
    android:textColor="#ffffff"  
    android:background="@drawable/text_shape6"/>  
  
<TextView  
    android:layout_width="51dp"  
    android:layout_height="wrap_content"  
    android:layout_marginTop="20dp"  
    android:gravity="center"  
    android:text="2"  
    android:textSize="20sp"  
    android:textColor="#ffffff"  
    android:background="@drawable/text_shape7"/>  
  
<TextView  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_marginTop="20dp"  
    android:gravity="center"  
    android:text="3"  
    android:textSize="20sp"  
    android:textColor="#ff0000"  
    android:background="@drawable/text_shape8"/>  
  
<TextView  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_marginTop="20dp"  
    android:gravity="center"  
    android:text="4"  
    android:textSize="20sp"  
    android:textColor="#ffffff"  
    android:background="@drawable/text_shape9"/>  
  
<TextView  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_marginTop="20dp"  
    android:gravity="center"  
    android:text="5"  
    android:textSize="20sp"  
    android:textColor="#ff0000"  
    android:background="@drawable/text_shape10"/> 

shape背景

  • text_shape6
<?xml version="1.0" encoding="utf-8"?>  
<shape   
    android:shape="oval">  
    <solid  
        android:color="#990000ff"/>  
    <padding  
        android:bottom="5dp"  
        android:left="5dp"  
        android:right="5dp"  
        android:top="5dp" />  
    <size  
        android:width="50dp"  
        android:height="30dp"/>  
</shape> 
  • text_shape7
<?xml version="1.0" encoding="utf-8"?>  
<shape   
    android:shape="oval">  
    <solid  
        android:color="#990000ff"/>  
    <size  
        android:width="50dp"  
        android:height="50dp"/>  
</shape>  
  • text_shape8
<?xml version="1.0" encoding="utf-8"?>  
<shape   
    android:shape="oval">  
    <solid  
        android:color="#ffffff"/>  
    <size  
        android:width="50dp"  
        android:height="50dp"/>  
    <stroke  
        android:width="1dp"  
        android:color="#00ff00"/>  
</shape>  
  • text_shape9
<?xml version="1.0" encoding="utf-8"?>  
<shape   
    android:shape="oval">  
    <solid  
        android:color="#0000ff"/>  
    <size  
        android:width="50dp"  
        android:height="50dp"/>  
    <gradient  
        android:startColor="#0000ff"  
        android:endColor="#00ff00"  
        android:gradientRadius="40dp"  
        android:type="radial"/>  
</shape> 
  • text_shape10
<?xml version="1.0" encoding="utf-8"?>  
<shape   
    android:shape="oval">  
    <solid  
        android:color="#ffffff"/>  
    <size  
        android:width="50dp"  
        android:height="50dp"/>  
    <stroke  
        android:width="1dp"  
        android:color="#00ff00"  
        android:dashWidth="4dp"  
        android:dashGap="4dp"/>  
</shape>  

总结

  • 绘制正圆的时候需要设置属性size,其宽高大小相等。当然也可以控制控件的宽高来实现。
  • 数字4使用了radial渐变的效果,使用radial渐变时,必须指定渐变的半径,即android:gradientRadius属性
Top