【Android】よく使う共通処理【Util】
個人的によく使う処理です。
Utilにまとめておくと意外と便利!
/** * クリップボードにコピー */ public static void copy(Context c, String label, String text) { android.content.ClipboardManager cm = (android.content.ClipboardManager)c.getSystemService(Context.CLIPBOARD_SERVICE); cm.setPrimaryClip(ClipData.newPlainText(label, text)); } /** * 画面横サイズ取得 */ public static int getDisplayWidth(Context context) { try { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return metrics.widthPixels; } catch (Exception e) { return 0; } } /** * 画面縦サイズ取得 */ public static int getDisplayHeight(Context context) { try { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return metrics.heightPixels; } catch (Exception e) { return 0; } } /** * dip → px 変換 */ public static int dipToPixel(Context context, int dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); } /** * 色を少し明るくする */ public static int lighter(int color, float factor) { int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255); int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255); int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255); return Color.argb(Color.alpha(color), red, green, blue); } /** * 色を少し暗くする */ public static int darker (int color, float factor) { if (factor < 0 || factor > 1) return color; int r = Color.red(color); int g = Color.green( color ); int b = Color.blue( color ); return Color.argb(Color.alpha(color), Math.max((int)(r * factor), 0), Math.max((int)(g * factor), 0), Math.max((int)(b * factor), 0)); } /** * 現在時刻を取得 */ public static String now() { return now("yyyy-MM-dd HH:mm:ss", TimeZone.getDefault().toString()); } public static String now(String format) { return now(format, TimeZone.getDefault().toString()); } public static String now(String format, String timeZone) { Date date = new Date(); return FastDateFormat.getInstance(format, TimeZone.getTimeZone(timeZone)).format(date); } /** * data/data/パッケージ名/filesの下にフォルダを作成し、作成したフォルダのパスを返却 */ public static String makePkgFilesDir(Context context, String folderPath) { String path = context.getFilesDir().getAbsolutePath() + "/" + folderPath; // ディレクトリがない場合は作成 File makeDir = new File(path); if (makeDir.exists() == false) makeDir.mkdirs(); return path; } /** * ディレクトリ、ファイルの削除 */ public static boolean delete(File f) { if (f.exists() == false) return false; if (f.isFile()) { f.delete(); } else if (f.isDirectory()) { File[] files = f.listFiles(); for (int i = 0; i < files.length; i++) { delete(files[i]); } f.delete(); } return true; } /** * キーボード表示 */ public static void show(Activity activity) { if(null == activity) return; InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); View v = activity.getCurrentFocus(); if(null != v) { imm.showSoftInput(v.findFocus(), 0); } } /** * キーボード非表示 */ public static void hide(Activity activity) { if(null == activity) return; InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); View v = activity.getCurrentFocus(); if(null != v) { imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }