Java tutorial
/**************************************************************************** * Copyright (C) 2014 www.apkdv.com * Author: LengYue ProjectName: DvTools * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *****************************************************************************/ package com.dv.Utils; //~--- non-JDK imports -------------------------------------------------------- import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.PixelFormat; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.Log; import android.view.View; import org.json.JSONException; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; //~--- JDK imports ------------------------------------------------------------ /** * @ClassInfo * @Author LengYue * @CreateDate 2014823-?2:20:15 * @ProjectName AiGuang * @PackeName com.AiGuang.Utils.MyTools * @ClassName Tools */ public class Tools { // public static boolean showError = true; public static String TAG = "Myshop"; /** * ? * * @param string * @return */ public static boolean NotNull(String string) { if ((string != null) && !string.isEmpty() && (string.length() != 0)) { return true; } else { return false; } } /* * List? */ /** * NotNull description * Description: * * @param list * @return */ public static boolean NotNull(ArrayList<?> list) { if ((list != null) && !list.isEmpty()) { return true; } else { return false; } } /* * List? */ /** * NotNull description * Description: * * @param list * @return */ public static boolean NotNull(List<?> list) { if ((list != null) && !list.isEmpty()) { return true; } else { return false; } } /** * NotNull description * Description: * * @param arry * @param <T> * @return */ public static <T> boolean NotNull(T[] arry) { if ((arry != null) && (arry.length != 0)) { return true; } else { return false; } } /* * ??JsonArray,? */ /** * jsonNotNull description * Description: * * @param object * @param name * @return * @throws org.json.JSONException */ public static boolean jsonNotNull(JSONObject object, String name) throws JSONException { if (!object.has(name) || (object.getJSONArray(name) == null) || (object.getJSONArray(name).length() == 0)) { return false; } else { return true; } } /* * ??,? */ /** * NotNull description * Description: * * @param object * @param name * @return * @throws org.json.JSONException */ public static boolean NotNull(JSONObject object, String name) throws JSONException { if (!object.has(name) || !NotNull(object.getString(name))) { return false; } else { return true; } } /** * @param str * @return * @MethodsInfo?""? * @Author LengYue * @CreateDate 201491-?2:03:01 * @Return String */ public static String parseEmpty(String str) { if ((str == null) || "null".equals(str.trim())) { str = ""; } return str.trim(); } /** * ??:Int? * * @param str, int * @return * @Autho LengYue */ public static String parseEmpty(int str) { return str + ""; } /** * @param str * @return * @MethodsInfo:?? * @Author LengYue * @CreateDate 201492-?1:25:50 * @Return boolean */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } /** * ? * * @param e */ public static void error(Exception e) { if (showError) { ; Log.e(TAG + Thread.currentThread().getStackTrace()[1].getClass().getSimpleName(), e.toString()); } } /** * error description * Description: * * @param e */ public static void error(String e) { if (showError) { Log.e(TAG + Thread.currentThread().getStackTrace()[1].getClass().getSimpleName(), e); } } /** * log description * Description: * * @param string */ public static void log(String string) { if (showError) { Log.i(TAG + Thread.currentThread().getStackTrace()[0].getClass().getSimpleName(), string); } } /** * log description * Description: * * @param string */ public static void log(int string) { if (showError) { Log.i(TAG + Thread.currentThread().getStackTrace()[1].getClass().getSimpleName(), string + ""); } } /** * log description * Description: * * @param string */ public static void log(long string) { if (showError) { Log.i(TAG + Thread.currentThread().getStackTrace()[1].getClass().getSimpleName(), string + ""); } } /** * log description * Description: * * @param TAG * @param value */ public static void log(String TAG, String value) { if (showError) { Log.i(TAG + Thread.currentThread().getStackTrace()[1].getClass().getSimpleName(), value); } } /** * view?bitmap * * @param view * @return */ public static Bitmap convertViewToBitmap(View view) { view.destroyDrawingCache(); view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); return bitmap; } /** * ? dp ?? ? px(?) * * @param context * @param dpValue * @return */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * ? px(?) ?? ? dp * * @param context * @param pxValue * @return */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * byte[] * * @param bitmap * @return */ public static byte[] bitmap2byte(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); return baos.toByteArray(); } /** * install app * * @param context * @param filePath * @return whether apk exist */ public static boolean install(Context context, String filePath) { Intent i = new Intent(Intent.ACTION_VIEW); File file = new File(filePath); if ((file != null) && (file.length() > 0) && file.exists() && file.isFile()) { i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive"); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); return true; } return false; } /** * BitMap * * @param drawable * @return */ public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), (drawable.getOpacity() != PixelFormat.OPAQUE) ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); // canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } // /** * getTypeFace description * Description: * * @param context * @return */ public static Typeface getTypeFace(Context context) { Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/STHeiti-Light.ttc"); return typeFace; } }