Android 360Float悬浮框项目笔记

借鉴 郭霖大神的专栏博客,地址:
http://blog.csdn.net/guolin_blog/article/details/8689140

1、getX()是表示Widget相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕)
2。 (一)用于获取状态栏的高度。
`

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;
}

`
(二)屏幕高度和宽度获取方法

int screenWidth,screenHeight;    
WindowManager windowManager = getWindowManager();    
Display display = windowManager.getDefaultDisplay();    
screenWidth = display.getWidth();    
screenHeight = display.getHeight();  

(三)获取标题栏的高度

int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  
//statusBarHeight是上面状态栏的高度  
int titleBarHeight = contentTop - statusBarHeight;  

(四)、获取ActionBar高度

int actionBarHeight = getActionBar().getHeight();  

3。计算内存

/**
     * 计算已使用内存的百分比,并返回。
     * 
     * @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 "悬浮窗";
    }




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

android的总内存大小信息存放在系统的/proc/meminfo文件里面,可以通过读取这个文件来获取这些信息

subMemoryLine.replaceAll(“\D+”, “”) 把非数字字符去掉。

关于android系统信息(内存、cpu、sd卡、电量、版本)获取请移至博客http://gqdy365.iteye.com/blog/1066113

/** 
     * 获得属于桌面的应用的应用包名称 
     *  
     * @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;  
    }  

相关的代码,原文链接郭霖大神博客里有,有需要的大家可以去下载。
http://download.csdn.net/detail/sinyu890807/5158470
学习记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值