一個老程序猿的焦慮

寫在前面


在這個小鮮肉橫行的年代,程序員的世界也不太平靜,你會發現愈來愈多比你小還比你優秀的孩子,請容許我「親切」地稱呼他們爲孩子。但是,你能怎麼辦呢,沒辦法,繼續努力唄!故,大晚上的,甚是焦慮,從公司回來後,決定分享這篇文章。git

進入正題


其實呢,我是真心想分享Android平常開發中一些經常使用和有用的工具。程序員

  1. 做爲程序猿,命名一直都很重要,這不只有助於你維護代碼,並且更有利於團隊的協做。好的開發,從命名開始。github

    下面推薦一個網站codelf,將中文轉爲英文,並且是駝峯式的命名。算法

如圖,輸入變量名稱,會自動出現相關聯的英文單詞,還能夠根據語言篩選結果。canvas

當你詞窮時,使用此工具來自動生成,真的特別爽!!!bash

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);
    }    
    @Override
    public void onFinish() {// 計時完畢
        button.setText("獲取驗證碼");
        button.setClickable(true);
        button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius14));
    }   
     @Override
    public void onTick(long millisUntilFinished) {// 計時過程
        button.setClickable(false);//防止重複點擊
        button.setText(millisUntilFinished / 1000 + "s後重試");
        button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius_gray));
    }
}複製代碼

最後

確實是有點晚了,年紀大了,身體有點扛不住,今天先分享到這,下週繼續。
好東西仍是要繼續分享,纔會造福更多的人,有你有我有他。ide

相關文章
相關標籤/搜索