Writing in the front
In this era of little fresh meat rampant, the world of programmers is not too calm, you will find more and more than you are younger than you are also better than your children, please allow me to “kindly” call them children. But, what can you do, no way, continue to work hard! So, in the evening, very anxious, after coming back from the company, decided to share this article.
Get into the business
In fact, I really want to share some common and useful tools for everyday Android development.
-
As a programmer, naming is always important, not only to help you maintain your code, but also to help your team work together. Good development starts with naming.
Here’s a website, Codelf, that converts Chinese to English and has a hump name.
As shown in the figure, input variable name, the associated English words will automatically appear, and the results can be filtered according to language.
When you run out of words, use this tool to automatically generate, really cool!!
2. In Android development, it is very important to collect some common tool classes. And as Android technology matures, to learn to stand on the shoulders of giants to develop, some of the framework and tools developed by the big cattle still need to use.
- The MD5 algorithm:
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(); / / get the MD5 digest algorithms MessageDigest object MessageDigest mdInst = MessageDigest. GetInstance ("MD5"); // Update the digest mdinst.update (btInput) with the specified byte; Byte [] md = mdinst.digest (); byte[] md = mdinst.digest (); // Convert the ciphertext to a hexadecimal string form int j = md.length; char str[] = new char[j * 2]; int k = 0; // Convert the byte to the corresponding stringfor (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();
returnnull; }}Copy the code
- Get the width, height and density of the device screen:
Public static int getScreenWidth(Context Context) {public static int getScreenWidth(Context Context) {returncontext.getResources().getDisplayMetrics().widthPixels; } public static int getScreenHeight(Context Context) {public static int getScreenHeight(Context Context) {returncontext.getResources().getDisplayMetrics().heightPixels; } /** * get device density */ public staticfloat getScreenDensity(Context context) {
returncontext.getResources().getDisplayMetrics().density; }}Copy the code
- Dp, SP to px tool class:
Public class DisplayUtil {/** * public static int px2dip(Context);float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return(int) (pxValue/scale + 0.5f); } /** * Convert dip or DP values to PX values, keeping the size unchanged * @param dipValue * @param scale * (attribute density in DisplayMetrics) * @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return(int) (dipValue * scale + 0.5f); } /** * convert the px value to the sp value, keeping the text size unchanged ** @param pxValue * @param fontScale * (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); }}Copy the code
- Drawable to convert bitmap utility class:
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;
}Copy the code
- Verification code countdown tool class:
public class TimeCount extends CountDownTimer { private Button button; @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 voidonFinish() {button.settext () {button.settext ();"Obtain captcha code");
button.setClickable(true); button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius14)); } @override public [^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^] {false); // prevent button. SetText (zipuntilfinished / 1000 +)"Try again after S"); button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius_gray)); }}Copy the code
The last
It is a little late indeed, old age, the body a little bit can not carry, today first share this, next week continue. Good things or to continue to share, will benefit more people, you have me have him.