Last active
February 1, 2016 10:19
-
-
Save brandhill/56771d4bbd680ecf7833 to your computer and use it in GitHub Desktop.
常用的 utility functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 判斷 app 是否有安裝 | |
| private static boolean isPkgInstalled(Context context, String pkgName) { | |
| PackageInfo packageInfo = null; | |
| try { | |
| packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0); | |
| } catch (PackageManager.NameNotFoundException e) { | |
| packageInfo = null; | |
| e.printStackTrace(); | |
| } | |
| if (packageInfo == null) { | |
| return false; | |
| } else { | |
| return true; | |
| } | |
| } | |
| // drawable to bitmap | |
| public static Bitmap drawableToBitmap(Drawable drawable) { | |
| // 取 drawable 的长宽 | |
| int w = drawable.getIntrinsicWidth(); | |
| int h = drawable.getIntrinsicHeight(); | |
| // 取 drawable 的颜色格式 | |
| Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 | |
| : Bitmap.Config.RGB_565; | |
| // 建立对应 bitmap | |
| Bitmap bitmap = Bitmap.createBitmap(w, h, config); | |
| // 建立对应 bitmap 的画布 | |
| Canvas canvas = new Canvas(bitmap); | |
| drawable.setBounds(0, 0, w, h); | |
| // 把 drawable 内容画到画布中 | |
| drawable.draw(canvas); | |
| return bitmap; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment