直接继承View实现,属性全部自定义

<?xml version="1.0" encoding="utf-8"?>
<resources><!-- 按钮的背景色 --><attr name="back_ground_color" format="color" /><!-- 点击的阴影颜色 --><attr name="click_color" format="color" /><!-- 文本 --><attr name="text" format="string" /><!-- 文本的位置 --><attr name="text_position" format="enum"><enum name="left" value="0" /><enum name="center" value="1" /><enum name="right" value="2" /></attr><!-- 文字的大小 --><attr name="text_size" format="dimension" /><!-- 文字的颜色 --><attr name="text_color" format="color" /><declare-styleable name="click_view"><attr name="back_ground_color" /><attr name="click_color" /><attr name="text" /><attr name="text_position" /><attr name="text_size" /><attr name="text_color" /></declare-styleable></resources>

具体实现类

package com.sunrui.mysport.widget;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;import com.sunrui.mysport.R;/*** Created by sunrui on 15/9/9.* 点击扩散效果的View*/
public class ClickView extends View {/*** 背景颜色*/private int background_color;/*** 点击时的颜色*/private int click_color;/*** 显示的文本*/private String text;/*** 文本颜色*/private int text_color;/*** 文本位置* 0 左侧* 1 居中* 2 右侧*/private int text_position;/*** 文本大小*/private float text_size;/*** 控件的宽*/private int width;/*** 控件的高*/private int height;/*** 文本区域大小*/private Rect text_area;/*** 文本画笔*/private Paint text_p;/*** 背景画笔*/private Paint background_p;/*** 点击效果的画笔*/private Paint click_p;/*** 点击阴影的半径*/private float radius;/*** 阴影部分区域*/private RectF shadow_area;public ClickView(Context context) {super(context);}public ClickView(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs);}public ClickView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(Context context, AttributeSet attrs) {TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.click_view, 0, 0);int n = typedArray.getIndexCount();for (int i = 0; i < n; i++) {int attr = typedArray.getIndex(i);switch (attr) {case R.styleable.click_view_back_ground_color:background_color = typedArray.getColor(attr, Color.WHITE);break;case R.styleable.click_view_click_color:click_color = typedArray.getColor(attr, Color.GRAY);break;case R.styleable.click_view_text:text_area = new Rect();text = typedArray.getString(attr);break;case R.styleable.click_view_text_color:text_color = typedArray.getColor(attr, Color.BLACK);break;case R.styleable.click_view_text_position:text_position = typedArray.getInt(attr, 1);break;case R.styleable.click_view_text_size:text_size = typedArray.getDimension(attr, 32.0f);break;default:break;}}background_p = new Paint();text_p = new Paint();click_p = new Paint();shadow_area = new RectF(0, 0, 0, 0);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {width = 0;height = 0;int specMode = MeasureSpec.getMode(widthMeasureSpec);int specSize = MeasureSpec.getSize(widthMeasureSpec);switch (specMode) {case MeasureSpec.EXACTLY:width = getPaddingLeft() + getPaddingRight() + specSize;break;case MeasureSpec.AT_MOST:if (text_area != null) {width = getPaddingLeft() + getPaddingRight() + text_area.width();} else {width = getPaddingLeft() + getPaddingRight();}break;default:break;}specMode = MeasureSpec.getMode(heightMeasureSpec);specSize = MeasureSpec.getSize(heightMeasureSpec);switch (specMode) {case MeasureSpec.EXACTLY:height = getPaddingTop() + getPaddingBottom() + specSize;break;case MeasureSpec.AT_MOST:if (text_area != null) {height = getPaddingTop() + getPaddingBottom() + text_area.height();} else {height = getPaddingTop() + getPaddingBottom();}break;default:break;}setMeasuredDimension(width, height);}@Overrideprotected void onDraw(Canvas canvas) {//绘制背景background_p.setColor(background_color);canvas.drawRect(0, 0, width, height, background_p);//绘制文字text_p.setColor(text_color);text_p.setTextSize(text_size);float text_x = 0f;text_p.getTextBounds(text, 0, text.length(), text_area);float text_y = (height + text_area.height()) / 2;Log.v("sss", text_y + "===" + height + "===" + text_area.height());if (text_position == 0) {text_x = 0f;} else if (text_position == 1) {text_x = (width - text_area.width()) / 2;Log.v("sss", width + "===" + text_area.width() + text_area.height());} else if (text_position == 2) {text_x = width - text_area.width();}canvas.drawText(text, text_x, text_y, text_p);//绘制阴影click_p.setColor(click_color);canvas.drawArc(shadow_area, 0, 360, false, click_p);}@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction();final float click_x = event.getX();switch (action) {case MotionEvent.ACTION_DOWN:radius = height * 1.5f;shadow_area = new RectF(click_x - radius / 2, (height - radius) / 2, click_x + radius / 2, (height + radius) / 2);postInvalidate();break;case MotionEvent.ACTION_UP:new Thread() {@Overridepublic void run() {while (true) {if (radius > width * 2.0f) {break;}radius += width / 18;shadow_area = new RectF(click_x - radius / 2, (height - radius) / 2, click_x + radius / 2, (height + radius) / 2);try {Thread.sleep(300/18);} catch (InterruptedException e) {e.printStackTrace();}postInvalidate();}shadow_area = new RectF(0, 0, 0, 0);}}.start();break;default:break;}return true;}}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"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.sunrui.mysport.ui.AddSportItemActivity"><com.sunrui.mysport.widget.ClickViewandroid:layout_width="match_parent"android:layout_height="48dp"custom:back_ground_color="#ffffff"custom:click_color="#33000000"custom:text="点击测试"custom:text_position="center"custom:text_size="32px"custom:text_color="#00ff00"/></RelativeLayout>

如果不是在Android studio中,将第二行的res-auto改成包名

点击按钮的动画,点击出现阴影,松开后逐渐扩散到整个按钮相关推荐

