android快速開発(二)補助類の使用:開発速度の加速


補助クラスの使用:開発の高速化
さぎょう
その名の通り、開発速度を向上させ、絶え間ない開発の中で補助類を最適化することもでき、最後には応用品質を向上させることができる.
単純な補助クラス
        L(Log)
package com.yqy.yqy_abstract.utils;

import android.util.Log;

public class L {

	/**
	 *     ,          false
	 */
	public static boolean isShow = true;
	public static String TAG = "YQY";

	/**
	 * 
	 * @param tag
	 *              tag
	 * @param msg
	 */
	public static void e(String tag, String msg) {
		if (isShow)
			Log.e(tag, msg);
	}

	/**
	 *     tag
	 * 
	 * @param msg
	 */
	public static void e(String msg) {
		if (isShow)
			Log.e(TAG, msg);
	}

	//   ...

}

        T(Toast)
package com.yqy.yqy_abstract.utils;

import android.content.Context;
import android.widget.Toast;

public class T {

	/**
	 *     ,          false
	 */
	public static boolean isShow = true;

	public static void showShort(Context context, String text) {
		if (isShow)
			Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	public static void showLong(Context context, String text) {
		if (isShow)
			Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}

}

SPUtil(sharedpreferencesツールクラス)
package com.yqy.yqy_abstract.utils;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * sharedpreferences   
 * 
 * @author YQY
 * 
 */
public class SPUtil {

	private SharedPreferences sp;
	private static SPUtil instance = null;

	public static SPUtil getInstance() {
		synchronized (SPUtil.class) {
			if (instance == null)
				instance = new SPUtil();
		}
		return instance;
	}

