Android利用shape实现各种简单的形状

一,概述

我们在android开发中经常要用到图片,而一些简单的图片我们完全可以用shape形状drawable资源代替,使用shape有一个好处就是可以减小我们apk的大小,因为同样的效果,shape比图片更节省空间,好了,我们废话不多说,下面进入正题。

二,shape初识

shape是android drawable资源中的一个重要的角色,drawable资源覆盖面广,它不仅代表图片,它可以是一个颜色,一个形状,因为shape其简单实用,下面我们来看一下shape形状的分类:

ectangle:

rectangle代表者矩形,它是shape默认的形状类型,即如果我们不在shape的android:shape属性指定其类型时,默认是矩形,用它我们可以画一个矩形,圆角矩形,具体在下面会说道

oval:

ovel,椭圆,用它可以画椭圆,圆

line:

水平线,在使用该形状的时候,我们得给它指定stroke元素指定其宽度,不然在使用该形状的时候会报空指针异常

ring:

环形

下面我们来用上面说道的各种形状画图形,打造各种简单的形状

三,shape的使用

下面看看用shape画的一些简单的图形,之后我会按照图形说一下shape的各种属性以及一些要注意的问题:

1.实心长方形

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 设置固定填充色 --> <solid android:color="#f00" /> <size android:width="60dp" android:height="30dp"/> </shape>

2.炫彩实心长方形

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="60dp" android:height="30dp"/> <!-- 设置渐变填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"></gradient> </shape>

3.长方形外框

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 设置描边 --> <stroke android:width="2dp" android:color="#f00" ></stroke> </shape>

4.虚线长方形外框

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 设置描边 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> </shape>

5.椭圆虚线外框

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 设置描边 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> <corners android:radius="15dp"/> </shape>

6.实心长方体切圆角

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 设置固定填充色 --> <solid android:color="#f00" /> <size android:width="60dp" android:height="30dp"/> <corners android:radius="10dp"/> </shape>

7.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="60dp" android:height="30dp"/> <!-- 设置渐变填充色 --> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:gradientRadius="60" android:type="radial"></gradient> </shape>

8.

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 设置描边 --> <stroke android:width="2dp" android:color="#f00" ></stroke> <corners android:radius="15dp"/> </shape>

9.

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 设置渐变填充色 --> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="sweep"></gradient> </shape>

10.

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <size android:width="60dp" android:height="30dp"/> <!-- 设置描边 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> <corners android:radius="15dp" android:topRightRadius="0dp" android:bottomRightRadius="0dp"/> </shape>

11.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!-- 设置固定填充色 --> <solid android:color="#f00" /> <size android:width="60dp" android:height="30dp"/> </shape>

12.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 设置渐变色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"></gradient> </shape>

13.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 设置描边色 --> <stroke android:width="2dp" android:color="#f00" ></stroke> </shape>

14.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 设置渐变填充色 --> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:gradientRadius="60" android:type="sweep"></gradient> </shape>

15.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="60dp" android:height="30dp"/> <!-- 设置描边色 --> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"></stroke> </shape>

16.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <!-- 设置固定填充色 --> <solid android:color="#f00" /> <size android:height="44dp" /> </shape>

17.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <!-- 设置渐变填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"/> <size android:height="44dp" /> </shape>

18.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <!-- 设置渐变填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"/> <size android:height="44dp" /> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"/> </shape>

19.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:thickness="2dp" android:useLevel="false" > <size android:height="44dp" /> <stroke android:width="2dp" android:color="#f00" android:dashWidth="5dp" android:dashGap="5dp"/> </shape>

20.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:innerRadius="20dp" android:shape="ring" android:innerRadiusRatio="8" android:useLevel="false" > <!-- 设置渐变填充色 --> <gradient android:startColor="#00f" android:centerColor="#0f0" android:endColor="#f00"/> <size android:height="44dp" /> </shape>

21.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="#f00"/> <size android:height="2dp"></size> </shape>

22.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="#f00"/> <size android:height="10dp"></size> </shape>

23.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" > <stroke android:dashGap="8px" android:dashWidth="8px" android:width="1dp" android:color="#f00" /> <size android:height="30dip" /> </shape>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

时间: 2024-09-20 05:40:20

Android利用shape实现各种简单的形状的相关文章

