Android下基于XML的Graphics shape的高级UI设计,定义圆角背景等


大家很多人都用过新浪微博Android客户端,感觉它的UI实在做到很精致,昨天晚上熬夜研究了新浪微博的UI相关的代码,于是有了下面这个文章。

以前的UI设计一般有两种方式,首先是UI把图形设计好,分解成UI设计元素后,在代码中直接使用,对于那些简单的图形,如矩形、扇形这样的图形,一般的系统的API会提供这样的接口,但是在Android下,有第三种画图方式,介于二者之间,结合二者的长处,如下的代码:

  1. <item android:id="@android:id/secondaryProgress">   
  2.     <clip>   
  3.         
  4.         <shape>   
  5.             <corners android:radius="5dip" />   
  6.             <gradient   
  7.                     android:startColor="#0055ff88"   
  8.                     android:centerColor="#0055ff00"   
  9.                     android:centerY="0.75"   
  10.                     android:endColor="#00320077"   
  11.                     android:angle="270"   
  12.             />   
  13.         </shape>   
  14.     </clip>   
  15. </item>   

这是一个Progress的style里面的代码,描述的是进度条的为达到的图形,原本以为这是一个图片,后来仔细的跟踪代码,发现居然是 xml,像这种shape corners gradient等等这还是第一次碰到。shape 表示是一个图形,corners表示是有半径为5像素的圆角,然后,gradient表示一个渐变。这样作图简单明了,并且可以做出要求很好的图形,并且节省资源

xml代码

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">   
  2.    <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"   
  3.             android:angle="270"/>   
  4.    <padding android:left="50dp" android:top="20dp"   
  5.             android:right="7dp" android:bottom="7dp" />   
  6.    <corners android:radius="8dp" />   
  7. </shape>  

gradient 产生颜色渐变 android:angle 从哪个角度开始变 貌似只有90的整数倍可以
android:shape="rectangle" 默认的也是长方形

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">   
  2.    <solid android:color="#ff4100ff"/>   
  3.    <stroke android:width="2dp" android:color="#ee31ff5e"   
  4.             android:dashWidth="3dp" android:dashGap="2dp" />   
  5.    <padding android:left="7dp" android:top="7dp"   
  6.             android:right="7dp" android:bottom="7dp" />   
  7.    <corners android:radius="6dp" />   
  8. </shape>   

#ff4100ff蓝色#ff4100ff绿色
<solid android:color="#ff4100ff"/>实心的 填充里面
<stroke 描边 采用那样的方式将外形轮廓线画出来
android:dashWidth="3dp" android:dashGap="2dp" 默认值为0
android:width="2dp" android:color="#FF00ff00"笔的粗细,
android:dashWidth="5dp" android:dashGap="5dp" 实现- - -这样的效果,dashWidth指的是一条小横线的宽度
dashGap 指的是 小横线与小横线的间距。 width="2dp" 不能太宽


shape等特殊xml

1.用 shape 作为背景

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android">   
  2.    <solid android:color="#f0600000"/>   
  3.    <stroke android:width="3dp" color="#ffff8080"/>   
  4.    <corners android:radius="3dp" />   
  5.    <padding android:left="10dp" android:top="10dp"   
  6.     android:right="10dp" android:bottom="10dp" />   
  7. </shape>  
  • 1
  • 2
  • 下一页

相关内容