protected Options getOptions(Context context) { Options options = new Options(); options.inDensity = 330; options.inTargetDensity = (int) (context.getResources().getDisplayMetrics().density * ((float) options.inDensity)); return options; }
/** * @param data 数据或路径 * @param width 目标宽 * @param height 目标高 * @return 图片 */ private Bitmap createBitmap(Object data, int width, int height) { Options options = new Options(); int scale = 1; if (width > 0 && height > 0) { // 创建目标大小的图片 options.inJustDecodeBounds = true; if (data instanceof String) { BitmapFactory.decodeFile((String) data, options); } else { BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options); } int dw = options.outWidth / width; int dh = options.outHeight / height; scale = Math.max(dw, dh); options = new Options(); } options.inDensity = DeviceInfo.getInstance().getDencity(); options.inScaled = true; options.inPurgeable = true; options.inSampleSize = scale; Bitmap bitmap = null; if (data instanceof String) { bitmap = BitmapFactory.decodeFile((String) data, options); } else { bitmap = BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options); } return bitmap; }
private void copyOptions(Options srcOptions, Options destOptions) { destOptions.inDensity = srcOptions.inDensity; destOptions.inDither = srcOptions.inDither; destOptions.inInputShareable = srcOptions.inInputShareable; destOptions.inJustDecodeBounds = srcOptions.inJustDecodeBounds; destOptions.inPreferredConfig = srcOptions.inPreferredConfig; destOptions.inPurgeable = srcOptions.inPurgeable; destOptions.inSampleSize = srcOptions.inSampleSize; destOptions.inScaled = srcOptions.inScaled; destOptions.inScreenDensity = srcOptions.inScreenDensity; destOptions.inTargetDensity = srcOptions.inTargetDensity; destOptions.inTempStorage = srcOptions.inTempStorage; if (Build.VERSION.SDK_INT >= 10) copyOptions10(srcOptions, destOptions); if (Build.VERSION.SDK_INT >= 11) copyOptions11(srcOptions, destOptions); }