	public void write(String key, String value) {
		Editor editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public void write(String key, int value) {
		Editor editor = sp.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public void write(String key, boolean value) {
		Editor editor = sp.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 *   key
	 * 
	 * @param key
	 */
	public void remove(String key) {
		Editor editor = sp.edit();
		editor.remove(key);
		editor.commit();
	}
	
	public int read(String key, int defaultValue) {
		return sp.getInt(key, defaultValue);
	}

	public boolean read(String key, Boolean defaultValue) {
		return sp.getBoolean(key, defaultValue);
	}
	
	public String read(String key, String defaultValue) {
		return sp.getString(key, defaultValue);
	}
}

TextUtils
package com.yqy.yqy_abstract.utils;

public class TextUtils {
	
	public static boolean isEmpty(String str) {
		if (str == null || str.length() == 0)
			return true;
		return false;
	}

}

FormatTools(いくつかのフォーマットを判断するツールクラス)
package com.yqy.yqy_abstract.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

/**
 *           
 * 
 * @author YQY
 * 
 */
public class FormatTools {

	/**
	 *     
	 */
	public static boolean isName(String name) {
		String str = "^[\u4e00-\u9fa5]{2,5}$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(name);
		return m.matches();
	}
	
	/****
	 *            
	 *     ,        _  ,        2-15
	 * ***/
	public static boolean isNameCus(String name)
	{
		String str = "[\u4e00-\u9fa5_a-zA-Z0-9_]{2,15}" ;
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(name);
		return m.matches();
	}
	
	
	/**
	 *     
	 */
	public static boolean isWebSite(String name) {
		String str = "\\.[a-zA-Z]{3}$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(name);
		return m.matches();
	}
	
	/**
	 *     
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);

		return m.matches();
	}

	//      
	public static boolean IsPhoneNum(String mobiles) {
		if (mobiles.trim().length() == 11) {
			Pattern p = Pattern
					.compile("^((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))\\d{8}$");
			Matcher m = p.matcher(mobiles);
			Log.d("IsPhoneNum", m.matches() + "");
			return m.matches();
		}
		return false;

	}

	//     
	public static boolean IsCallNum(String mobiles) {
		boolean isValid = false;
		CharSequence inputStr = mobiles;
		String expression = "^(0\\d{2,3})(\\d{7,8})(\\d{3,})?$";
		Pattern pattern = Pattern.compile(expression);
		Matcher matcher = pattern.matcher(inputStr);
		if (matcher.matches()) {
			isValid = true;
		}
		return isValid;
	}

	//        
	public static boolean IsAllCallNum(String mobiles) {
		boolean isValid = false;
		String expression = "(^((13[0-9])|(15[^4,\\D])|(18[0,2,5-9]))\\d{8}$)|"
				+ "(^(0\\d{2,3})(\\d{7,8})(\\d{3,})?$)";
		CharSequence inputStr = mobiles;
		Pattern pattern = Pattern.compile(expression);
		Matcher matcher = pattern.matcher(inputStr);
		if (matcher.matches()) {
			isValid = true;
		}
		return isValid;
	}

	//         
	public static boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			// System.out.println(str.charAt(i));
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 *        
	 * 
	 * @param str
	 *                  
	 * 
	 * @return      true,    false
	 */

	public static boolean isInteger(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}

	/**
	 *            6-16               
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isPwd(String str) {
		Pattern pattern = Pattern.compile("^[a-z0-9A-Z]+$");
		return pattern.matcher(str).matches();
	}

	/**
	 *          
	 * 
	 * @param second
	 * @return
	 */
	public static String changeTotime(int second) {
		int h = 0;
		int d = 0;
		int s = 0;
		int temp = second % 3600;
		if (second > 3600) {
			h = second / 3600;
			if (temp != 0) {
				if (temp > 60) {
					d = temp / 60;
					if (temp % 60 != 0) {
						s = temp % 60;
					}
				} else {
					s = temp;
				}
			}
		} else {
			d = second / 60;
			if (second % 60 != 0) {
				s = second % 60;
			}
		}

		// return h + "  " + d + "  " + s + " ";
		return h + "  " + d + "  ";
	}

	/**
	 *            & /
	 * 
	 * @return
	 */
	public static boolean isContent(String content) {
		if (content.contains("&") || content.contains("/")) {
			return true;
		}
		return false;
	}

	/**
	 *       
	 */
	public static String miToGl(int distance) {
		double dis = Math.round(distance / 100d) / 10d;
		return dis + "  ";
	}

	/**
	 *              
	 */
	public static boolean notMood(String str_mood) {
		boolean isValid = false;
		CharSequence inputStr = str_mood;
		String expression = "^([a-z]|[A-Z]|[0-9]|[\u2E80-\u9FFF]){3,}|@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?|[wap.]{4}|[www.]{4}|[blog.]{5}|[bbs.]{4}|[.com]{4}|[.cn]{3}|[.net]{4}|[.org]{4}|[http://]{7}|[ftp://]{6}$|(^[A-Za-z\\d\\u4E00-\\u9FA5\\p{P}‘’“”]+$)|(^[0-9a-zA-Z\u4E00-\u9FA5]+)";
		Pattern pattern = Pattern.compile(expression);
		Matcher matcher = pattern.matcher(inputStr);
		if (matcher.matches()) {
			isValid = true;
		}
		return isValid;
	}
}

ScreenUtils(スクリーンツールクラス)
package com.yqy.yqy_abstract.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

/**
 *       
 * 
 * @author YQY
 * 
 */
public class ScreenUtils {

	/**
	 *       
	 * 
	 */
	public static int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		int width = outMetrics.widthPixels;
		return width;
	}

	/**
	 *       
	 */
	public static int getScreenHeight(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		int height = outMetrics.heightPixels;
		return height;
	}

	/**
	 *       
	 */
	public static float getScreenDensity(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		float density = outMetrics.density;
		return density;
	}

	/**
	 *         
	 */
	public static int getStatusHeight(Context context) {

		int statusHeight = -1;
		try {
			Class<?> clazz = Class.forName("com.android.internal.R$dimen");
			Object object = clazz.newInstance();
			int height = Integer.parseInt(clazz.getField("status_bar_height")
					.get(object).toString());
			statusHeight = context.getResources().getDimensionPixelSize(height);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return statusHeight;
	}

	/**
	 *         ,     
	 */
	public static Bitmap snapShotWithStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 *         ,      
	 */
	public static Bitmap snapShotWithoutStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;

		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
				- statusBarHeight);
		view.destroyDrawingCache();
		return bp;
	}

}