Android学习日记

年前后一直在搭建新的项目框架并结合MVP架构来规划新的APP,十分费心,但也不过是胡乱搭个台,也不敢写成博客.今天打开scdn一看,有一个月没有写博客了.搭框架,写布局这些活也不好说,都是些琐碎的活,我也说不好,也就原谅自己,可以总感觉好长时间不写,手心痒痒,就打开郭霖的博客从第一篇看,写个学习笔记吧:

  1. 获取当前内存
 /** 
     * 获取当前可用内存,返回数据以字节为单位。 
     *  
     * @param context 可传入应用程序上下文。 
     * @return 当前可用内存。 
     */  
    private static long getAvailableMemory(Context context) {  
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();  
        getActivityManager(context).getMemoryInfo(mi);  
        return mi.availMem;  
    }  

2.计算已经使用内存百分比:

/** 
     * 计算已使用内存的百分比,并返回。 
     *  
     * @param context 可传入应用程序上下文。 
     * @return 已使用内存的百分比,以字符串形式返回。 
     */  
    public static String getUsedPercentValue(Context context) {  
        String dir = "/proc/meminfo";  
        try {  
            FileReader fr = new FileReader(dir);  
            BufferedReader br = new BufferedReader(fr, 2048);  
            String memoryLine = br.readLine();  
            String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));  
            br.close();  
            long totalMemorySize = Integer.parseInt(subMemoryLine.replaceAll("\\D+", ""));  
            long availableSize = getAvailableMemory(context) / 1024;  
            int percent = (int) ((totalMemorySize - availableSize) / (float) totalMemorySize * 100);  
            return percent + "%";  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return "悬浮窗";  
    }  

3.在桌面创建悬浮窗:


 /** 
     * 创建一个大悬浮窗。位置为屏幕正中间。
     *  其中FloatWindowBigView是自定义的View继承自LinearLayout
     * @param context 
     *            必须为应用程序的Context. 
     */  
    public static void createBigFloatWindow(Context context) {  
        WindowManager windowManager = getWindowManager(context);  
        int screenWidth = windowManager.getDefaultDisplay().getWidth();  
        int screenHeight = windowManager.getDefaultDisplay().getHeight();  
        if (bigWindow == null) {  
            bigWindow = new FloatWindowBigView(context);  
            if (bigWindowParams == null) {  
                bigWindowParams = new LayoutParams();  
                bigWindowParams.x = screenWidth / 2 - FloatWindowBigView.viewWidth / 2;  
                bigWindowParams.y = screenHeight / 2 - FloatWindowBigView.viewHeight / 2;  
                bigWindowParams.type = LayoutParams.TYPE_PHONE;  
                bigWindowParams.format = PixelFormat.RGBA_8888;  
                bigWindowParams.gravity = Gravity.LEFT | Gravity.TOP;  
                bigWindowParams.width = FloatWindowBigView.viewWidth;  
                bigWindowParams.height = FloatWindowBigView.viewHeight;  
            }  
            windowManager.addView(bigWindow, bigWindowParams);  
        }  
    }  

4.自定义View——–一直以来都是在网上各种找,也不留心,今天看郭神第一篇博客,一个简单的自定义view居然突然有所发现,记录一下:
在自定义View的构造方法中使用inflater.inflate()方法就可以将xml布局文件引入到当前视图View中,原理居然和碎片在onCreateView()中一样的原理,为啥现在才留意呢?我是不是太不合格了,呵呵

LayoutInflater.from(context).inflate(R.layout.float_window_big, this);

5.在代码中拿到控件的宽高:

//当然这里拿到的是自定义布局视图的宽高
View view = findViewById(R.id.small_window_layout);  
viewWidth = view.getLayoutParams().width;  
viewHeight = view.getLayoutParams().height; 

6.获取状态栏的高度:

/** 
     * 用于获取状态栏的高度。 
     *  
     * @return 返回状态栏高度的像素值。 
     */  
    private int getStatusBarHeight() {  
        if (statusBarHeight == 0) {  
            try {  
                Class<?> c = Class.forName("com.android.internal.R$dimen");  
                Object o = c.newInstance();  
                Field field = c.getField("status_bar_height");  
                int x = (Integer) field.get(o);  
                statusBarHeight = getResources().getDimensionPixelSize(x);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        return statusBarHeight;  
    }  

7.判断当前界面是否是桌面:

/** 
     * 判断当前界面是否是桌面 
     */  
    private boolean isHome() {  
        ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);  
        List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);  
        return getHomes().contains(rti.get(0).topActivity.getPackageName());  
    }  

    /** 
     * 获得属于桌面的应用的应用包名称 
     *  
     * @return 返回包含所有包名的字符串列表 
     */  
    private List<String> getHomes() {  
        List<String> names = new ArrayList<String>();  
        PackageManager packageManager = this.getPackageManager();  
        Intent intent = new Intent(Intent.ACTION_MAIN);  
        intent.addCategory(Intent.CATEGORY_HOME);  
        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,  
                PackageManager.MATCH_DEFAULT_ONLY);  
        for (ResolveInfo ri : resolveInfo) {  
            names.add(ri.activityInfo.packageName);  
        }  
        return names;  
    }  

8.定时执行异步任务:

//5秒执行一次
timer = new Timer();  
timer.scheduleAtFixedRate(new RefreshTask(), 0, 5000);        

(1)scheduleAtFixedRate方法:下一次执行时间相对于上一次开始的 时间点 ,因此执行时间不会延后,存在并发性;
(2)若用schedule方法:下一次执行时间相对于 上一次 实际执行完成的时间点 ,因此执行时间会不断延后;
博客推荐:http://blog.sina.com.cn/s/blog_6c82728b0100zstc.html

9.坐标点的位置计算

event.getRawX()和event.getX()的区别,getRawX()是获取点击位置相对于整个屏幕原点(左上)的x距离,getX()则是相对于自身的x距离,所以event.getRawX() - event.getX()得到的就是newWorkImageView左上角相对屏幕的x距离。当然了,对于y轴,还要减去状态栏的距离。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值