健康宝——呼吸频率部分开发(六)

工具类


在手机中创建录音文件的类


public class FileUtil {

private static final String TAG = "FileUtil";

public static final String LOCAL = "Brealthe";

public static final String LOCAL_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator;

/**
 * 录音文件目录
 */
public static final String REC_PATH = LOCAL_PATH + LOCAL + File.separator;

/**
 * 自动在SD卡创建相关的目录
 */
static {
    File dirRootFile = new File(LOCAL_PATH);
    if (!dirRootFile.exists()) {
        dirRootFile.mkdirs();
    }
    File recFile = new File(REC_PATH);
    if (!recFile.exists()) {
        recFile.mkdirs();
    }
}

private FileUtil() {
}

/**
 * 判断是否存在存储空间    *
 *
 * @return
 */
public static boolean isExitSDCard() {
    return Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED);
}

private static boolean hasFile(String fileName) {
    File f = createFile(fileName);
    return null != f && f.exists();
}

public static File createFile(String fileName) {

    File myCaptureFile = new File(REC_PATH + fileName);
    if (myCaptureFile.exists()) {
        myCaptureFile.delete();
    }
    try {
        myCaptureFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return myCaptureFile;
}

}

判断屏幕大小的类


public class ScreenUtil {
private ScreenUtil() {
}

/**
 * 屏幕密度
 *
 * @param context
 * @return
 */
public static float getDensity(Context context) {
    if (context instanceof Activity) {
        context = context.getApplicationContext();
    }
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.density;
}
}

设置分贝数与获取分贝数的类


public class World {

public static float dbCount = 40;

private static float lastDbCount = dbCount;
private static float min = 0.5f;  //设置声音最低变化
private static float value = 0;   // 声音分贝值

public static void setDbCount(float dbValue) {
    if (dbValue > lastDbCount) {
        value = dbValue - lastDbCount > min ? dbValue - lastDbCount : min;
    } else {
        value = dbValue - lastDbCount < -min ? dbValue - lastDbCount : -min;
    }
    dbCount = lastDbCount + value * 0.2f; //防止声音变化太快
    lastDbCount = dbCount;
}
}

你可能感兴趣的:(健康宝——呼吸频率部分开发(六))