在项目开发过程中经常会遇到自定义图形,然而在日常很多有重复的工作,如一个图形颜色变了或者线框变宽了等等都需要我们重新新建一个shape文件,这样你就会发现资源文件里面多出了很多shape;这里写了一个我在工作中遇到最多的一种Textview的背景色

首先我们要知道要TextView背景样式的有几种

1.圆角(支持定义圆角半径)

2.按下效果(改变边框或者填充色)

3.按下改变文本颜色

基本上也就这些了,先上一张图

基本上就是把shape问代码实现了一遍

先自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CustumBgTextView"><!--填充色--><attr name="solidColor" format="color"></attr><!--边框色--><attr name="strokeColor" format="color"></attr><!--按下填充色--><attr name="solidTouchColor" format="color"></attr><!---按下边框色--><attr name="strokeTouchColor" format="color"></attr><!--文字按下颜色 --><attr name="textTouchColor" format="color"></attr><attr name="strokeWith" format="integer"></attr><attr name="radius" format="dimension"></attr><attr name="topLeftRadius" format="dimension"></attr><attr name="topRightRadius" format="dimension"></attr><attr name="bottomLeftRadius" format="dimension"></attr><attr name="bottomRightRadius" format="dimension"></attr><!--虚线的间隙--><attr name="dashGap" format="dimension"></attr><!--虚线的宽度--><attr name="dashWidth" format="dimension"></attr><attr name="shapeTpe" format="enum"><enum name="rectangle" value="0"></enum><enum name="oval" value="1"></enum><enum name="line" value="2"></enum><enum name="ring" value="3"></enum></attr></declare-styleable></resources>

然后是布局

 <com.shape.tools.CustumShapeBgTextViewandroid:layout_width="300dp"android:layout_height="50dp"android:layout_marginTop="16dp"android:text="rectangle"apps:radius="7dp"apps:strokeWith="1"android:gravity="center"apps:solidColor="#5555ff"apps:strokeColor="#000000"/><com.shape.tools.CustumShapeBgTextViewandroid:layout_width="50dp"android:layout_height="50dp"android:layout_marginTop="16dp"android:text="椭圆Oval"apps:radius="7dp"apps:strokeWith="1"apps:strokeColor="#000000"apps:textTouchColor="#ffffff"android:gravity="center"apps:solidColor="#5555ff"apps:shapeTpe="oval"/><com.shape.tools.CustumShapeBgTextViewandroid:layout_width="300dp"android:layout_height="50dp"android:layout_marginTop="16dp"android:text="rectangle"android:textColor="#000000"apps:bottomLeftRadius="25dp"apps:topRightRadius="25dp"apps:strokeWith="1"android:gravity="center"apps:strokeColor="#ff0000"apps:solidTouchColor="#ff5555"apps:strokeTouchColor="#000000"apps:textTouchColor="#ffffff"apps:shapeTpe="rectangle"apps:dashGap="2dp"apps:dashWidth="3dp"/><com.shape.tools.CustumShapeBgTextViewandroid:layout_width="fill_parent"android:layout_height="50dp"android:layout_marginTop="16dp"android:text="line"android:textColor="#ff0000"apps:bottomLeftRadius="25dp"apps:topRightRadius="25dp"apps:strokeWith="1"android:gravity="center"apps:strokeColor="#ff0000"apps:solidTouchColor="#ff5555"apps:strokeTouchColor="#000000"apps:textTouchColor="#000"apps:shapeTpe="line"android:layerType="software"apps:dashGap="2dp"apps:dashWidth="3dp"/><com.shape.tools.CustumShapeBgTextViewandroid:layout_width="50dp"android:layout_height="50dp"android:layout_marginTop="16dp"android:text="ring"android:textColor="#ff0000"apps:bottomLeftRadius="25dp"apps:topRightRadius="25dp"apps:strokeWith="5"android:gravity="center"apps:strokeColor="#ff0000"apps:solidTouchColor="#ff5555"apps:strokeTouchColor="#000000"apps:textTouchColor="#000"apps:shapeTpe="ring"apps:dashGap="2dp"apps:dashWidth="3dp"/>

