当前位置:网站首页>【分享】代码编程习惯:在多参数的方法的情况下需要对方法整理-即最多参数的方法为主要实现
【分享】代码编程习惯:在多参数的方法的情况下需要对方法整理-即最多参数的方法为主要实现
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);
}
整合到一块,代码就比较清晰。
边栏推荐
- 常见的 web 安全问题总结
- 莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》
- Go compilation principle series 9 (function inlining)
- Visit GOPS Long Zhi booth, Forrester's latest report: "the Forrester Wave: the fourth quarter of 2021 enterprise service management report
- The principle and application scenario of mysql master-slave synchronization
- Android 开发用 Kotlin 编程语言一 基本数据类型
- I'm going crazy.Again A few days can not be A problem
- Student Information Management System (first time...)
- 【HMS core】【FAQ】Health Kit, Ads kit, Push Kit Typical Questions Collection 5
- PMP每日一练 | 考试不迷路-8.5(包含敏捷+多选)
猜你喜欢
2022年6月互联网医疗领域月度观察
对于聚合物聚乙二醇PEG大家了解多少了?以及在生活中的应用
STM32H743IIT6学习笔记03——使用第三方组件FreeRTOS
机器学习——逻辑回归
Version Control | Longzhi invites you to go to the GOPS Global Operation and Maintenance Conference to explore the road of large-scale, agile, high-quality and open software development and operation
“小钢炮”气质明显,安全、舒适一个不落
基于NSQ搭建高可用分布式消息队列
163_Tricks_Power BI one-click batch creation of custom field parameters
莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》
Flink Yarn Per Job - JobManger 申请 Slot
随机推荐
女人是这个世界上最美丽的生命
LeetCode brush questions (8)
el-menu箭头改为右下
“蘑菇书”是怎样磨出来的?
消息中间件汇总
对于聚合物聚乙二醇PEG大家了解多少了?以及在生活中的应用
Student Information Management System (first time...)
停电。。。烦烦烦!!!
Naive bayes
Qt::qcustomplot 和 qchart数据填充相关
【名词】什么是PV和UV?
硅谷来信:快速行动,Facebook、Quora等成功的“神器”!
Flink Yarn Per Job - RM启动SlotManager
数据治理体系演进简介
STM32H743IIT6学习笔记03——使用第三方组件FreeRTOS
No developers, received a job to develop an IoT system, do you want to do it?
普通二本毕业八年,京东就职两年、百度三年,分享大厂心得
【硬件架构的艺术】学习笔记(2)同步和复位
常用的免费Api接口网址
Http-Sumggling Cache Vulnerability Analysis