UiUtils.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package com.cooleshow.base.utils;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Point;
  5. import android.graphics.Rect;
  6. import android.graphics.Typeface;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.Handler;
  9. import android.os.Looper;
  10. import android.text.Spannable;
  11. import android.text.SpannableString;
  12. import android.text.Spanned;
  13. import android.text.TextUtils;
  14. import android.text.style.ForegroundColorSpan;
  15. import android.text.style.ImageSpan;
  16. import android.text.style.StyleSpan;
  17. import android.text.style.TextAppearanceSpan;
  18. import android.util.DisplayMetrics;
  19. import android.view.LayoutInflater;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.TextView;
  23. import com.cooleshow.base.R;
  24. import com.cooleshow.base.common.BaseApplication;
  25. import com.cooleshow.base.constanst.StyleConfig;
  26. import com.cooleshow.base.widgets.DialogUtil;
  27. import com.cooleshow.base.widgets.span.QMUIAlignMiddleImageSpan;
  28. import com.rui.common_base.widget.span.CenterImageSpan;
  29. import java.math.BigDecimal;
  30. import java.text.ParseException;
  31. import java.text.SimpleDateFormat;
  32. import java.util.ArrayList;
  33. import java.util.Date;
  34. import java.util.regex.Matcher;
  35. import java.util.regex.Pattern;
  36. import androidx.fragment.app.FragmentManager;
  37. /**
  38. * Description:
  39. * Copyright : Copyright (c) 2019
  40. * Company : 大雅乐盟
  41. * Author : r
  42. * Date : 2019/8/22 15:08
  43. */
  44. public class UiUtils {
  45. private final static Handler mainHand = new Handler(Looper.getMainLooper());
  46. public static void postDelayed(Runnable r, long delay) {
  47. mainHand.postDelayed(r, delay);
  48. }
  49. public static void setMargins(View v, int l, int t, int r, int b) {
  50. if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
  51. ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
  52. p.setMargins(l, t, r, b);
  53. v.requestLayout();
  54. }
  55. }
  56. public static SpannableString matcherSearchText(Context context, String text, int keyword, int color) {
  57. String key = keyword + "";
  58. SpannableString ss = new SpannableString(text);
  59. Pattern pattern = Pattern.compile(key);
  60. Matcher matcher = pattern.matcher(ss);
  61. while (matcher.find()) {
  62. int start = matcher.start();
  63. int end = matcher.end();
  64. ss.setSpan(new TextAppearanceSpan(context, color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//new ForegroundColorSpan(color)
  65. }
  66. return ss;
  67. }
  68. public static String formateTime(String dateString, String format) {
  69. if (TextUtils.isEmpty(dateString)) return "";
  70. try {
  71. Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
  72. String ds = new SimpleDateFormat(format).format(date);
  73. return ds;
  74. } catch (ParseException e) {
  75. return "";
  76. }
  77. }
  78. public static String formateHourTime(String dateString, String format) {
  79. if (TextUtils.isEmpty(dateString)) return "";
  80. try {
  81. Date date = new SimpleDateFormat("HH:mm:ss").parse(dateString);
  82. String ds = new SimpleDateFormat(format).format(date);
  83. return ds;
  84. } catch (ParseException e) {
  85. return "";
  86. }
  87. }
  88. // 两次点击按钮之间的点击间隔不能少于1000毫秒
  89. private static final int MIN_CLICK_DELAY_TIME = 1000;
  90. private static long lastClickTime;
  91. public static boolean isFastClick() {
  92. boolean flag = false;
  93. long curClickTime = System.currentTimeMillis();
  94. if ((curClickTime - lastClickTime) < MIN_CLICK_DELAY_TIME) {
  95. flag = true;
  96. }
  97. lastClickTime = curClickTime;
  98. return flag;
  99. }
  100. public static boolean isFastClick(long timeLimit) {
  101. boolean flag = false;
  102. long curClickTime = System.currentTimeMillis();
  103. if ((curClickTime - lastClickTime) < timeLimit) {
  104. flag = true;
  105. }
  106. lastClickTime = curClickTime;
  107. return flag;
  108. }
  109. public static int getMaxHeightAtRatio16_9(Context context) {
  110. float ratio = 16 / 9f;
  111. return getMaxHeightAtAspectRatio(context, ratio);
  112. }
  113. public static int getHeightAtRatio16_9(Context context, float width) {
  114. float ratio = 16 / 9f;
  115. return (int) (width / ratio);
  116. }
  117. public static int getMaxHeightAtAspectRatio(Context context, float ratio) {
  118. DisplayMetrics deviceSize = DeviceUtil.getDeviceSize(context);
  119. if (deviceSize != null) {
  120. int width = deviceSize.widthPixels;
  121. return (int) (width / ratio);
  122. }
  123. return 0;
  124. }
  125. public static SpannableString diffColorString(String bigSizeStr, String centerStr, String lastStr, int firstColor, int centerColor) {
  126. String tmpStr = bigSizeStr + centerStr + lastStr;
  127. SpannableString result = new SpannableString(tmpStr);
  128. try {
  129. result.setSpan(new ForegroundColorSpan(firstColor), 0, bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  130. result.setSpan(new ForegroundColorSpan(centerColor), bigSizeStr.length(), bigSizeStr.length() + centerStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  131. result.setSpan(new ForegroundColorSpan(firstColor), bigSizeStr.length() + centerStr.length(), tmpStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  132. } catch (Exception e) {
  133. e.printStackTrace();
  134. }
  135. return result;
  136. }
  137. public static SpannableString diffColorStringAndBold(String bigSizeStr, String centerStr, String lastStr, int firstColor, int centerColor) {
  138. String tmpStr = bigSizeStr + centerStr + lastStr;
  139. SpannableString result = new SpannableString(tmpStr);
  140. try {
  141. result.setSpan(new ForegroundColorSpan(firstColor), 0, bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  142. result.setSpan(new ForegroundColorSpan(centerColor), bigSizeStr.length(), bigSizeStr.length() + centerStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  143. result.setSpan(new StyleSpan(Typeface.BOLD), bigSizeStr.length(), bigSizeStr.length() + centerStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  144. result.setSpan(new ForegroundColorSpan(firstColor), bigSizeStr.length() + centerStr.length(), tmpStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. }
  148. return result;
  149. }
  150. public static SpannableString diffColorString(String bigSizeStr, String lastStr, int firstColor, int lastColor) {
  151. String tmpStr = bigSizeStr + lastStr;
  152. SpannableString result = new SpannableString(tmpStr);
  153. try {
  154. result.setSpan(new ForegroundColorSpan(firstColor), 0, bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  155. result.setSpan(new ForegroundColorSpan(lastColor), bigSizeStr.length(), tmpStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  156. } catch (Exception e) {
  157. e.printStackTrace();
  158. }
  159. return result;
  160. }
  161. public static SpannableString diffColorString(String bigSizeStr, String lastStr, int firstColor, int lastColor, Drawable iconDrawable) {
  162. String tmpStr = bigSizeStr + lastStr;
  163. try {
  164. String icon = "[icon]";
  165. SpannableString result = new SpannableString("[icon]" + tmpStr);
  166. if (iconDrawable != null) {
  167. iconDrawable.setBounds(0, 0, iconDrawable.getIntrinsicWidth(), iconDrawable.getIntrinsicHeight());
  168. }
  169. ImageSpan alignMiddleImageSpan = new CenterImageSpan(iconDrawable, ImageSpan.ALIGN_BOTTOM, 4);
  170. result.setSpan(alignMiddleImageSpan, 0, icon.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
  171. result.setSpan(new ForegroundColorSpan(firstColor), icon.length(), icon.length() + bigSizeStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  172. result.setSpan(new ForegroundColorSpan(lastColor), icon.length() + bigSizeStr.length(), result.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  173. return result;
  174. } catch (Exception e) {
  175. e.printStackTrace();
  176. SpannableString spannableString = new SpannableString(tmpStr);
  177. return spannableString;
  178. }
  179. }
  180. public static String[] splitSubjectName(String subjectName) {
  181. if (TextUtils.isEmpty(subjectName)) {
  182. return null;
  183. }
  184. String[] split = subjectName.split(",");
  185. return split;
  186. }
  187. public static String[] splitSubjectId(String subjectId) {
  188. if (TextUtils.isEmpty(subjectId)) {
  189. return null;
  190. }
  191. String[] split = subjectId.split(",");
  192. return split;
  193. }
  194. /**
  195. * 保留2位小数
  196. *
  197. * @param targetValue
  198. * @return
  199. */
  200. public static String convertDouble(double targetValue) {
  201. BigDecimal bd = new BigDecimal(targetValue);
  202. double result = bd.setScale(2, BigDecimal.ROUND_DOWN).doubleValue(); // 保留两位小数,不四舍五入(可选舍入模式)
  203. return String.valueOf(result);
  204. }
  205. public static String getCourseTimeString(String start, String end) {
  206. if (TextUtils.isEmpty(start) || TextUtils.isEmpty(end)) {
  207. return "";
  208. }
  209. if (TextUtils.isEmpty(start) && !TextUtils.isEmpty(end)) {
  210. return TimeUtils.date2String(TimeUtils.getDate(start), "yyyy-MM-dd HH:mm");
  211. }
  212. if (!TextUtils.isEmpty(start) && TextUtils.isEmpty(end)) {
  213. return TimeUtils.date2String(TimeUtils.getDate(end), "yyyy-MM-dd HH:mm");
  214. }
  215. String time = "";
  216. String startTime = TimeUtils.date2String(TimeUtils.getDate(start), "yyyy-MM-dd");
  217. String endTime = TimeUtils.date2String(TimeUtils.getDate(end), "yyyy-MM-dd");
  218. String startTimeText = TimeUtils.date2String(TimeUtils.getDate(start), "HH:mm");
  219. String endTimeText = TimeUtils.date2String(TimeUtils.getDate(end), "HH:mm");
  220. if (TextUtils.equals(startTime, endTime)) {
  221. time = startTime + " " + startTimeText + "~" + endTimeText;
  222. } else {
  223. time = startTime + " " + startTimeText + "~" + endTime + " " + endTimeText;
  224. }
  225. return time;
  226. }
  227. public static String getPianoCourseName(String courseTitle, int classNum) {
  228. if (classNum != 0) {
  229. return courseTitle + "第" + classNum + "课";
  230. } else {
  231. return courseTitle;
  232. }
  233. }
  234. public static String getVideoCoursePriceText(String lessonPrice, int lessonCount) {
  235. String price = "¥" + lessonPrice;
  236. try {
  237. double v = Double.parseDouble(lessonPrice);
  238. if (v == 0) {
  239. price = Utils.getApp().getString(R.string.price_free_str);
  240. }
  241. } catch (Exception e) {
  242. e.printStackTrace();
  243. }
  244. return price + "/" + lessonCount + "课时";
  245. }
  246. public static String getVideoCoursePriceText2(String lessonPrice) {
  247. String price = lessonPrice;
  248. try {
  249. double v = Double.parseDouble(lessonPrice);
  250. if (v == 0) {
  251. price = Utils.getApp().getString(R.string.price_free_str);
  252. }
  253. } catch (Exception e) {
  254. e.printStackTrace();
  255. }
  256. return price;
  257. }
  258. public static boolean isFree(String price) {
  259. try {
  260. double v = Double.parseDouble(price);
  261. return v == 0;
  262. } catch (Exception e) {
  263. e.printStackTrace();
  264. }
  265. return false;
  266. }
  267. public static String getBuyNumTip(String lessonPrice, String buyCount) {
  268. try {
  269. double v = Double.parseDouble(lessonPrice);
  270. if (v == 0) {
  271. return buyCount + "人已领取";
  272. }
  273. } catch (Exception e) {
  274. e.printStackTrace();
  275. }
  276. return buyCount + "人已购买";
  277. }
  278. public static String getBuyNumTip2(String lessonPrice, String buyCount) {
  279. return buyCount + "人学习";
  280. }
  281. public static String getLiveCoursePriceText(String lessonPrice, int lessonCount) {
  282. String price = "¥" + lessonPrice;
  283. try {
  284. double v = Double.parseDouble(lessonPrice);
  285. if (v == 0) {
  286. price = Utils.getApp().getString(R.string.price_free_str);
  287. }
  288. } catch (Exception e) {
  289. e.printStackTrace();
  290. }
  291. return price;
  292. }
  293. public static String getLiveCoursePriceTextForStu(String lessonPrice) {
  294. String price = lessonPrice;
  295. try {
  296. double v = Double.parseDouble(price);
  297. if (v == 0) {
  298. price = Utils.getApp().getString(R.string.price_free_str);
  299. }
  300. } catch (Exception e) {
  301. e.printStackTrace();
  302. }
  303. return price;
  304. }
  305. public static String getVideoCoursePriceTextForStu(String lessonPrice) {
  306. String price = lessonPrice;
  307. try {
  308. double v = Double.parseDouble(price);
  309. if (v == 0) {
  310. price = Utils.getApp().getString(R.string.price_free_str);
  311. }
  312. } catch (Exception e) {
  313. e.printStackTrace();
  314. }
  315. return price;
  316. }
  317. public static boolean getLocalVisibleRect(Activity activity, View view, int offsetY) {
  318. Point p = new Point();
  319. activity.getWindowManager().getDefaultDisplay().getSize(p);
  320. int screenWidth = p.x;
  321. int screenHeight = p.y;
  322. Rect rect = new Rect(0, 0, screenWidth, screenHeight);//得到屏幕矩阵
  323. int[] location = new int[2];
  324. location[1] = location[1] + SizeUtils.dp2px(offsetY);
  325. view.getLocationInWindow(location);
  326. view.setTag(location[1]);//存储y方向的位置
  327. if (view.getLocalVisibleRect(rect)) {
  328. return true;
  329. } else {
  330. return false;
  331. }
  332. }
  333. public static String formatIdParams(ArrayList<String> list) {
  334. if (list == null || list.size() == 0) {
  335. return "";
  336. }
  337. StringBuilder stringBuilder = new StringBuilder();
  338. for (int i = 0; i < list.size(); i++) {
  339. stringBuilder.append(list.get(i));
  340. if (i != list.size() - 1) {
  341. stringBuilder.append(",");
  342. }
  343. }
  344. return stringBuilder.toString();
  345. }
  346. public static void showPermissionTipDialog(FragmentManager fragmentmanager, Context context, String title, String tip) {
  347. DialogUtil.showInCenter(fragmentmanager, com.cooleshow.base.R.layout.accompany_permissions_popu, (holder, dialog) -> {
  348. TextView tvTitle = holder.getView(com.cooleshow.base.R.id.tv_title);
  349. TextView tvContent = holder.getView(com.cooleshow.base.R.id.tv_content);
  350. View btncancel = holder.getView(com.cooleshow.base.R.id.btn_cancel);
  351. View btnCommit = holder.getView(com.cooleshow.base.R.id.btn_commit);
  352. tvTitle.setText(title);
  353. tvContent.setText(tip);
  354. btncancel.setOnClickListener(view1 -> {
  355. dialog.dismiss();
  356. });
  357. btnCommit.setOnClickListener(view1 -> {
  358. PermissionUtils.toSelfSetting(context);
  359. dialog.dismiss();
  360. });
  361. });
  362. }
  363. public static void refreshFilterTextStyle(boolean isShow, TextView textView) {
  364. if (textView == null) {
  365. return;
  366. }
  367. int mainColor = StyleConfig.getMainColor();
  368. textView.setTextColor(textView.getContext().getResources().getColor(isShow ? mainColor : R.color.color_333333));
  369. int drawableRight = 0;
  370. int selectDrawable = StyleConfig.isStudentStyle ? R.drawable.icon_arrow_top_blue : R.drawable.icon_arrow_top_red;
  371. drawableRight = isShow ? selectDrawable : R.drawable.icon_arrow_down3;
  372. textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRight, 0);
  373. }
  374. public static void refreshFilterTextStyle2(boolean isShow, TextView textView) {
  375. if (textView == null) {
  376. return;
  377. }
  378. int mainColor = StyleConfig.getMainColor();
  379. textView.setTextColor(textView.getContext().getResources().getColor(isShow ? mainColor : R.color.color_333333));
  380. int drawableRight = 0;
  381. int selectDrawable = R.drawable.icon_arrow_top_green;
  382. drawableRight = isShow ? selectDrawable : R.drawable.icon_arrow_down;
  383. textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRight, 0);
  384. }
  385. }