当前位置:网站首页>【分享】代码编程习惯:在多参数的方法的情况下需要对方法整理-即最多参数的方法为主要实现
【分享】代码编程习惯:在多参数的方法的情况下需要对方法整理-即最多参数的方法为主要实现
2022-08-05 11:46:00 【ChrisEighteen18】
背景
这是一个编写代码的习惯。在多参数同样功能的方法会推荐使用在最多参数的方法内实现,详见代码解析;
最开始的代码
对textView 带有不同颜色和图片操作。
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
/*** * 该工具类对TextView 的文本颜色进行设置: * 目前是前半部分以黄色为主,后半部分的主体Text以#FFFFFF为主 * written by Chris on 2022-07-14 */
public class TextColorHandler {
static String colorString = "#FFB763"; // 换色
static String defaultColorString = "#FFFFFF";
public static SpannableStringBuilder handlerMessage(String front, String content,String splitInfo,String color) {
colorString = color;
return handlerMessage(front,content,splitInfo);
};
public static SpannableStringBuilder handlerMessage(String front, String content,String splitInfo) {
// 文本颜色区分
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
ForegroundColorSpan frontColor = new ForegroundColorSpan(Color.parseColor("#FFB763"));
ForegroundColorSpan backColor = new ForegroundColorSpan(Color.parseColor("#FFFFFF"));
ForegroundColorSpan frontColor = new ForegroundColorSpan(Color.parseColor(colorString));
ForegroundColorSpan backColor = new ForegroundColorSpan(Color.parseColor(defaultColorString));
int length = stringBuilder.length();
stringBuilder.append(front + splitInfo);
stringBuilder.setSpan(frontColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
length = stringBuilder.length();
stringBuilder.append(content);
stringBuilder.setSpan(backColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
colorString = "#FFB763";// 记得还原回去
return stringBuilder;
}
public static SpannableStringBuilder handleMessageWithPicture(String front, String content,Drawable drawable,String color) {
colorString = color;
return handleMessageWithPicture(front,content,drawable);
};
// 在文本中添加图片操作
public static SpannableStringBuilder handleMessageWithPicture(String front, String content, Drawable drawable) {
// 文本颜色区分
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
ForegroundColorSpan frontColor = new ForegroundColorSpan(Color.parseColor("#FFB763"));
ForegroundColorSpan backColor = new ForegroundColorSpan(Color.parseColor("#FFFFFF"));
ForegroundColorSpan frontColor = new ForegroundColorSpan(Color.parseColor(colorString));
ForegroundColorSpan backColor = new ForegroundColorSpan(Color.parseColor(defaultColorString));
int length = stringBuilder.length();
stringBuilder.append(front+" ");
stringBuilder.setSpan(frontColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
length = stringBuilder.length();
//添加图片
drawable.setBounds(drawable.getBounds().left, drawable.getBounds().top, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
stringBuilder.append(" ");//放置图片
stringBuilder.setSpan(span, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
stringBuilder.append(" "+content);
stringBuilder.setSpan(backColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
colorString = "#FFB763";// 记得还原回去
return stringBuilder;
}
}
优化后的代码
package im.zego.libimchat.view.utils;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
/*** * 该工具类对TextView 的文本颜色进行设置: * 目前是前半部分以黄色为主,后半部分的主体Text以#FFFFFF为主 * written by Chris on 2022-07-14 */
public class TextColorHandler {
static int colorString = Color.parseColor("#FFB763"); // 换色 为黄色
static int defaultColorString = Color.parseColor("#FFFFFF");
public static SpannableStringBuilder handlerMessage(String front, String content,String splitInfo,int color) {
// 文本颜色区分
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
ForegroundColorSpan frontColor = new ForegroundColorSpan(color);
ForegroundColorSpan backColor = new ForegroundColorSpan(defaultColorString);
int length = stringBuilder.length();
stringBuilder.append(front + splitInfo);
stringBuilder.setSpan(frontColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
length = stringBuilder.length();
stringBuilder.append(content);
stringBuilder.setSpan(backColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
return stringBuilder;
}
public static SpannableStringBuilder handlerMessage(String front, String content,String splitInfo) {
return handlerMessage(front, content, splitInfo, colorString);
}
public static SpannableStringBuilder handleMessageWithPicture(String front, String content,Drawable drawable,int color) {
// 文本颜色区分
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
ForegroundColorSpan frontColor = new ForegroundColorSpan(color);
ForegroundColorSpan backColor = new ForegroundColorSpan(defaultColorString);
int length = stringBuilder.length();
stringBuilder.append(front+" ");
stringBuilder.setSpan(frontColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
length = stringBuilder.length();
//添加图片
drawable.setBounds(drawable.getBounds().left, drawable.getBounds().top, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
stringBuilder.append(" ");//放置图片
stringBuilder.setSpan(span, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
stringBuilder.append(" "+content);
stringBuilder.setSpan(backColor, length, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
return stringBuilder;
};
// 在文本中添加图片操作
public static SpannableStringBuilder handleMessageWithPicture(String front, String content, Drawable drawable) {
return handleMessageWithPicture(front, content, drawable,colorString);
}
}
关键:
public static SpannableStringBuilder handleMessageWithPicture(String front, String content, Drawable drawable) {
return handleMessageWithPicture(front, content, drawable,colorString);
}
整合到一块,代码就比较清晰。
边栏推荐
- 澳洲站:电吹风AS/NZS 60335.2.23: 2017 安全标准测试
- [7.29-8.5] Review of wonderful technical blog posts in the writing community
- power failure...Trouble trouble trouble!!!
- 字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?
- 365 days challenge LeetCode1000 questions - Day 050 add a row to the binary tree binary tree
- Security Issues and Prevention in Web3
- Naive bayes
- JS 从零手写实现一个call、apply方法
- Five reasons why developers choose Klocwork, a static analysis tool for code quality, for software security
- 2022 CCF International AIOps Challenge Finals and AIOps Seminar Registration Open
猜你喜欢

Cesium.js 地形挖洞

莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》

Go Quick Start Guide: Basic Types

Hands-on Deep Learning_GoogLeNet / Inceptionv1v2v3v4

Mathcad 15.0软件安装包下载及安装教程

机器学习——集成学习

2022年6月互联网医疗领域月度观察

TiDB 6.0 Placement Rules In SQL Usage Practice

五大理由告诉你为什么开发人员选择代码质量静态分析工具Klocwork来实现软件安全

623. Add a row to a binary tree: Simple binary tree traversal problems
随机推荐
常见的 web 安全问题总结
五大理由告诉你为什么开发人员选择代码质量静态分析工具Klocwork来实现软件安全
官方发布·2022南京智博会定于10月份在新庄国展召开
2022 CCF International AIOps Challenge Finals and AIOps Seminar Registration Open
知乎提问:中国是否还能实现伟大民族复兴
花的含义
Go Quick Start Guide: Basic Types
hdu1455 Sticks (search+pruning+pruning+.....+pruning)
WPF开发随笔收录-WriteableBitmap绘制高性能曲线图
Cesium.js点线面绘制
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
hdu2097 nyoj414 sky数 (进制转换)
“蘑菇书”是怎样磨出来的?
60行从零开始自己动手写FutureTask是什么体验?
2022年6月互联网医疗领域月度观察
623. Add a row to a binary tree: Simple binary tree traversal problems
碘乙酰胺在Desthiobiotin-Iodoacetamide试剂中的作用?
学习用于视觉跟踪的深度紧凑图像表示
支持向量机SVM
解决2022Visual Studio中scanf返回值被忽略问题