注意别忘记了在布局头部的命名空间

xmlns:apps="http://schemas.android.com/apk/res-auto"

好了看看正文吧

/*** Created by guof on 2016/8/3.*/
public class CustumShapeBgTextView extends TextView {/*** 实现自定义圆角背景* 支持* 1.四边圆角* 2.指定边圆角* 3.支持填充色以及边框色* 4.支持按下效果*/public CustumShapeBgTextView(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs);}private GradientDrawable gd;//填充色private int solidColor = 0;//边框色private int strokeColor = 0;//按下填充色private int solidTouchColor = 0;//按下边框色private int strokeTouchColor = 0;//边框宽度private int strokeWith = 0;private int shapeTpe = 0;//按下字体色private int textTouchColor=0;//字体色private int textColor=0;float dashGap=0;float dashWidth=0;public void init(Context context, AttributeSet attrs) {TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustumBgTextView, 0, 0);solidColor = ta.getInteger(R.styleable.CustumBgTextView_solidColor, 0x00000000);strokeColor = ta.getInteger(R.styleable.CustumBgTextView_strokeColor, 0x00000000);solidTouchColor = ta.getInteger(R.styleable.CustumBgTextView_solidTouchColor, 0x00000000);strokeTouchColor = ta.getInteger(R.styleable.CustumBgTextView_strokeTouchColor, 0x00000000);textTouchColor= ta.getInteger(R.styleable.CustumBgTextView_textTouchColor, 0x00000000);textColor=getCurrentTextColor();strokeWith = ta.getInteger(R.styleable.CustumBgTextView_strokeWith, 0);float radius = ta.getDimension(R.styleable.CustumBgTextView_radius, 0);float topLeftRadius = ta.getDimension(R.styleable.CustumBgTextView_topLeftRadius, 0);float topRightRadius = ta.getDimension(R.styleable.CustumBgTextView_topRightRadius, 0);float bottomLeftRadius = ta.getDimension(R.styleable.CustumBgTextView_bottomLeftRadius, 0);float bottomRightRadius = ta.getDimension(R.styleable.CustumBgTextView_bottomRightRadius, 0);dashGap = ta.getDimension(R.styleable.CustumBgTextView_dashGap, 0);dashWidth = ta.getDimension(R.styleable.CustumBgTextView_dashWidth, 0);shapeTpe= ta.getInt(R.styleable.CustumBgTextView_shapeTpe, GradientDrawable.RECTANGLE);Log.d("sss", "打印shapeTpe" + shapeTpe);gd = new GradientDrawable();gd.setShape(shapeTpe);//矩形if (shapeTpe == GradientDrawable.RECTANGLE) {gd.setShape(GradientDrawable.RECTANGLE);if (radius != 0) {gd.setCornerRadius(radius);} else {//分别表示 左上 右上 右下 左下gd.setCornerRadii(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius});}} drowBackgroud(false);ta.recycle();}public void setSelection(boolean selection) {drowBackgroud(selection);}/*** 设置按下颜色值*/private void drowBackgroud(boolean isTouch) {if (isTouch) {if (solidTouchColor != 0)gd.setColor(solidTouchColor);if (strokeWith != 0 && strokeTouchColor != 0) {if(dashGap!=0&&dashGap!=0)gd.setStroke(strokeWith, strokeTouchColor,dashGap,dashGap);elsegd.setStroke(strokeWith, strokeTouchColor);}if(textTouchColor!=0)setTextColor(textTouchColor);} else {if (solidColor != 0)gd.setColor(solidColor);elsegd.setColor(Color.TRANSPARENT);if (strokeWith != 0 && strokeColor != 0) {if(dashGap!=0&&dashGap!=0)gd.setStroke(strokeWith, strokeColor,dashGap,dashGap);elsegd.setStroke(strokeWith, strokeColor);}elsegd.setStroke(0, Color.TRANSPARENT);if(textTouchColor!=0)setTextColor(textColor);}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {setBackground(gd);}postInvalidate();}@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {drowBackgroud(true);} else {drowBackgroud(false);}return true;}
}

