List of usage examples for android.graphics Bitmap getDensity
public int getDensity()
Returns the density for this bitmap.
The default density is the same density as the current display, unless the current application does not support different screen densities in which case it is android.util.DisplayMetrics#DENSITY_DEFAULT .
From source file:Main.java
public static Drawable bitmap2Drawable(Bitmap bm) { if (bm == null) { return null; }// w w w .j a v a 2s. c o m BitmapDrawable bd = new BitmapDrawable(bm); bd.setTargetDensity(bm.getDensity()); return new BitmapDrawable(bm); }
From source file:Main.java
public static void blur(Context context, Bitmap bkg, View view, int width, int height) { float radius = 20; Log.i("hulixia", bkg.getWidth() + "w,h" + bkg.getDensity()); Bitmap overlay = Bitmap.createBitmap(bkg.getWidth(), bkg.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(overlay); canvas.translate(-view.getLeft(), -view.getTop()); canvas.drawBitmap(bkg, 0, 0, null);/*w w w . j a va2 s . co m*/ RenderScript rs = RenderScript.create(context); Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay); ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement()); blur.setInput(overlayAlloc); blur.setRadius(radius); blur.forEach(overlayAlloc); overlayAlloc.copyTo(overlay); view.setBackground(new BitmapDrawable(context.getResources(), overlay)); rs.destroy(); }
From source file:Main.java
@SuppressWarnings("deprecation") private static Drawable bitmap2Drawable(Bitmap bm) { if (bm == null) { return null; }//from w w w . j ava2 s . com BitmapDrawable bd = new BitmapDrawable(bm); bd.setTargetDensity(bm.getDensity()); return new BitmapDrawable(bm); }
From source file:Main.java
@SuppressWarnings("deprecation") public static Drawable bitmap2Drawable(Bitmap bm) { if (bm == null) { return null; }/* w w w. ja v a 2 s .c o m*/ BitmapDrawable bd = new BitmapDrawable(bm); bd.setTargetDensity(bm.getDensity()); return new BitmapDrawable(bm); }
From source file:com.linkbubble.util.Util.java
/** * Returns a bitmap suitable for the all apps view. *///from w ww . j a v a 2s .co m static Bitmap createIconBitmap(Drawable icon, Context context) { synchronized (sCanvas) { // we share the statics :-( if (sIconWidth == -1) { initStatics(context); } int width = sIconWidth; int height = sIconHeight; if (icon instanceof PaintDrawable) { PaintDrawable painter = (PaintDrawable) icon; painter.setIntrinsicWidth(width); painter.setIntrinsicHeight(height); } else if (icon instanceof BitmapDrawable) { // Ensure the bitmap has a density. BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; Bitmap bitmap = bitmapDrawable.getBitmap(); if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); } } int sourceWidth = icon.getIntrinsicWidth(); int sourceHeight = icon.getIntrinsicHeight(); if (sourceWidth > 0 && sourceHeight > 0) { // There are intrinsic sizes. if (width < sourceWidth || height < sourceHeight) { // It's too big, scale it down. final float ratio = (float) sourceWidth / sourceHeight; if (sourceWidth > sourceHeight) { height = (int) (width / ratio); } else if (sourceHeight > sourceWidth) { width = (int) (height * ratio); } } else if (sourceWidth < width && sourceHeight < height) { // Don't scale up the icon width = sourceWidth; height = sourceHeight; } } // no intrinsic size --> use default size int textureWidth = sIconTextureWidth; int textureHeight = sIconTextureHeight; final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888); final Canvas canvas = sCanvas; canvas.setBitmap(bitmap); final int left = (textureWidth - width) / 2; final int top = (textureHeight - height) / 2; @SuppressWarnings("all") // suppress dead code warning final boolean debug = false; if (debug) { // draw a big box for the icon for debugging canvas.drawColor(sColors[sColorIndex]); if (++sColorIndex >= sColors.length) sColorIndex = 0; Paint debugPaint = new Paint(); debugPaint.setColor(0xffcccc00); canvas.drawRect(left, top, left + width, top + height, debugPaint); } sOldBounds.set(icon.getBounds()); icon.setBounds(left, top, left + width, top + height); icon.draw(canvas); icon.setBounds(sOldBounds); canvas.setBitmap(null); return bitmap; } }
From source file:Main.java
/** * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix. *//*from ww w. j ava 2s .co m*/ private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m) { // Re-implement Bitmap createBitmap() to always return a mutable bitmap. Canvas canvas = new Canvas(); Bitmap bitmap; Paint paint; if ((m == null) || m.isIdentity()) { bitmap = Bitmap.createBitmap(width, height, source.getConfig()); paint = null; } else { RectF rect = new RectF(0, 0, width, height); m.mapRect(rect); bitmap = Bitmap.createBitmap(Math.round(rect.width()), Math.round(rect.height()), source.getConfig()); canvas.translate(-rect.left, -rect.top); canvas.concat(m); paint = new Paint(Paint.FILTER_BITMAP_FLAG); if (!m.rectStaysRect()) { paint.setAntiAlias(true); } } bitmap.setDensity(source.getDensity()); canvas.setBitmap(bitmap); Rect srcBounds = new Rect(x, y, x + width, y + height); RectF dstBounds = new RectF(0, 0, width, height); canvas.drawBitmap(source, srcBounds, dstBounds, paint); return bitmap; }
From source file:com.android.launcher3.Utilities.java
/** * @param scale the scale to apply before drawing {@param icon} on the canvas */// w w w .j a v a2 s . c o m public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) { synchronized (sCanvas) { final int iconBitmapSize = getIconBitmapSize(); int width = iconBitmapSize; int height = iconBitmapSize; if (icon instanceof PaintDrawable) { PaintDrawable painter = (PaintDrawable) icon; painter.setIntrinsicWidth(width); painter.setIntrinsicHeight(height); } else if (icon instanceof BitmapDrawable) { // Ensure the bitmap has a density. BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; Bitmap bitmap = bitmapDrawable.getBitmap(); if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) { bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); } } int sourceWidth = icon.getIntrinsicWidth(); int sourceHeight = icon.getIntrinsicHeight(); if (sourceWidth > 0 && sourceHeight > 0) { // Scale the icon proportionally to the icon dimensions final float ratio = (float) sourceWidth / sourceHeight; if (sourceWidth > sourceHeight) { height = (int) (width / ratio); } else if (sourceHeight > sourceWidth) { width = (int) (height * ratio); } } // no intrinsic size --> use default size int textureWidth = iconBitmapSize; int textureHeight = iconBitmapSize; final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888); final Canvas canvas = sCanvas; canvas.setBitmap(bitmap); final int left = (textureWidth - width) / 2; final int top = (textureHeight - height) / 2; @SuppressWarnings("all") // suppress dead code warning final boolean debug = false; if (debug) { // draw a big box for the icon for debugging canvas.drawColor(sColors[sColorIndex]); if (++sColorIndex >= sColors.length) sColorIndex = 0; Paint debugPaint = new Paint(); debugPaint.setColor(0xffcccc00); canvas.drawRect(left, top, left + width, top + height, debugPaint); } sOldBounds.set(icon.getBounds()); icon.setBounds(left, top, left + width, top + height); canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2); icon.draw(canvas); canvas.restore(); icon.setBounds(sOldBounds); canvas.setBitmap(null); return bitmap; } }
From source file:org.appcelerator.titanium.view.TiDrawableReference.java
/** * Gets the bitmap, scaled to a width & height specified in TiDimension params. * @param destWidthDimension (null-ok) TiDimension specifying the desired width. If .isUnitAuto() * then the width will be the source width. If destWidthDimension is null, then the TiContext * will be used to determine the activity window's decor width and use that. * @param destHeightDimension (null-ok) TiDimension specifying the desired height. If .isUnitAuto() * then the height will be the source height. If destHeightDimension is null, then resulting height will * be at same ratio to the resulting width as the original height:width. * @return Bitmap, or null if any problem getting it. Check logcat if null. */// w w w . j a v a2s. c om public Bitmap getBitmap(View parent, TiDimension destWidthDimension, TiDimension destHeightDimension) { int srcWidth, srcHeight, destWidth, destHeight; Bounds bounds = peekBounds(); srcWidth = bounds.width; srcHeight = bounds.height; if (srcWidth <= 0 || srcHeight <= 0) { Log.w(TAG, "Bitmap bounds could not be determined. If bitmap is loaded, it won't be scaled."); return getBitmap(); // fallback } if (parent == null) { Activity activity = softActivity.get(); if (activity != null && activity.getWindow() != null) { parent = activity.getWindow().getDecorView(); } } Bounds destBounds = calcDestSize(srcWidth, srcHeight, destWidthDimension, destHeightDimension, parent); destWidth = destBounds.width; destHeight = destBounds.height; // If src and dest width/height are same, no need to go through all the sampling and scaling jazz. if (srcWidth == destWidth && srcHeight == destHeight) { return getBitmap(); } if (destWidth <= 0 || destHeight <= 0) { // calcDestSize() should actually prevent this from happening, but just in case... Log.w(TAG, "Bitmap final bounds could not be determined. If bitmap is loaded, it won't be scaled."); return getBitmap(); } InputStream is = getInputStream(); if (is == null) { Log.w(TAG, "Could not open stream to get bitmap"); return null; } Bitmap b = null; try { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inInputShareable = true; opts.inPurgeable = true; opts.inSampleSize = calcSampleSize(srcWidth, srcHeight, destWidth, destHeight); if (Log.isDebugModeEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("Bitmap calcSampleSize results: inSampleSize="); sb.append(opts.inSampleSize); sb.append("; srcWidth="); sb.append(srcWidth); sb.append("; srcHeight="); sb.append(srcHeight); sb.append("; finalWidth="); sb.append(opts.outWidth); sb.append("; finalHeight="); sb.append(opts.outHeight); Log.d(TAG, sb.toString()); } Bitmap bTemp = null; try { oomOccurred = false; bTemp = BitmapFactory.decodeStream(is, null, opts); if (bTemp == null) { Log.w(TAG, "Decoded bitmap is null"); return null; } if (Log.isDebugModeEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("decodeStream resulting bitmap: .getWidth()=" + bTemp.getWidth()); sb.append("; .getHeight()=" + bTemp.getHeight()); sb.append("; getDensity()=" + bTemp.getDensity()); Log.d(TAG, sb.toString()); } // Set the bitmap density to match the view density before scaling, so that scaling // algorithm takes destination density into account. DisplayMetrics displayMetrics = new DisplayMetrics(); displayMetrics.setToDefaults(); bTemp.setDensity(displayMetrics.densityDpi); // Orient the image when orientation is set. if (autoRotate) { // Only set the orientation if it is uninitialized if (orientation < 0) { orientation = getOrientation(); } if (orientation > 0) { return getRotatedBitmap(bTemp, orientation); } } if (bTemp.getNinePatchChunk() != null) { // Don't scale nine-patches b = bTemp; bTemp = null; } else { Log.d(TAG, "Scaling bitmap to " + destWidth + "x" + destHeight, Log.DEBUG_MODE); // If anyDensity=false, meaning Android is automatically scaling // pixel dimensions, need to do that here as well, because Bitmap width/height // calculations do _not_ do that automatically. if (anyDensityFalse && displayMetrics.density != 1f) { destWidth = (int) (destWidth * displayMetrics.density + 0.5f); // 0.5 is to force round up of dimension. Casting to int drops decimals. destHeight = (int) (destHeight * displayMetrics.density + 0.5f); } // Created a scaled copy of the bitmap. Note we will get // back the same bitmap if no scaling is required. b = Bitmap.createScaledBitmap(bTemp, destWidth, destHeight, true); } } catch (OutOfMemoryError e) { oomOccurred = true; Log.e(TAG, "Unable to load bitmap. Not enough memory: " + e.getMessage(), e); } finally { // Recycle the temporary bitmap only if it isn't // the same instance as our scaled bitmap. if (bTemp != null && bTemp != b) { bTemp.recycle(); bTemp = null; } } } finally { try { is.close(); } catch (IOException e) { Log.e(TAG, "Problem closing stream: " + e.getMessage(), e); } } if (Log.isDebugModeEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("Details of returned bitmap: .getWidth()=" + b.getWidth()); sb.append("; getHeight()=" + b.getHeight()); sb.append("; getDensity()=" + b.getDensity()); Log.d(TAG, sb.toString()); } return b; }