  1. 汉堡式折叠html,纯CSS3菜单汉堡包按钮变形动画特效

    这是一款非常有趣的纯CSS3菜单汉堡包按钮变形动画特效.该特效共有9种不同的按钮变形动画效果,这些效果都是使用CSS3帧动画完成的,效果非常的酷. 制作方法 HTML结构 该按钮变形动画使用嵌套 的H ...

  2. 用html和css怎么做出点击关注红心,Twitter“点赞”红心按钮CSS3动画特效

    这是一款效果非常炫酷的仿Twitter"点赞"红心按钮CSS3动画特效.该"点赞"特效使用一颗心形按钮,在用户点击心形按钮的时候,心形按钮由灰色变为红色,同时会 ...

  3. html中如何实现放大动画,CSS3实现点击放大的动画实例

    这次给大家带来CSS3实现点击放大的动画实例,CSS3实现点击放大动画实例的注意事项有哪些,下面就是实战案例,一起来看一下. 前言 最近在工作中遇到一个需求,实现的效果是当点击商品图片右上的收藏按钮触 ...

  4. invoke 按钮点击_使用aspectj对app中按钮的快速点击进行处理

    最近项目进入紧锣密鼓测试阶段,昨天测试提了一个issue,app中按钮都没有做快速点击校验. 这就涉及到aop面向切面编程了!后端开发Spring对aop应该很熟悉,android开发中可能用到aop ...

  5. 点击展开按钮设计_使您的按钮设计可点击

    点击展开按钮设计 A button is an important UI element that will heavily affect your interaction design. Butto ...

  6. IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题

    在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的:    这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需 ...

  7. 如何解决秒杀的性能问题和超卖的讨论 及防止按钮多次点击

    抢购活动一般会经过[预约][抢订单][支付]这3个大环节,而其中[抢订单]这个环节是最考验业务提供方的抗压能力的. 抢订单环节一般会带来2个问题: 1.高并发 比较火热的秒杀在线人数都是10w起的,如 ...

  8. vue 按钮多次点击重复提交数据

    这个其实是一个很细节的问题. 如果我们操作一个按钮,然后在按钮点击的时候绑定事件. 事件分为两种情况: 第一种: 不操作数据型 第二种: 操作数据型 <template><butto ...

  9. vue限制点击次数_解决vue 按钮多次点击重复提交数据问题

    这个其实是一个很细节的问题. 如果我们操作一个按钮,然后在按钮点击的时候绑定事件. 事件分为两种情况: •第一种: 不操作数据型 •第二种: 操作数据型 点击 这里我们通过控制isDisable 来设 ...

最新文章

  1. zabbix 自动注册发现
  2. linux date
  3. 升级 90天 vs2008 在win2008下。
  4. 第八天2017/04/17(2、❤String类的源代码)
  5. SAP CRM WebClient UI html 格式的 Text 显示逻辑
  6. Azure PowerShell (1) PowerShell整理
  7. Linux MySQL 5.1.62 source install
  8. poj-2528线段树练习
  9. Java里a和b哪个大_Java中 a+=b和a=a+b有什么区别?
  10. 线性结构 —— ST 表与 RMQ
  11. 百度地图检索以及路径规划
  12. numpy基础笔记01
  13. mysql怎么换行_MySql的主从复制、主主复制
  14. 我大意了,刚一放出来就上了牛客网头条了
  15. dell 工作站装linux_dell服务器linux系统安装
  16. 面试一家公司之前需要做的准备
  17. 内存取证之Volatility ——合天网安实验室学习笔记
  18. 顾泽苍:新一代人工智能——产业推动的核心理论
  19. Android q索尼手机相机算法,手机厂商套路太多!竟在手机摄像头上玩“掩眼法”?...
  20. c语言测量身体健康的程序,c++ c语言 学生健康管理系统.doc

热门文章

  1. github 删除仓库内容 上传代码到github仓库
  2. 在Ubuntu上安装MariaDB以及初步设置
  3. CentOS安装autojump
  4. 一站到底关于计算机科学,机电工程系和计算机科学系联合举办“一站到底”知识竞赛...
  5. 下一代数据存储OneStorage闪亮登场,华为打造全场景智能的基石
  6. 管道式广谱感应水处理器详细介绍
  7. 前端-CSS-从入门到精通
  8. (转载)小米手机给我的启发和思考
  9. lz4压缩算法java实现_LZ4压缩算法分析
  10. 网赚人必经的60件事,你中了几条?