Android代码优化----Application节点的模板写法及UI工具类

简介:

一、 MyApplication类的编写:

新建一个类MyApplication,继承自Application。代码如下:

MyApplication.java:

复制代码
 1 package com.smyhvae.homepicdemo;
 2 
 3 import android.app.Application;
 4 import android.os.Handler;
 5 import android.os.Looper;
 6 
 7 /**
 8  * Created by smyhvae on 2015/5/13.
 9  */
10 public class MyApplication extends Application {
11     //获取到主线程的上下文
12     private static MyApplication mContext = null;
13     //获取到主线程的handler
14     private static Handler mMainThreadHandler = null;
15     //获取到主线程的looper
16     private static Looper mMainThreadLooper = null;
17     //获取到主线程
18     private static Thread mMainThead = null;
19     //获取到主线程的id
20     private static int mMainTheadId;
21 
22     @Override
23     public void onCreate() {
24         // TODO Auto-generated method stub
25         super.onCreate();
26         this.mContext = this;
27         this.mMainThreadHandler = new Handler();
28         this.mMainThreadLooper = getMainLooper();
29         this.mMainThead = Thread.currentThread();
30         //android.os.Process.myUid()   获取到用户id
31         //android.os.Process.myPid()获取到进程id
32         //android.os.Process.myTid()获取到调用线程的id
33         this.mMainTheadId = android.os.Process.myTid();
34     }
35 
36     public static MyApplication getApplication() {
37         return mContext;
38     }
39 
40     public static Handler getMainThreadHandler() {
41         return mMainThreadHandler;
42     }
43 
44     public static Looper getMainThreadLooper() {
45         return mMainThreadLooper;
46     }
47 
48     public static Thread getMainThread() {
49         return mMainThead;
50     }
51 
52     public static int getMainThreadId() {
53         return mMainTheadId;
54     }
55 
56 }
复制代码

上面的所有代码每次在开发一个新的app时都需要用到的,然后具体到不同的项目,再继续添加不同的东西。

然后记得在清单文件中进行声明:

1     <application
2         android:allowBackup="true"
3         android:icon="@mipmap/ic_launcher"
4         android:label="@string/app_name"
5         android:theme="@style/AppTheme" 
6         android:name=".MyApplication">

需要声明的是上方代码的第6行android:name的属性。

 

二、UI工具类的编写:

这个工具类也是在app开发中经常用到的。可以直接copy。代码如下:

UIUtils.java:

复制代码
  1 package com.smyhvae.homepicdemo.utils;
  2 
  3 import android.content.Context;
  4 import android.content.Intent;
  5 import android.content.res.ColorStateList;
  6 import android.content.res.Resources;
  7 import android.graphics.drawable.Drawable;
  8 import android.os.Handler;
  9 import android.view.LayoutInflater;
 10 import android.view.View;
 11 
 12 import com.smyhvae.homepicdemo.MyApplication;
 13 
 14 /**
 15  * Created by smyhvae on 2015/5/13.
 16  */
 17 
 18 public class UIUtils {
 19 
 20     
 21     public static Context getContext() {
 22         return MyApplication.getApplication();
 23     }
 24 
 25     public static Thread getMainThread() {
 26         return MyApplication.getMainThread();
 27     }
 28 
 29     public static long getMainThreadId() {
 30         return MyApplication.getMainThreadId();
 31     }
 32 
 33     /** dip转换px */
 34     public static int dip2px(int dip) {
 35         final float scale = getContext().getResources().getDisplayMetrics().density;
 36         return (int) (dip * scale + 0.5f);
 37     }
 38 
 39     /** pxz转换dip */
 40     public static int px2dip(int px) {
 41         final float scale = getContext().getResources().getDisplayMetrics().density;
 42         return (int) (px / scale + 0.5f);
 43     }
 44 
 45     /** 获取主线程的handler */
 46     public static Handler getHandler() {
 47         return MyApplication.getMainThreadHandler();
 48     }
 49 
 50     /** 延时在主线程执行runnable */
 51     public static boolean postDelayed(Runnable runnable, long delayMillis) {
 52         return getHandler().postDelayed(runnable, delayMillis);
 53     }
 54 
 55     /** 在主线程执行runnable */
 56     public static boolean post(Runnable runnable) {
 57         return getHandler().post(runnable);
 58     }
 59 
 60     /** 从主线程looper里面移除runnable */
 61     public static void removeCallbacks(Runnable runnable) {
 62         getHandler().removeCallbacks(runnable);
 63     }
 64 
 65     public static View inflate(int resId){
 66         return LayoutInflater.from(getContext()).inflate(resId,null);
 67     }
 68 
 69     /** 获取资源 */
 70     public static Resources getResources() {
 71         
 72         return getContext().getResources();
 73     }
 74 
 75     /** 获取文字 */
 76     public static String getString(int resId) {
 77         return getResources().getString(resId);
 78     }
 79 
 80     /** 获取文字数组 */
 81     public static String[] getStringArray(int resId) {
 82         return getResources().getStringArray(resId);
 83     }
 84 
 85     /** 获取dimen */
 86     public static int getDimens(int resId) {
 87         return getResources().getDimensionPixelSize(resId);
 88     }
 89 
 90     /** 获取drawable */
 91     public static Drawable getDrawable(int resId) {
 92         return getResources().getDrawable(resId);
 93     }
 94 
 95     /** 获取颜色 */
 96     public static int getColor(int resId) {
 97         return getResources().getColor(resId);
 98     }
 99 
100     /** 获取颜色选择器 */
101     public static ColorStateList getColorStateList(int resId) {
102         return getResources().getColorStateList(resId);
103     }
104     //判断当前的线程是不是在主线程 
105     public static boolean isRunInMainThread() {
106         return android.os.Process.myTid() == getMainThreadId();
107     }
108     
109     public static void runInMainThread(Runnable runnable) {
110         if (isRunInMainThread()) {
111             runnable.run();
112         } else {
113             post(runnable);
114         }
115     }
116 
117     public static void startActivity(Intent intent){
118 //        BaseActivity activity = BaseActivity.getForegroundActivity();
119 //        if(activity != null){
120 //            activity.startActivity(intent);
121 //        }else{
122 //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
123 //            getContext().startActivity(intent);
124 //        }
125     }
126 
127     /** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
128     public static void showToastSafe(final int resId) {
129         showToastSafe(getString(resId));
130     }
131 
132     /** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
133     public static void showToastSafe(final String str) {
134         if (isRunInMainThread()) {
135             showToast(str);
136         } else {
137             post(new Runnable() {
138                 @Override
139                 public void run() {
140                     showToast(str);
141                 }
142             });
143         }
144     }
145 
146     private static void showToast(String str) {
147 //        BaseActivity frontActivity = BaseActivity.getForegroundActivity();
148 //        if (frontActivity != null) {
149 //            Toast.makeText(frontActivity, str, Toast.LENGTH_LONG).show();
150 //        }
151     }
152 }
复制代码

 

相关文章
|
17天前
|
消息中间件 安全 数据处理
Android为什么不能在子线程更新UI
Android为什么不能在子线程更新UI
23 0
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
41 1
|
3月前
|
开发工具 Android开发 开发者
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
31 4
|
3月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
31 2
|
3月前
|
Android开发 容器
Android UI设计: 什么是View和ViewGroup?
Android UI设计: 什么是View和ViewGroup?
36 0
|
3月前
|
数据可视化 Android开发 容器
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
92 5
|
6天前
|
编解码 Android开发 UED
安卓UI/UX设计原则:打造引人入胜的用户体验
【4月更文挑战第13天】本文探讨了安卓UI/UX设计的关键原则,包括一致性、简洁性、反馈、清晰性、效率和适应性。一致性要求视觉和行为保持一致,利用系统UI;简洁性减少用户行动,简化导航;反馈需即时且明确;清晰性强调表达清晰,布局有序;效率关注性能优化和任务简化;适应性涉及多设备适配和用户多样性。遵循这些原则,可创建出色应用,提供无缝用户体验。设计应持续迭代,适应技术发展和用户需求。
|
11天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
12天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
1月前
|
XML API Android开发
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
【Android 从入门到出门】第三章:使用Hilt处理Jetpack Compose UI状态
26 4