Android必知必会--使用shape制作drawable素材

前言

最近看到朋友制作的Android APP使用了极少的图片,但是图形却极其丰富,问了之后得知是使用shape绘制的,有很多优点。

下面是我整理的一些素材:

预览

下面是图片预览:

代码

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s1"
        android:padding="5dp"
        android:text="@string/s1"
        android:textColor="#fff"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s2"
        android:padding="5dp"
        android:text="@string/s2"
        android:textColor="#ff9800"
        android:textSize="16sp" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s3"
        android:padding="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/lsearch" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s4"
        android:padding="5dp"
        android:text="@string/s4"
        android:textColor="#fff"
        android:textSize="16sp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s5"
        android:padding="5dp"
        android:text="@string/s5"
        android:textColor="#00bcd4"
        android:textSize="16sp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:background="@drawable/s6"
        android:padding="5dp"
        android:text="@string/s6"
        android:textColor="#fff"
        android:textSize="16sp" />

</LinearLayout>

shape文件

绿色标签s1.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#009688" />
    <corners android:radius="8dp" />
</shape>

橙色标签s2.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#00000000" />
    <corners android:radius="8dp" />
    <stroke android:width="1dp" android:color="#ff9800" />
</shape>

蓝色圆形按钮s3.xml

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

    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="#aa00bcd4" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape android:shape="oval">
            <solid android:color="#aa00bcd4" />
        </shape>
    </item>

    <item>
        <shape android:shape="oval">
            <solid android:color="#00bcd4" />
        </shape>
    </item>

</selector>

蓝色按钮s4.xml

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

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#aa00bcd4" />
            <corners android:radius="8dp" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <solid android:color="#aa00bcd4" />
            <corners android:radius="8dp" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="#00bcd4" />
            <corners android:radius="8dp" />
        </shape>
    </item>

</selector>

蓝色边框按钮s5.xml

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

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#e3e3e3" />
            <corners android:radius="8dp" />
            <stroke android:width="1dp" android:color="#00bcd4" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <solid android:color="#e3e3e3" />
            <corners android:radius="8dp" />
            <stroke android:width="1dp" android:color="#00bcd4" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="#00000000" />
            <corners android:radius="8dp" />
            <stroke android:width="1dp" android:color="#00bcd4" />
        </shape>
    </item>

</selector>

蓝色带阴影按钮s6.xml

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

    <item android:state_pressed="true">
        <layer-list>
            <item android:top="3dp">
                <shape>
                    <solid android:color="#00bcd4" />
                    <corners android:radius="8dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_focused="true">
        <layer-list>
            <item android:top="3dp">
                <shape>
                    <solid android:color="#00bcd4" />
                    <corners android:radius="8dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    <item>
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#dddddd" />
                    <corners android:radius="8dp"/>
                </shape>
            </item>
            <item android:bottom="3dp">
                <shape>
                    <solid android:color="#00bcd4" />
                    <corners android:radius="8dp"/>
                </shape>
            </item>
        </layer-list>
    </item>

</selector>

参考

谷歌官方文档

qiita.com

时间: 2024-08-11 09:57:25

Android必知必会--使用shape制作drawable素材的相关文章

Android 使用shape制作drawable素材

Android开发中,资源文件中会有大量的图片素材文件,这样会额外增加APP的大小,有时面对对APP 大小有限制的,那就要考虑尽可能的对图片进行压缩处理或者减少资源文件中图片的数量,那么减少了资源素材文件,我们如何满足应用对图形的丰富要求呢?我们可以使用shape绘制的,有很多优点.从而满足我们的要求,下面是我整理的一些素材: 首先看最终效果: 1.主布局XML文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/re

Android必知必会-使用okhttp的PUT方式上传文件

