一个老程序猿的焦虑

写在前面


在这个小鲜肉横行的年代,程序员的世界也不太平静,你会发现愈来愈多比你小还比你优秀的孩子,请容许我“亲切”地称呼他们为孩子。但是,你能怎么办呢,没办法,继续努力呗!故,大晚上的,甚是焦虑,从公司回来后,决定分享这篇文章。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

相关文章
相关标签/搜索