Skip to content

Instantly share code, notes, and snippets.

@brandhill
Last active February 1, 2016 10:19
Show Gist options
  • Select an option

  • Save brandhill/56771d4bbd680ecf7833 to your computer and use it in GitHub Desktop.

Select an option

Save brandhill/56771d4bbd680ecf7833 to your computer and use it in GitHub Desktop.
常用的 utility functions
// 判斷 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