初始化一个项目工程
来源:互联网 发布:外卖电话软件 编辑:程序博客网 时间:2024/06/05 20:20
这篇是作为谷歌市场项目的开头(源码网上都有),作为学习的一部分详细记录了完成一个项目完成到结束需要考虑的架构和可能潜在的问题。也不是自己写的,只是跟着一点点跟下来码的作为自己的一个学习参考
1.创建GooglePlayApplication做全局管理
public class GooglePlayApplication extends Application { private static Context context; private static Handler handler; private static int mainThreadId; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); handler = new Handler(); //主线程id //拿到当前线程id,此处是主线程id mainThreadId = android.os.Process.myTid();// android.os.Process.myPid();//拿到进程id } public static Context getContext() { return context; } public static Handler getHandler() { return handler; } public static int getMainThreadId() { return mainThreadId; }}
2.常用的UI工具
public class UIUtils { public static Context getContext(){ return GooglePlayApplication.getContext(); } public static Handler getHandler(){ return GooglePlayApplication.getHandler(); } public static int getMainThreadId(){ return GooglePlayApplication.getMainThreadId(); } ///////////////////////加载资源文件/////////////////////////// //获取字符串 public static String getString(int id){ return getContext().getResources().getString(id); } //获取字符串数组 public static String[] getStringArray(int id){ return getContext().getResources().getStringArray(id); } //获取图片 public static Drawable getDrawable(int id){ return getContext().getResources().getDrawable(id); } //获取颜色 public static int getColor(int id){ return getContext().getResources().getColor(id); } public static ColorStateList getColorStateList(int id){ return getContext().getResources().getColorStateList(id); } //获取尺寸 public static int getDimens(int id){ return getContext().getResources().getDimensionPixelSize(id);//返回具体像素值 } /////////////////////dip和px转换的逻辑////////////////////////// public static int dip2px(float dip){ float density = getContext().getResources().getDisplayMetrics().density; return (int)(dip*density+0.5f); } public static float px2dip(int px){ float density = getContext().getResources().getDisplayMetrics().density; return px/density; } ////////////////////加载布局文件///////////////////// public static View inflate(int id){ return View.inflate(getContext(),id,null); } /////////////////////判断是否运行在主线程///////////////////// public static boolean isRunOnUIThread(){ //获取当前线程id,如果当前线程id和主线程id相同,就是运行在当前线程中 int myTid = android.os.Process.myTid(); if(myTid == getMainThreadId()){ return true; } return false; } public static void runOnUIThread(Runnable r){ if(isRunOnUIThread()){ //已经是主线程,直接运行 r.run(); }else{ //如果是子线程,借助handler让其运行在主线程 getHandler().post(r); } }}
阅读全文
0 0
- 初始化一个项目工程
- 初始化一个mvn工程
- 初始化一个Django项目
- 初始化一个express项目
- 初始化一个vue项目
- 一个项目多web工程
- 在oschina上创建项目并初始化工程
- 一个项目中初始化的问题
- MFC一个工程添加多个项目
- android 中一个项目工程引用另一个项目工程和jar
- android项目中曾样让一个工程引用另外一个工程的接口
- WEEX|初始化工程
- 初始化一个带npm的package.json和TypeScript项目
- git初始化一个项目时加入.gitignore文件的内容
- [快乐android1]建立一个新工程的输入项目
- 多个项目工程(类库)合并成一个Dll
- SVN管理的项目工程创建一个分支
- 一个android工程代码多个差异化项目管理方法探讨
- LeetCode133 Clone Graph
- SARSA和Q-learning算法
- Oracle基本操作十二:存储过程及定时调用存储过程
- 深入理解ArrayList与LinkedList的区别
- 【算法题】0-1 背包问题
- 初始化一个项目工程
- 错误日记 函数返回数组
- 平面坐标系绕原点顺时针变化
- swjtuoj 2381 Matrix
- 如何消除Permission is only granted to system apps报错
- windows环境下使用git客户端、GitHub和TortoiseGit管理项目代码
- 使用原生JDBC从数据库读取数据并且封装
- 【读书笔记】大规模Web开发技术(前言)
- JavaWeb开发知识总结(JSP,EL,JSTL)