背景 公司的文件上传接口使用PUT协议,之前一直用的都是老项目中的上传类,现在项目中使用了okhttp网络库,就查了下资料,在这里分享一下. 代码实现 /** * @param mediaType MediaType * @param uploadUrl put请求地址 * @param localPath 本地文件路径 * @return 响应的结果 和 HTTP status code * @throws IOException */ public String put(MediaType

Android必知必会-获取视频文件的截图、缩略图

背景 公司最近要求给我负责的APP加上视频录制和发布的功能,我简单的完成了基本的录制和视频压缩功能,后来发现发布接口需要上传视频的截图,网上搜索了一下资料,在这里整理一下. 代码实现 /** * 获取视频文件截图 * * @param path 视频文件的路径 * @return Bitmap 返回获取的Bitmap */ public static Bitmap getVideoThumb(String path) { MediaMetadataRetriever media = new Me

迈向高阶:优秀Android程序员必知必会的网络基础

1.前言 网络通信一直是Android项目里比较重要的一个模块,Android开源项目上出现过很多优秀的网络框架,从一开始只是一些对HttpClient和HttpUrlConnection简易封装使用的工具类,到后来Google开源的比较完善丰富的Volley,再到如今比较流行的Okhttp.Retrofit. 要想理解他们之间存在的异同(或者具体点说,要想更深入地掌握Android开发中的网络通信技术),必须对网络基础知识.Android网络框架的基本原理等做到心中有数.信手拈来,关键时刻才能

移动前端开发人员必知必会:移动设备概述

因为工作岗位的变换带来工作内容的变动,对于移动网站的前端开发已经疏远了好几个月,在这好几个月中有很多新的东西出现,自己所掌握的一些东西也已经陈旧,所以选择了这本书<HTML5触摸界面设计与开发>来系统地学习和整理一下关于移动网站前端开发的知识体系. 之所以选择这本书,一是因为这本书比较新,2014年04月发的第一版.其二是因为作者Stephen Woods,这是Flickr团队的资深前端,Yahoo主页的Javascript技术平台正是出自此人之手. 接下来的时间里会陆续上传关于这本书学习的一

C++必知必会(3)

条款26操作符函数查找 class X { public: Xoperator %( const X& ) const;             //二元取余操作 XmemFunc1( const X&); voidmemFunc2(); }; 可以采用中缀或函数调用语法来调用这个重载的操作符:如下 X a,b,c; a = b%c;        //采用中缀语法调用成员操作符% a = b.operator %(c);   //成员函数调用语法 a = b.memFunc1(c);  

移动前端开发者必知必会:移动设备概述

由于工作岗位的变换带来工作内容的变动,对于移动站点的前端开发已经疏远了好几个月,在这好几个月中有非常多新的东西出现,自己所掌握的一些东西也已经陈旧,所以选择了这本书<HTML5触摸界面设计与开发>来系统地学习和整理一下关于移动站点前端开发的知识体系. 之所以选择这本书,一是由于这本书比較新,2014年04月发的第一版.其二是由于作者Stephen Woods,这是Flickr团队的资深前端,Yahoo主页的Javascript技术平台正是出自此人之手. 接下来的时间里会陆续上传关于这本书学习的

MySQL必知必会 学习笔记(一)

第一章  了解SQL 模式:   关于数据库和表的布局以及特性的信息.[描述表可以存储什么样的数据,数据如何分解,各部分信息如何命名等等,可以用来描述数据库中特定的表以及整个数据库(和其中表的关系)]. 第二章 MySQL简介 MySQL是一种DBMS,即它是一种数据库软件.基于客户机----服务器的数据库. MySQL工具: 1.mysql 命令行实用程序 2.MySQL Administrator 3.MySQL query Browser 第四章 检索数据 LIMIT 5 表示MySQL返

mysql 必知必会总结

以前 mysql 用的不是很多, 2 天看了一遍 mysql 必知必会又复习了一下基础.  200 页的书,很快就能看完, 大部分知识比较基础, 但还是了解了一些以前不知道的知识点.自己做一个备份,随时查看. 命令:sql 不区分大小写,语句大写,列.表名小写是一种习惯连接命令:mysql -u user_name –h example.mysql.alibabalabs.com –P3306 –pxxxxquithelp show; // 查看所有 show 命令show databases;