Android Drawable之Shape使用小案例(一)
2016-04-12 15:58
507 查看
日常开发中少不了自己画shape,那么接下来就通过几个小案例来学习一下吧
效果图
圆角矩形
圆角矩形虚线
注:破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线
圆角矩形背景渐变
圆形
注:这里需要主要的是,在使用圆形shape时,必须将控件宽度和高度设置为一样
布局文件
效果图
实战
按照上图先后顺序的效果圆角矩形
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!--圆形shape--> <!-- 填充颜色 --> <solid android:color="#B848FF"></solid> <!--线的宽度,颜色灰色--> <stroke android:width="2dp" android:color="#777777"></stroke> <!-- 矩形的圆角半径 --> <corners android:radius="5dp" /> </shape>
圆角矩形虚线
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--圆角矩形虚线--> <!-- 填充颜色 --> <solid android:color="#FFFFFF"></solid> <!-- 线的宽度,颜色灰色 --> <stroke android:width="1dp" android:color="#D5D5D5" android:dashGap="3dp" android:dashWidth="10dp"></stroke> <!-- 矩形的圆角半径 --> <corners android:radius="10dp" /> </shape>
注:破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线
圆角矩形背景渐变
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--半圆角渐变--> <!--分別對應上面左圆角的半径,上面右圆角的半径,下面左圆角的半径,下面右圆角的半径--> <corners android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" /> <!--设置渐变--> <gradient android:angle="0" android:endColor="#FFFFFF" android:startColor="#D5D5D5" /> <!--即android:angle=“0”时,是从左到右,按照开始颜色到结束颜色来渲染 的, android:angle=“90”是从上到下来渲染的,android:angle=“180”是从右到左来渲染 的, android:angle=“360”和android:angle=“0”是一样的,所以这里应该是这样的, 渲染时按照最原始的渲染色板(把控件内部看作一块可以绕中心旋转的板子)围绕控 件中心来旋转相应的度数,即android:angle里面的值就是所需要旋转的角度, 只是这个旋转角度必须是45的整数倍--> <stroke android:width="1dp" android:color="#D5D5D5" /> </shape>
圆形
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!--圆形shape--> <!-- 填充颜色 --> <solid android:color="#B848FF"></solid> <!--线的宽度,颜色灰色--> <stroke android:width="2dp" android:color="#777777"></stroke> <!-- 矩形的圆角半径 --> <corners android:radius="5dp" /> </shape>
注:这里需要主要的是,在使用圆形shape时,必须将控件宽度和高度设置为一样
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.drawableshapoe.MainActivity"> <TextView android:layout_width="200dp" android:layout_height="25dp" android:background="@drawable/rounded_rectangle_shape" android:gravity="center_vertical|center_horizontal" android:text="Hello World!" /> <TextView android:layout_width="200dp" android:layout_height="25dp" android:layout_marginTop="10dp" android:background="@drawable/rounded_rectangle_dotted_line_shape" android:gravity="center_vertical|center_horizontal" android:text="Hello World!" /> <TextView android:layout_width="200dp" android:layout_height="25dp" android:layout_marginTop="10dp" android:background="@drawable/rounded_half_rectangle_gradient_shape" android:gravity="center_vertical|center_horizontal" android:text="Hello World!" /> <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="10dp" android:background="@drawable/rounded_oval_shape" android:gravity="center_vertical|center_horizontal" android:text="Hello World!" /> </LinearLayout>
源码
github:https://github.com/Waylenw/AndroidBase相关文章推荐
- 通过获取Android应用程序列表谈MVC模式
- Android自定义View之Layout的放缩(2)
- android(NDK+JNI)---JNI数据类型
- android Json解析详解(详细代码)
- 解决android 4.0 EditText无框的烦恼
- Android中将View的内容保存为图像的方法
- Android最棒的下拉刷新!——SwipeRefreshLayout
- 关于android实现fastindexbar(快速索引)详解
- 详解Android应用中使用TabHost组件进行布局的基本方法
- Android自定义View之闪动的TextView
- 自定义dialog,再次点击让dialog消失
- 寻找android中的设计模式(二)
- 简析Android的垃圾回收与内存泄露
- android 百度地图路径规划问题(第一篇)
- android:windowSoftInputMode属性详解
- Android高效加载大图、多图解决方案,有效避免程序OOM
- Android应用中使用TabHost组件继承TabActivity的布局方法
- android的消息机制处理
- android studio常用设置-代码格式化mac
- 关于Xamarin.Android ListView图片加载+Android-Universal-Image-Loader框架