基本上都能看懂,没啥复杂的地方

备注:

shape写line虚线的时候发现4.0以上机型很多都没办法显示,后来在xml中

  1. android:layerType="software"

代码中可以添加:

[java] view plaincopy
  1. line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Android 代码实现shape(GradientDrawable详解)相关推荐

  1. Android中shape属性详解

    一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...

  2. Android开发 GradientDrawable详解

    Android开发 GradientDrawable详解 前言 GradientDrawable类似与Xml布局里的shape,常用在一些自己封装的对话框控件的背景或者其他View中,优势是不需要你在 ...

  3. android message 代码,Android Handler移除Message详解及实例代码

    Android Handler移除Message详解 问题: 1.removeMessage(what)函数是否只能移除对应what值的Message? 2.对于Delayed发送的Message,能 ...

  4. android 设置listview滚动条,Android ListView 滚动条的设置详解及实例代码

    Android ListView 滚动条的设置详解 1.滚动条的属性 android:scrollbarAlwaysDrawHorizontalTrack 设置是否始终显示水平滚动条.这里用Scrol ...

  5. android demo示例代码,Android Service demo例子使用详解(示例代码)

    Android Service demo例子使用详解\ 概述 Service 是 Android 的四大组件之一,它主要的作用是后台执行操作,Activity 属于带有 UI 界面跟用户进行交互,而 ...

  6. Android个人手机通讯录开发详解

    一.Android 个人手机通讯录开发 数据存储:SQLite 数据库 开发工具:Android Studio 二.Phone Module 简介 界面展示 文件结构简单分析 三.个人手机通讯录代码实 ...

  7. android json格式解析,android之解析json数据格式详解

    1.JSON解析 (1).解析Object之一: 解析方法: 1 JSONObject demoJson =newJSONObject(jsonString); 2 String url = demo ...

  8. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解 (旧版本 | 仅作参考)

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  9. Android Telephony分析(五) ---- TelephonyRegistry详解

    本文紧接着上一篇文章<Android Telephony分析(四) -- TelephonyManager详解 >的1.4小节.  从TelephonyRegistry的大部分方法中:  ...

  10. Android Telephony分析(三) ---- RILJ详解

    前言 本文主要讲解RILJ工作原理,以便更好地分析代码,分析业务的流程.  这里说的RILJ指的是RIL.java (frameworks\opt\telephony\src\java\com\And ...

最新文章

  1. 金融风控实战——有监督分箱
  2. 第六讲 使用第三方库及简单网页
  3. Dubbo学习笔记(二)
  4. 剧透丨新基建沙龙第二期,你想了解的都在这里!
  5. Kubernetes Secret
  6. 555定时器回差电压计算公式_555定时器及其应用
  7. JAVA对接大汉三通短信http接口
  8. R语言画图教程之盒形图
  9. matlab分析地形,基于Matlab的地形数据处理
  10. 机器人开发--机器人资料汇总
  11. 用批处理的方式压缩文件
  12. igraph入门教程
  13. LittleFs文件系统
  14. Entity and Evidence Guided Relation Extraction for DocRED
  15. excel不显示提示对话框
  16. 小封装SOT23车规加密认证芯片ALPU-CV
  17. bootstrap之入门教程
  18. 亥姆霍兹线圈主要用途有哪些
  19. shader学习网站
  20. 计算机职称评定认可增刊吗,学术期刊增刊对评职称是否有用.pdf

热门文章

  1. CVPR 9999 Best Paper:《一种加辣椒的番茄炒蛋》
  2. WPS中插入“公式”后行距不正常的解决办法
  3. crontab布置定时任务
  4. python字体设置不了_设置字体样式
  5. 线程的优先级(详细)
  6. excel填充序列_分分钟搞定10万个序号自动填充,拒绝加班,你还在手动输入吗?...
  7. 关于Pearson相关系数的显著性p值如何计算以及背后原因的思考
  8. IOS 13陀螺仪监控
  9. linux压缩归档命令gzip、bzip2、xz、tar、zip详解
  10. 以Apollo为例学习/分析自动驾驶运动规划算法