List of usage examples for android.graphics.drawable Drawable setBounds
public void setBounds(int left, int top, int right, int bottom)
From source file:com.tr4android.support.extension.widget.CircleImageView.java
/** * Helper for creating a bitmap from a drawable * * @param drawable The drawable which should be converted to a bitmap * @return the bitmap containing the drawable *///from w w w .j av a 2s . c om public static Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) return null; if (drawable instanceof BitmapDrawable) { Log.w(LOG_TAG, "For better performance consider using setImageBitmap() instead!"); return ((BitmapDrawable) drawable).getBitmap(); } else { Bitmap bitmap = Bitmap.createBitmap(Math.max(2, drawable.getIntrinsicWidth()), Math.max(2, drawable.getIntrinsicHeight()), 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
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }/*from w w w. java 2 s. co m*/ // We ask for the bounds if they have been set as they would be most // correct, then we check we are > 0 final int width = !drawable.getBounds().isEmpty() ? drawable.getBounds().width() : drawable.getIntrinsicWidth(); final int height = !drawable.getBounds().isEmpty() ? drawable.getBounds().height() : drawable.getIntrinsicHeight(); // Now we check we are > 0 final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : 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:com.hippo.android.animator.AnimatorsBase.java
static Animator crossFade(final View from, final View to, ViewGroup ancestor, final boolean toIsTop) { // Ensure views is laid out if (!ViewCompat.isLaidOut(from) || !ViewCompat.isLaidOut(to)) { Log.w(LOG_TAG, "From view and to view must be laid out before calling crossFade()."); return null; }/*from www.j av a 2 s . c om*/ // Get overlay final ViewOverlayCompat overlay = ViewOverlayCompat.from(ancestor); if (overlay == null) { Log.w(LOG_TAG, "The ancestor in crossFade() must be able to create a ViewOverlay."); return null; } // Get the location of from view if (!Utils.getLocationInAncestor(from, ancestor, TEMP_LOCATION)) { Log.w(LOG_TAG, "From view must be in ancestor in crossFade()."); return null; } int fromX = TEMP_LOCATION[0]; int fromY = TEMP_LOCATION[1]; // Get the location of to view if (!Utils.getLocationInAncestor(to, ancestor, TEMP_LOCATION)) { Log.w(LOG_TAG, "From view must be in ancestor in crossFade()."); return null; } int toX = TEMP_LOCATION[0]; int toY = TEMP_LOCATION[1]; // Get the screenshot of from view final Bitmap fromBitmap = Utils.screenshot(from); if (fromBitmap == null) { Log.w(LOG_TAG, "Can't screenshot from view in crossFade()."); return null; } // Get the screenshot of to view final Bitmap toBitmap = Utils.screenshot(to); if (toBitmap == null) { Log.w(LOG_TAG, "Can't screenshot to view in crossFade()."); fromBitmap.recycle(); return null; } // Create start drawables final Drawable fromDrawable = new BitmapDrawable(from.getContext().getResources(), fromBitmap); int fromWidth = fromBitmap.getWidth(); int fromHeight = fromBitmap.getHeight(); fromDrawable.setBounds(fromX, fromY, fromX + fromWidth, fromY + fromHeight); int fromCenterX = fromX + fromWidth / 2; int fromCenterY = fromY + fromHeight / 2; // Create end drawable final Drawable toDrawable = new BitmapDrawable(to.getContext().getResources(), toBitmap); int toWidth = toBitmap.getWidth(); int toHeight = toBitmap.getHeight(); int toStartX = fromCenterX - toWidth / 2; int toStartY = fromCenterY - toHeight / 2; toDrawable.setBounds(toStartX, toStartY, toStartX + toWidth, toStartY + toHeight); int toCenterX = toX + toWidth / 2; int toCenterY = toY + toHeight / 2; List<Animator> set = new ArrayList<>(4); // Create alpha animators Animator fromAlpha = ObjectAnimator.ofInt(fromDrawable, DRAWABLE_ALPHA_PROPERTY, 255, 0); Animator toAlpha = ObjectAnimator.ofInt(toDrawable, DRAWABLE_ALPHA_PROPERTY, 0, 255); set.add(fromAlpha); set.add(toAlpha); // Create position animators if (fromCenterX != toCenterX || fromCenterY != toCenterY) { Path path = ACR_PATH_MOTION.getPath(fromCenterX, fromCenterY, toCenterX, toCenterY); Animator fromPosition = Animators.ofPointF(fromDrawable, DRAWABLE_POSITION_PROPERTY, path); Animator toPosition = Animators.ofPointF(toDrawable, DRAWABLE_POSITION_PROPERTY, path); set.add(fromPosition); set.add(toPosition); } Animator animator = Animators.playTogether(set); animator.addListener(new AnimatorListenerAdapter() { float fromAlpha; float toAlpha; @Override public void onAnimationStart(Animator animation) { // Add drawables to overlay if (toIsTop) { overlay.add(fromDrawable); overlay.add(toDrawable); } else { overlay.add(toDrawable); overlay.add(fromDrawable); } // Hide from view and to view fromAlpha = from.getAlpha(); toAlpha = to.getAlpha(); from.setAlpha(0.0f); to.setAlpha(0.0f); } @Override public void onAnimationEnd(Animator animation) { // Remove drawables from overlay overlay.remove(fromDrawable); overlay.remove(toDrawable); // Show from view and to view from.setAlpha(fromAlpha); to.setAlpha(toAlpha); // Recycle bitmaps fromBitmap.recycle(); toBitmap.recycle(); } }); return animator; }
From source file:com.android.leanlauncher.WidgetPreviewLoader.java
private static void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) { if (bitmap != null) { final Canvas c = new Canvas(bitmap); Rect oldBounds = d.copyBounds(); d.setBounds(x, y, x + w, y + h); d.draw(c);//from w w w. j ava2 s . c o m d.setBounds(oldBounds); // Restore the bounds c.setBitmap(null); } }
From source file:com.alainesp.fan.sanderson.MainActivity.java
/** * Set the badge number to all menu items visible */// w w w . j a v a 2s . c om private static void setBadgesNumberUI() { if (navigationView != null) { NavigationMenuView v = (NavigationMenuView) navigationView.getChildAt(0); Menu drawerMenu = navigationView.getMenu(); if (v != null) // Iterate all children for (int childIndex = 0; childIndex < v.getChildCount(); childIndex++) { View v1 = v.getChildAt(childIndex); if (v1 instanceof NavigationMenuItemView) { TextView mTextView = (TextView) ((NavigationMenuItemView) v1).getChildAt(0); if (mTextView != null) { // Get the menu index Integer menuIndex = reverseMenuText.get(mTextView.getText().toString()); if (menuIndex != null && menuIndex < badgeNumbers.length) { Drawable numberText = null; if (badgeNumbers[menuIndex] > 0) { int height = mTextView.getHeight(); numberText = new TextDrawable(badgeNumbers[menuIndex], mTextView.getTextSize(), mTextView.getCurrentTextColor(), height); numberText.setBounds(0, 0, height, height); } // Similar to NavigationMenuItemView.setIcon Drawable icon = drawerMenu.getItem(menuIndex).getIcon(); Drawable.ConstantState state = icon.getConstantState(); icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate(); int mIconSize = navigationView.getContext().getResources().getDimensionPixelSize( android.support.design.R.dimen.design_navigation_icon_size); icon.setBounds(0, 0, mIconSize, mIconSize); DrawableCompat.setTintList(icon, navigationView.getItemIconTintList()); TextViewCompat.setCompoundDrawablesRelative(mTextView, icon, null, numberText, null); } } } } } }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap3(Drawable imageDrawable, int radius) { Bitmap d = ((BitmapDrawable) imageDrawable).getBitmap(); BitmapShader shader = new BitmapShader(d, TileMode.CLAMP, TileMode.CLAMP); int size = Math.min(d.getWidth(), d.getHeight()); Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); RectF outerRect = new RectF(0, 0, size, size); // float cornerRadius = radius; Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setShader(shader);/* w w w. java 2 s . c o m*/ paint.setAntiAlias(true); paint.setColor(Color.RED); canvas.drawCircle(outerRect.centerX(), outerRect.centerY(), d.getWidth() / 2, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, size, size); Canvas canvas1 = new Canvas(output); RectF outerRect1 = new RectF(0, 0, size, size); Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG); paint1.setShader(shader); paint1.setAntiAlias(true); paint1.setColor(Color.RED); canvas1.drawCircle(outerRect1.centerX(), outerRect1.centerY(), d.getWidth() / 2, paint); paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); return output; }
From source file:org.bottiger.podcast.utils.UIUtils.java
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); }/*from w w w .j ava2s . c om*/ 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 bitmap; }
From source file:io.jawg.osmcontributor.ui.utils.BitmapHandler.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }//from w ww . j a va 2s . c o m 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 bitmap; }
From source file:Main.java
public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness) { int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight()); // Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder; Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); RectF outerRect = new RectF(0, 0, size, size); float cornerRadius = size / 18f; Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED);/* w w w . j a v a2 s . c o m*/ canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, size, size); // Save the layer to apply the paint canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG); imageDrawable.draw(canvas); canvas.restore(); // FRAMING THE PHOTO float border = size / 15f; // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas framedCanvas = new Canvas(framedOutput); // End of Step 1 // Start - TODO IMPORTANT - this section shouldn't be included in the final code // It's needed here to differentiate step 2 (red) with the background color of the activity // It's should be commented out after the codes includes step 3 onwards // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // squaredPaint.setColor(Color.BLUE); // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint); // End // 2. Draw an opaque rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s RectF innerRect = new RectF(border, border, size - border, size - border); Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); innerPaint.setColor(Color.RED); framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint); // 3. Set the Power Duff mode link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // 4. Draw a translucent rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU outerPaint.setColor(Color.argb(100, 0, 0, 0)); framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint); // 5. Draw the frame on top of original bitmap canvas.drawBitmap(framedOutput, 0f, 0f, null); return output; }
From source file:Main.java
public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness, int color) { int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight()); // Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder; Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); RectF outerRect = new RectF(0, 0, size, size); float cornerRadius = size / 18f; Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED);/*from w w w . j a v a 2s .c o m*/ canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, size, size); // Save the layer to apply the paint canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG); imageDrawable.draw(canvas); canvas.restore(); // FRAMING THE PHOTO float border = size / 15f; // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas framedCanvas = new Canvas(framedOutput); // End of Step 1 // Start - TODO IMPORTANT - this section shouldn't be included in the final code // It's needed here to differentiate step 2 (red) with the background color of the activity // It's should be commented out after the codes includes step 3 onwards // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // squaredPaint.setColor(Color.BLUE); // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint); // End // 2. Draw an opaque rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s RectF innerRect = new RectF(border, border, size - border, size - border); Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); innerPaint.setColor(Color.RED); framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint); // 3. Set the Power Duff mode link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // 4. Draw a translucent rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU outerPaint.setColor(color); framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint); // 5. Draw the frame on top of original bitmap canvas.drawBitmap(framedOutput, 0f, 0f, null); return output; }