List of usage examples for android.graphics.drawable Drawable getIntrinsicWidth
public int getIntrinsicWidth()
From source file:Main.java
public static Bitmap createFromDrawable(Drawable drawable) { if (drawable == null) return null; if (drawable instanceof BitmapDrawable) return ((BitmapDrawable) drawable).getBitmap(); int width = drawable.getIntrinsicWidth(); if (width == -1) // e.g. for ColorDrawable. width = 1;// w ww . jav a 2s . c o m int height = drawable.getIntrinsicHeight(); if (height == -1) height = 1; // NOTE: Although the following code is a bit expensive, it will not be necessary // for most drawables, since they are normally bitmap resources. Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
From source file:Main.java
/** * Return the {@link Bitmap} representing the {@link Drawable}. * @param drawable Object to convert to {@link Bitmap}. * @return {@link Bitmap} representing the {@link Drawable}. *//*from ww w . j av a2 s . co m*/ public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap retVal; if (drawable instanceof BitmapDrawable) { //Easy retVal = ((BitmapDrawable) drawable).getBitmap(); } else { retVal = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); synchronized (canvas) { canvas.setBitmap(retVal); drawable.draw(canvas); } } return retVal; }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatchDrawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas);/*from w ww .j a v a 2 s . co m*/ return bitmap; } else { throw new IllegalArgumentException("can not support this drawable to bitmap now!!!"); } }
From source file:Main.java
public static Bitmap recolorBitmap(Drawable drawable, int color) { if (drawable == null) { return null; }/* ww w. ja v a 2 s. c om*/ int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); if (width <= 0 || height <= 0) { return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); } Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(outBitmap); drawable.setBounds(0, 0, outBitmap.getWidth(), outBitmap.getHeight()); drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); drawable.draw(canvas); drawable.setColorFilter(null); drawable.setCallback(null); // free up any references return outBitmap; }
From source file:Main.java
public static Bitmap drawable2Bitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof NinePatchDrawable) { 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); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas);/*from w w w . ja v a 2 s .c om*/ return bitmap; } else { return null; } }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable); return bitmapDrawable.getBitmap(); } else {/*from w w w . j a va 2 s . c om*/ 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); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } }
From source file:Main.java
public static Bitmap getBitmap(Drawable drawable, boolean scaleBitmap, int width, int height) { Bitmap bitmap;//from w w w .j a va 2 s . c om if (drawable instanceof BitmapDrawable) { bitmap = ((BitmapDrawable) drawable).getBitmap(); } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); } if (scaleBitmap) { bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); } return bitmap; }
From source file:Main.java
public static void drawLeftAlignItem(Canvas canvas, Drawable drawable, View child, ViewGroup.MarginLayoutParams params) { final int top = child.getTop() - params.topMargin; final int bottom = child.getBottom() + params.bottomMargin; final int left = child.getLeft() - params.leftMargin - drawable.getIntrinsicWidth(); final int right = left + drawable.getIntrinsicWidth(); drawable.setBounds(left, top, right, bottom); drawable.draw(canvas);//from ww w .j av a2 s . c om }
From source file:Main.java
@SuppressWarnings("deprecation") public static int PutImageTargetHeight(Canvas canvas, Drawable image, double Angle, int x, int y, int newHeight) { float scale = (float) newHeight / (float) image.getIntrinsicHeight(); float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale); Bitmap bmp = ((BitmapDrawable) image).getBitmap(); int width = bmp.getWidth(); int height = bmp.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // createa matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // rotate the Bitmap matrix.postRotate((float) Angle); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true); // make a Drawable from Bitmap to allow to set the BitMap // to the ImageView, ImageButton or what ever BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight()); bmd.draw(canvas);/*from ww w . ja v a 2 s . c o m*/ return bmd.getIntrinsicWidth(); }
From source file:com.danimahardhika.android.helpers.core.DrawableHelper.java
@Nullable public static Drawable getResizedDrawable(@NonNull Context context, @NonNull Drawable drawable, float sizeInDp) { try {/*from w ww. j a v a2s . c o m*/ int size = Math.round(UnitHelper.toPixel(context, sizeInDp)); Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true)); } catch (OutOfMemoryError e) { return null; } }