Android利用Gson解析嵌套多层的Json的简单方法_Android

首先先讲一个比较简单点的例子(最简单的我就不讲啦,网上很多),帮助新手理解Gson的使用方法: 比如我们要解析一个下面这种的Json: String json = {"a":"100", "b":[{"b1":"b_value1","b2":"b_value2"}, {"b1":"b_value1","b2"

Android利用Gson解析嵌套多层的Json的简单方法

首先先讲一个比较简单点的例子(最简单的我就不讲啦,网上很多),帮助新手理解Gson的使用方法: 比如我们要解析一个下面这种的Json: String json = {"a":"100", "b":[{"b1":"b_value1","b2":"b_value2"}, {"b1":"b_value1","b2"

Android中WebView的一些简单用法_Android

Android中WebView的一些简单用法 一直想写一个关于 WebView 控件的 一些简单运用,都没什么时间,这次也是挤出时间写的,里面的一些基础知识就等有时间再更新讲解一下,今天就先把项目出来做一些简单介绍,过多的内容可以看我的源码,都传到github上了. 下面是项目的效果图: 应用用到的是 MVP 设计模式,对这种模式还不太了解的可以先自行google一下,不然项目估计会看的晕,虽然我的代码都很简洁的. 对于MVP 可以带着一个思路看源码,那就是 activity(或其他组件)通过

java实现利用String类的简单方法读取xml文件中某个标签中的内容_java

1.利用String类提供的indexOf()和substring()快速的获得某个文件中的特定内容 public static void main(String[] args) { // 测试某个词出现的位置 String reqMessage = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + "<in>" + "<head&g

Android中WebView的一些简单用法

Android中WebView的一些简单用法 一直想写一个关于 WebView 控件的 一些简单运用,都没什么时间,这次也是挤出时间写的,里面的一些基础知识就等有时间再更新讲解一下,今天就先把项目出来做一些简单介绍,过多的内容可以看我的源码,都传到github上了. 下面是项目的效果图: 应用用到的是 MVP 设计模式,对这种模式还不太了解的可以先自行google一下,不然项目估计会看的晕,虽然我的代码都很简洁的. 对于MVP 可以带着一个思路看源码,那就是 activity(或其他组件)通过

Android 录音与播放功能的简单实例

Android 录音与播放功能的简单实例 最近在研究Android中一些常用的功能,像地图.拍照.录音和播放的实现等等,还有一些侧滑.动画等是如何实现的. 今天就把录音和播放的实现分享一下,录音和播放比较简单,利用android内部的类即可实现. 1.先看下运行后的界面: 以下三张图分别是进入.录音.播放时的. 2.Layout布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x

Android Kotlin的使用及简单实例

Android Kotlin的使用及简单实例 写在前面的话,作为一个不熬夜的人,一觉醒来发现Kotlin成为了Android的官方语言,可谓是大喜过望.为了趁热打铁,我决定提前三天放出原定本周日Release的文章.希望能及时让大家了解一下Kotlin. 相信很多开发人员,尤其是Android开发者都会或多或少听说过Kotlin,当然如果没有听过或者不熟悉也没有关系.因为本篇文章以及博客后期的内容会涉及到很多关于Kotlin的知识分享. 在写这篇文章前的一个多月,Flipboard中国的Andr

Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址

Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端通信技术,无奈一直心不在此,但是真当自己要使用的时候还是比较迷茫,所以最近也有意向来学习这一块,同时,把自己的学习经历分享出来 一.摘要 Android智能硬件前几年野一直不温不火的,到了现在却热火朝天了,各种智能手环,智能手表,智能家居等,而使用BLE这个方向也越来越多,而这方面的资料却是真的很少

udp-关于Android利用局域网进行UDP通信的问题

问题描述 关于Android利用局域网进行UDP通信的问题 我写了一个小程序准备利用UDP和电脑之间进行通信,然后基于这个做一点东西,可是问题是我用AVD测试很好用的,但是用真机测试就是不行.我的思路是让电脑和Android设备连接一台路由器,路由器为两台设备分配固定的ip地址进行通信.拜托大家帮我看看啊,我这才是第一步就郁闷的要死下面是代码.1:MainActivity package com.company.zebork.testudpll; import android.app.Activ