自定义状态切换按钮

news/2024/7/3 14:18:12

最近在做一个项目,一个界面的按钮UI给画成了这样(默认状态是蓝色的然后触摸后变成灰色的)

img_d8ed28f1de28115fa5c69fca763ab67c.png
UI效果

然后本着给低版本系统APP适配的职业素养(其实是不想画这种按钮),想让UI兄弟给将图标改成整个按钮效果的图片,可是。。。人说人也很忙不给俺做咋办。。。。没办法只好自己写了一个满足这种需求的按钮了。。。

img_f3e899cfe0b503a824f614d56d44e7e4.png
职业素养

整体流程如下

img_0221ab4ee1dd24cca133870a64aaca51.png
实现流程

下面说一下主要功能的实现代码

○动态的修改控件大小,这里没什么难处,主要是动态修改控件大小的代码要放在onLayout方法里,之前试了onMeasure方法里设置然并卵。

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);int width = getWidth();LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();layoutParams.width= (int) (width*0.5);layoutParams.height=(int) (width*0.5);layoutParams.topMargin=(int) (width*0.05);textView.setTextSize((float) (width*0.09));
}

○重写onTouchEvent,实现点击效果,这里是通过判断按下、抬起来动态的设置显示效果

 @Override
public boolean onTouchEvent(MotionEvent event) {switch (event.getAction()){case MotionEvent.ACTION_DOWN:;if(pressImage!=0){imageView.setImageResource(pressImage);}break;case MotionEvent.ACTION_UP:textView.setTextColor(textcolorDefault);if(defaultImage!=0){imageView.setImageResource(defaultImage);}break;}return true;
}

○添加点击事件,这里选择在抬起的时候执行点击事件

//点击事件接口
public interface MyStateButtonClickListener {void onClick(View view);
}@Override
public boolean onTouchEvent(MotionEvent event) {switch (event.getAction()){case MotionEvent.ACTION_DOWN:;textView.setTextColor(textcolorPress);if(pressImage!=0){imageView.setImageResource(pressImage);}break;case MotionEvent.ACTION_UP:textView.setTextColor(textcolorDefault);if(defaultImage!=0){imageView.setImageResource(defaultImage);}//抬起时执行点击事件if(myStateButtonClickListener!=null){myStateButtonClickListener.onClick(view);}break;}return true;
}

○通过代码添加边框,并添加自定义属性(默认图片、点击图片、默认文字颜色、点击文字颜色、边框宽度、边框圆角大小、默认边框颜色、点击边框颜色)

○attrs文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyStateButton"><attr name="defaultImage" format="reference"/><attr name="pressImage" format="reference"/><attr name="text" format="string"/><attr name="defaultTextColor" format="color"/><attr name="pressTextColor" format="color"/><attr name="defaultBorderColor" format="color"/><attr name="pressBorderColor" format="color"/><attr name="cornerRadius" format="float"/><attr name="borderWidth" format="integer"/>
</declare-styleable>
</resources>

○获取属性值

//        初始化GradientDrawable用于显示边框defaultDrawable = new GradientDrawable();pressDrawable = new GradientDrawable();TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyStateButton);if(typedArray!=null){
//            默认图片defaultImage = typedArray.getResourceId(R.styleable.MyStateButton_defaultImage, R.mipmap.one);
//            点击图片pressImage = typedArray.getResourceId(R.styleable.MyStateButton_pressImage, R.mipmap.one_press);
//            设置文字String text = typedArray.getString(R.styleable.MyStateButton_text);
//            默认文字颜色textcolorDefault = typedArray.getColor(R.styleable.MyStateButton_defaultTextColor, Color.parseColor("#567DBF"));
//            点击文字颜色textcolorPress = typedArray.getColor(R.styleable.MyStateButton_pressTextColor, Color.parseColor("#BFBFBF"));imageView.setImageResource(defaultImage);textView.setTextColor(textcolorDefault);textView.setText(text);
//            默认边框颜色borderColorDefault = typedArray.getColor(R.styleable.MyStateButton_defaultBorderColor, Color.parseColor("#567DBF"));
//            点击边框颜色borderColorPress = typedArray.getColor(R.styleable.MyStateButton_pressBorderColor, Color.parseColor("#BFBFBF"));
//            边框宽度borderWidth = typedArray.getInteger(R.styleable.MyStateButton_borderWidth, 2);
//            边框圆角大小cornerRadius = typedArray.getFloat(R.styleable.MyStateButton_cornerRadius, 8);defaultDrawable.setStroke(borderWidth,borderColorDefault);pressDrawable.setStroke(borderWidth,borderColorPress);defaultDrawable.setCornerRadius(cornerRadius);pressDrawable.setCornerRadius(cornerRadius);setBackground(defaultDrawable);}

最终效果

img_3cc2bf4e548cfe32bf41795c224c3292.gif
最终效果

附上源码地址https://github.com/myml666/MyStateButton

个人博客https://myml666.github.io


http://lihuaxi.xjx100.cn/news/236748.html

相关文章

EOS Cleos 命令使用指南

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 命令参考 操作 语法 例子 获取所有命令 $ cleos 例子 获取所有子命令 $ cleos ${command} 例子 链接节点 $ cleos --url node:{node}:no…

[USACO07JAN]平衡的阵容Balanced Lineup BZOJ 1699

题目背景 题目描述&#xff1a; 每天,农夫 John 的N(1 < N < 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 < Q < 18…

Java基础知识回顾之六 ----- IO流

前言 在上一篇文章中&#xff0c;回顾了Java的多线程。而在本篇文章中主要介绍Java IO的相关知识。 IO的介绍 什么是IO&#xff1f; IO的名称又来是Input与Output的缩写&#xff0c;也就是输入流和输出流。输入流用于从源读取数据&#xff0c;输出流用于向目标写数据。 可以从下…

[每日短篇] 17 - 正确使用随机数 Random

2019独角兽企业重金招聘Python工程师标准>>> 随机数在系统开发中几乎是不可避免的一个需求&#xff0c;在大多数面试宝典一定会告诉你所谓的随机数其实是“伪”随机数&#xff0c;除此之外也就没有什么别的了。实际上这条知识本身已经是非常落后了&#xff0c;更不用…

模拟器抓取https方法

说明&#xff1a;为了解决安卓手线上不能抓取https请求&#xff0c;以下整理通过模拟器抓取https请求方法如下&#xff1a;前置条件&#xff1a;安卓模拟器1、夜神抓包工具&#xff1a;fiddler、charles不要安装证书 第一步安装模拟器 可以按照夜神模拟器步骤省略 第二步de.rob…

区块链分享

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 区块链行业现状 政府关注 企业极力研究 学术取得共识 学校和培训机构设立学科 资方积极参与 争先恐后炒币 技术不完善 借区块链热点的传销和骗局横…

[转] vuewebpack多页面配置

前言 最近由于项目需求&#xff0c;选择使用vue框架&#xff0c;webpack打包直接使用的vue-cli&#xff0c;因为需要多页面而vue-cli只有单页面&#xff0c;所以就决定修改vue-cli的配置文件来满足开发需求。 html-webpack-plugin 实现需求需要用到这个插件&#xff0c; 具体信…

VBA注释临时

Sub shishi() 按ABCDE为多选题定义答案; A&#xff0e;沙利度胺 B&#xff0e;异烟肼 C&#xff0e;利福平 d.氯法齐明 E.氨苯砜 46&#xff0e;各型麻风病的首选药物为(D) A&#xff0e;沙利度胺 B&#xff0e;异烟肼 C&#xff0e;利福平 d.氯法齐明 E.氨苯砜 45&#xf…