一个老程序猿的焦虑
寫在前面
在這個小鮮肉橫行的年代,程序員的世界也不太平靜,你會發現越來越多比你小還比你優秀的孩子,請允許我“親切”地稱呼他們為孩子??墒?#xff0c;你能怎么辦呢,沒辦法,繼續努力唄!故,大晚上的,甚是焦慮,從公司回來后,決定分享這篇文章。
進入正題
其實呢,我是真心想分享Android日常開發中一些常用和有用的工具。
作為程序猿,命名一直都很重要,這不僅有助于你維護代碼,而且更有利于團隊的協作。好的開發,從命名開始。
下面推薦一個網站codelf,將中文轉為英文,而且是駝峰式的命名。
如圖,輸入變量名稱,會自動出現相關聯的英文單詞,還可以根據語言篩選結果。
當你詞窮時,使用此工具來自動生成,真的特別爽!!!
2.Android開發中,收集一些常用的工具類是非常之重要的。而且隨著Android技術的成熟,要學會站在巨人的肩膀上開發,大牛們開發的一些框架和工具類還是要用起來的。
- MD5算法:
public final static String MD5(String s) { char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; try { byte[] btInput = s.getBytes(); // 獲得MD5摘要算法的 MessageDigest 對象MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字節更新摘要mdInst.update(btInput); // 獲得密文byte[] md = mdInst.digest(); // 把密文轉換成十六進制的字符串形式int j = md.length; char str[] = new char[j * 2]; int k = 0; //把字節轉換成對應的字符串for (int i = 0; i < j; i++) { byte byte0 = md[i];str[k++] = hexDigits[byte0 >>> 4 & 0xf];str[k++] = hexDigits[byte0 & 0xf];} return new String(str);} catch (Exception e) {e.printStackTrace(); return null;} }復制代碼
- 獲取設備屏幕的寬度高度密度:
public class DisplayUtil { /*** 得到設備屏幕的寬度*/ public static int getScreenWidth(Context context) { return context.getResources().getDisplayMetrics().widthPixels; } /*** 得到設備屏幕的高度*/ public static int getScreenHeight(Context context) { return context.getResources().getDisplayMetrics().heightPixels; } /*** 得到設備的密度*/ public static float getScreenDensity(Context context) { return context.getResources().getDisplayMetrics().density; } }復制代碼
- dp、sp 轉換為 px 的工具類:
public class DisplayUtil { /*** 將px值轉換為dip或dp值,保證尺寸大小不變*/ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density;return (int) (pxValue / scale + 0.5f); } /*** 將dip或dp值轉換為px值,保證尺寸大小不變* @param dipValue* @param scale* (DisplayMetrics類中屬性density)* @return*/ public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /*** 將px值轉換為sp值,保證文字大小不變* * @param pxValue* @param fontScale* (DisplayMetrics類中屬性scaledDensity)* @return*/ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /*** 將sp值轉換為px值,保證文字大小不變* * @param spValue* @param fontScale* (DisplayMetrics類中屬性scaledDensity)* @return*/ public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } }復制代碼
- drawable轉bitmap的工具類:
private Bitmap drawableToBitamp(Drawable drawable) { if (null == drawable) { return null;}if (drawable instanceof BitmapDrawable) {BitmapDrawable bd = (BitmapDrawable) drawable; return bd.getBitmap();} int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight();Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, w, h);drawable.draw(canvas); return bitmap; }復制代碼
- 驗證碼倒計時工具類:
public class TimeCount extends CountDownTimer { private Button button; /*** 到計時* @param millisInFuture 到計時多久,毫秒* @param countDownInterval 周期* @param button 按鈕*/public TimeCount(long millisInFuture, long countDownInterval,Button button) { super(millisInFuture, countDownInterval); this.button =button;} public TimeCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval);} @Overridepublic void onFinish() {// 計時完畢button.setText("獲取驗證碼");button.setClickable(true);button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius14));} @Overridepublic void onTick(long millisUntilFinished) {// 計時過程button.setClickable(false);//防止重復點擊button.setText(millisUntilFinished / 1000 + "s后重試");button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius_gray));} }復制代碼
最后
確實是有點晚了,年紀大了,身體有點扛不住,今天先分享到這,下周繼續。
好東西還是要繼續分享,才會造福更多的人,有你有我有他。
總結
- 上一篇: java 使用webmagic 爬虫框架
- 下一篇: JSP标签和JSTL标签注意点