当前位置:   article > 正文

android常用工具类 --- UI操作_android 公用类操作ui

android 公用类操作ui
  1. public class UIUtils {
  2. public static Context getContext() {
  3. return XXApplication.getApplication();
  4. }
  5. public static Thread getMainThread() {
  6. return XXApplication.getMainThread();
  7. }
  8. public static long getMainThreadId() {
  9. return XXApplication.getMainThreadId();
  10. }
  11. /**
  12. * dip转换px
  13. */
  14. public static int dip2px(int dip) {
  15. final float scale = getContext().getResources().getDisplayMetrics().density;
  16. return (int) (dip * scale + 0.5f);
  17. }
  18. /**
  19. * pxz转换dip
  20. */
  21. public static int px2dip(int px) {
  22. final float scale = getContext().getResources().getDisplayMetrics().density;
  23. return (int) (px / scale + 0.5f);
  24. }
  25. /**
  26. * 获取主线程的handler
  27. */
  28. public static Handler getHandler() {
  29. return XXApplication.getMainThreadHandler();
  30. }
  31. /**
  32. * 延时在主线程执行runnable
  33. */
  34. public static boolean postDelayed(Runnable runnable, long delayMillis) {
  35. return getHandler().postDelayed(runnable, delayMillis);
  36. }
  37. /**
  38. * 在主线程执行runnable
  39. */
  40. public static boolean post(Runnable runnable) {
  41. return getHandler().post(runnable);
  42. }
  43. /**
  44. * 从主线程looper里面移除runnable
  45. */
  46. public static void removeCallbacks(Runnable runnable) {
  47. getHandler().removeCallbacks(runnable);
  48. }
  49. public static View inflate(int resId) {
  50. return LayoutInflater.from(getContext()).inflate(resId, null);
  51. }
  52. /**
  53. * 获取资源
  54. */
  55. public static Resources getResources() {
  56. return getContext().getResources();
  57. }
  58. /**
  59. * 获取文字
  60. */
  61. public static String getString(int resId) {
  62. return getResources().getString(resId);
  63. }
  64. /**
  65. * 获取文字数组
  66. */
  67. public static String[] getStringArray(int resId) {
  68. return getResources().getStringArray(resId);
  69. }
  70. /**
  71. * 获取dimen
  72. */
  73. public static int getDimens(int resId) {
  74. return getResources().getDimensionPixelSize(resId);
  75. }
  76. /**
  77. * 获取drawable
  78. */
  79. @SuppressWarnings("deprecation")
  80. public static Drawable getDrawable(int resId) {
  81. return getResources().getDrawable(resId);
  82. }
  83. /**
  84. * 获取颜色
  85. */
  86. public static int getColor(int resId) {
  87. return getResources().getColor(resId);
  88. }
  89. /**
  90. * 获取颜色选择器
  91. */
  92. public static ColorStateList getColorStateList(int resId) {
  93. return getResources().getColorStateList(resId);
  94. }
  95. /**
  96. * 判断当前的线程是不是在主线程
  97. *
  98. * @return
  99. */
  100. public static boolean isRunInMainThread() {
  101. return android.os.Process.myTid() == getMainThreadId();
  102. }
  103. public static void runInMainThread(Runnable runnable) {
  104. if (isRunInMainThread()) {
  105. runnable.run();
  106. } else {
  107. post(runnable);
  108. }
  109. }
  110. /**
  111. * 对toast的简易封装。线程安全,可以在非UI线程调用。
  112. */
  113. public static void showToastSafe(final int resId) {
  114. showToastSafe(getString(resId));
  115. }
  116. /**
  117. * 对toast的简易封装。线程安全,可以在非UI线程调用。
  118. */
  119. public static void showToastSafe(final String str) {
  120. if (isRunInMainThread()) {
  121. showToast(str);
  122. } else {
  123. post(new Runnable() {
  124. @Override
  125. public void run() {
  126. showToast(str);
  127. }
  128. });
  129. }
  130. }
  131. private static Toast mToast;
  132. private static void showToast(String text) {
  133. if (mToast == null) {
  134. mToast = Toast.makeText(UIUtils.getContext(), text, Toast.LENGTH_SHORT);
  135. } else {
  136. mToast.setText(text);
  137. mToast.setDuration(Toast.LENGTH_SHORT);
  138. }
  139. mToast.show();
  140. }
  141. /**
  142. * 得到屏幕的高度
  143. *
  144. * @param activity
  145. * @return
  146. */
  147. @SuppressWarnings("deprecation")
  148. public static int getSreenHeight(Activity activity) {
  149. return activity.getWindowManager().getDefaultDisplay().getHeight();
  150. }
  151. /**
  152. * 得到屏幕的宽度
  153. *
  154. * @param activity
  155. * @return
  156. */
  157. @SuppressWarnings("deprecation")
  158. public static int getSreenWidth(Activity activity) {
  159. return activity.getWindowManager().getDefaultDisplay().getWidth();
  160. }
  161. /**
  162. * 得到一个控件相对于屏幕左侧的位置
  163. *
  164. * @param view
  165. * @return
  166. */
  167. public static int getLeftOnScreen(View view) {
  168. int[] location = new int[2];
  169. view.getLocationOnScreen(location);
  170. return location[0];
  171. }
  172. /**
  173. * 得到一个控件相对于屏幕左侧的位置
  174. *
  175. * @param view
  176. * @return
  177. */
  178. public static int getRightOnScreen(View view) {
  179. int[] location = new int[2];
  180. view.getLocationOnScreen(location);
  181. return location[0];
  182. }
  183. /**
  184. * 得到一个控件相对于屏幕顶部的位置
  185. *
  186. * @param view
  187. * @return
  188. */
  189. public static int getTopOnScreen(View view) {
  190. int[] location = new int[2];
  191. view.getLocationOnScreen(location);
  192. return location[1];
  193. }
  194. /**
  195. * 获得状态栏的高度
  196. *
  197. * @return
  198. */
  199. public static int getStatusHeight() {
  200. int statusHeight = -1;
  201. try {
  202. Class clazz = Class.forName("com.android.internal.R$dimen");
  203. Object object = clazz.newInstance();
  204. int height = Integer.parseInt(clazz.getField("status_bar_height")
  205. .get(object).toString());
  206. statusHeight = getResources().getDimensionPixelSize(height);
  207. } catch (Exception e) {
  208. e.printStackTrace();
  209. }
  210. return statusHeight;
  211. }
  212. /**
  213. * 设置沉浸式状态栏,以一个高度为0的View为基础(在每个Activity的setContentView之后添加效果为佳)
  214. *
  215. * @param activity 当前的Activity
  216. * @param view 高度为0的view
  217. * @param type 根布局的类型,线性布局为1,相对布局为2
  218. */
  219. public static void setStatusColor(Activity activity, View view, int type, int i) {
  220. try {
  221. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  222. activity.getWindow().addFlags(
  223. WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  224. activity.getWindow().addFlags(
  225. WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  226. if (1 == type) {
  227. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.getStatusHeight());
  228. view.setLayoutParams(params);
  229. } else if (2 == type) {
  230. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, UIUtils.getStatusHeight());
  231. view.setLayoutParams(params);
  232. }
  233. view.setBackgroundColor(getColor(R.color.our_main_color));
  234. } else {
  235. view.setVisibility(View.GONE);
  236. }
  237. } catch (Exception e) {
  238. view.setVisibility(View.GONE);
  239. e.printStackTrace();
  240. Log.e("MainActivity", "沉浸式状态栏设置出错");
  241. }
  242. }
  243. /**
  244. * 实现文本复制功能
  245. * 注意:导包的时候
  246. * API 11之前: android.text.ClipboardManager
  247. * API 11之后: android.content.ClipboardManager
  248. *
  249. * @param content
  250. */
  251. public static void copy(String content) {
  252. // 得到剪贴板管理器
  253. ClipboardManager cmb = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
  254. cmb.setText(content.trim());
  255. }
  256. /**
  257. * 实现粘贴功能
  258. *
  259. * @return
  260. */
  261. public static String paste() {
  262. // 得到剪贴板管理器
  263. ClipboardManager cmb = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
  264. return cmb.getText().toString().trim();
  265. }
  266. public static void setHeightByWidth(View view, float ratio) {
  267. if (ratio == 0) {
  268. throw new IllegalArgumentException("比例不能为零!");
  269. } else {
  270. view.getLayoutParams().height = (int) (view.getLayoutParams().width / ratio);
  271. }
  272. }
  273. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/928926
推荐阅读
相关标签
  

闽ICP备14008679号