Android 使用shape制作drawable素材

Android开发中,资源文件中会有大量的图片素材文件,这样会额外增加APP的大小,有时面对对APP 大小有限制的,那就要考虑尽可能的对图片进行压缩处理或者减少资源文件中图片的数量,那么减少了资源素材文件,我们如何满足应用对图形的丰富要求呢?我们可以使用shape绘制的,有很多优点。从而满足我们的要求,下面是我整理的一些素材:

首先看最终效果:

1、主布局XML文件

<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="10dp"
        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="10dp"
        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/ic_launcher" />

    <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>

2、shape文件

   1)s1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#23C7B2" />
    <corners android:radius="8dp" />
</shape>

   2)s2.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

   3)s3.xml  

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <shape android:shape="oval">
      <solid android:color="#42A0DB" />
    </shape>
  </item>
  <item android:state_focused="true">
    <shape android:shape="oval">
      <solid android:color="#42A0DB" />
    </shape>
  </item>
  <item>
    <shape android:shape="oval">
      <solid android:color="#42A0DB" />
    </shape>
  </item>
</selector>

   4)s4.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <shape>
      <solid android:color="#7F79B5" />
      <corners android:radius="8dp" />
    </shape>
  </item>
  <item android:state_focused="true">
    <shape>
      <solid android:color="#7F79B5" />
      <corners android:radius="8dp" />
    </shape>
  </item>
  <item>
    <shape>
      <solid android:color="#7F79B8" />
      <corners android:radius="8dp" />
    </shape>
  </item>
</selector>

   5)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>

   6)s6.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

3、String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">shape制作drawable素材</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="s1">我是第一行</string>
    <string name="s2">我是第二行</string>
    <string name="s3">我是第三行</string>
    <string name="s4">我是第四行</string>
    <string name="s5">我是第五行</string>
    <string name="s6">我是第六行</string>

</resources>

4、主界面Activity代码

package com.sunny.shape;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
	}
}

以上就是使用shape制作drawable素材,代码很详细,就不贴源码了,按照以上步骤操作就可以做出最终效果,也可以做出自己想要的喜欢样式,欢迎大家一块学习交流~

时间: 2024-07-30 05:18:57

Android 使用shape制作drawable素材的相关文章

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

前言 最近看到朋友制作的Android APP使用了极少的图片,但是图形却极其丰富,问了之后得知是使用shape绘制的,有很多优点. 下面是我整理的一些素材: 预览 下面是图片预览: 代码 布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height=&

Android系列:res之shape制作

大家好,pls call me francis. nice to me you. 本文将介绍使用在Android中使用shape标签绘制drawable资源图片. 下面的代码是shap标签的基本使用情况: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:

Android:res之shape制作圆角、虚线、渐变

xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐变padding ----------内容离边界距离size ------------大小 solid ----------填充颜色stroke ----------描边 注意的是corners的属性bottomLeftRadius为右下角.bottomRightRadius为左下角 shape制作圆角 &l

Android中shape属性详解

一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml 内容是这样的:(先不需要理解,先看shape怎么用) [html] view plaincopyprint? <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="ht

Android自定义控件背景及其Drawable以实现扁平化

扁平化? 人们都说扁平化是从IOS和WindowsPhone那边吹过来的邪风,但是不可否认:扁平化是我见过的最舒服.最自然的表现方式.从开发角度上来讲,扁平化的设计可以使得我们从许多屏幕适配和尺寸调节的工作中解放出来(虽然只是那么一点点),更加关注功能:而在在使用层面上,只要文化水平不是特别地低(没有恶意),拟物化的那点提示作用也不是那么明显,当然这里不是说拟物化不好,总之:相对于其他表现方式,扁平化碉堡了. 咱们也做一个扁平化 上面说了,扁平化的控件其实在开发中是非常容易的.这里让我们一起动手

[Android UI] shape和selector的结合使用

shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和selector在美化控件中的作用是至关重要的. 1.Shape 简介作用:XML中定义的几何形状位置:res/drawable/文件的名称.xml使用的方法: Java代码中:R.drawable.文件的名称XML中:Android:background="@drawable/文件的名称"

Android中Bitmap和Drawable

一.相关概念1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象2.Canvas画布,绘图的目的区域,用于绘图3.Bitmap位图,用于图的处理4.Matrix矩阵 二.Bitmap 1.从资源中获取Bitmap Java代码   Resources res = getResources(); Bitmap bmp = Bi

android利用jdk制作签名

Apk签名首先要有一个keystore的签名用的文件. keystore是由jdk自带的工具keytool生成的.具体生成方式参考一下: 开始->运行->cmd->cd 到你安装的jdk的目录这里我是 C:\Program Files\Java\jdk1.6.0_10\bin 然后输入:keytool -genkey -alias lvmama.keystore -keyalg RSA -validity 10000 -keystore lvmama.keystore 下面解释下签名的参

CSDN Android客户端的制作 导航帖

弄个导航贴,把相关知识来个汇总. CSDN Android的客户端的效果图: 分别通过以下博客进行详细的讲解: 1.Android 使用Fragment,ViewPagerIndicator 制作csdn app主要框架 主要使用ViewPageIndicator , FragmentPagerAdapter ,ViewPager对主框架的制作: 2.抓取csdn上的各类别的文章 (制作csdn app 二) 使用Java对CSDN中我项目中需要用的数据的抓取,一方面java调试比较方便,另一方