Example usage for android.graphics.drawable Drawable draw

List of usage examples for android.graphics.drawable Drawable draw

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable draw.

Prototype

public abstract void draw(@NonNull Canvas canvas);

Source Link

Document

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

Usage

From source file:com.waz.zclient.pages.main.conversationpager.SlidingPaneLayout.java

@Override
public void draw(Canvas c) {
    super.draw(c);
    final boolean isLayoutRtl = isLayoutRtlSupport();
    Drawable shadowDrawable;
    if (isLayoutRtl) {
        shadowDrawable = shadowDrawableRight;
    } else {//from w  ww.j  ava  2 s. co  m
        shadowDrawable = shadowDrawableLeft;
    }

    final View shadowView = getChildCount() > 1 ? getChildAt(1) : null;
    if (shadowView == null || shadowDrawable == null) {
        // No need to draw a shadow if we don't have one.
        return;
    }

    final int top = shadowView.getTop();
    final int bottom = shadowView.getBottom();

    final int shadowWidth = shadowDrawable.getIntrinsicWidth();
    final int left;
    final int right;
    if (isLayoutRtlSupport()) {
        left = shadowView.getRight();
        right = left + shadowWidth;
    } else {
        right = shadowView.getLeft();
        left = right - shadowWidth;
    }

    shadowDrawable.setBounds(left, top, right, bottom);
    shadowDrawable.draw(c);
}

From source file:com.mcxiaoke.minicat.ui.widget.StaggeredGridView.java

private void drawSelector(Canvas canvas) {
    if (!mSelectorRect.isEmpty() && mSelector != null) {
        final Drawable selector = mSelector;
        selector.setBounds(mSelectorRect);
        selector.draw(canvas);
    }//from w w  w .  j a  v  a  2s . c o m
}

From source file:com.linkbubble.util.Util.java

/**
 * Returns a bitmap suitable for the all apps view.
 *///ww  w  .  j a va  2 s.c  o 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:cn.bingoogolapple.swipebacklayout.BGASwipeBackLayout.java

@Override
public void draw(Canvas c) {
    super.draw(c);
    final boolean isLayoutRtl = isLayoutRtlSupport();
    Drawable shadowDrawable;
    if (isLayoutRtl) {
        shadowDrawable = mShadowDrawableRight;
    } else {/*from   w w  w  .j  av a 2 s .c  o  m*/
        shadowDrawable = mShadowDrawableLeft;
    }

    final View shadowView = getChildCount() > 1 ? getChildAt(1) : null;
    if (shadowView == null || shadowDrawable == null) {
        // No need to draw a shadow if we don't have one.
        return;
    }

    final int top = shadowView.getTop();
    final int bottom = shadowView.getBottom();

    final int shadowWidth = shadowDrawable.getIntrinsicWidth();
    final int left;
    final int right;
    if (isLayoutRtlSupport()) {
        left = shadowView.getRight();
        right = left + shadowWidth;
    } else {
        right = shadowView.getLeft();
        left = right - shadowWidth;
    }

    shadowDrawable.setBounds(left, top, right, bottom);
    shadowDrawable.draw(c);
}

From source file:chan.android.app.bitwise.util.StaggeredGridView.java

private void drawSelector(Canvas canvas) {
    if (!mSelectorRect.isEmpty() && mSelector != null && mBeginClick) {
        final Drawable selector = mSelector;
        selector.setBounds(mSelectorRect);
        selector.draw(canvas);
    }//  w  ww  .  j  a v  a 2 s .  c om
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

/**
 * Draws a divider for the given child in the given bounds.
 * /* w  w  w  . jav a2  s  .co m*/
 * @param canvas The canvas to draw to.
 * @param bounds The bounds of the divider.
 * @param childIndex The index of child (of the View) above the divider.
 *            This will be -1 if there is no child above the divider to be
 *            drawn.
 */
void drawDivider(final Canvas canvas, final Rect bounds, final int childIndex) {
    // This widget draws the same divider for all children
    final Drawable divider = mDivider;
    final boolean clipDivider = mClipDivider;

    if (!clipDivider) {
        divider.setBounds(bounds);
    } else {
        canvas.save();
        canvas.clipRect(bounds);
    }

    divider.draw(canvas);

    if (clipDivider) {
        canvas.restore();
    }
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private Bitmap createUnselectedChip(final RecipientEntry contact, final TextPaint paint,
        final boolean leaveBlankIconSpacer) {
    // Ellipsize the text so that it takes AT MOST the entire width of the
    // autocomplete text entry area. Make sure to leave space for padding
    // on the sides.
    final int height = (int) mChipHeight;
    int iconWidth = height;
    final float[] widths = new float[1];
    paint.getTextWidths(" ", widths);
    final float availableWidth = calculateAvailableWidth();
    final String chipDisplayText = createChipDisplayText(contact);
    final CharSequence ellipsizedText = ellipsizeText(chipDisplayText, paint,
            availableWidth - iconWidth - widths[0]);
    // Make sure there is a minimum chip width so the user can ALWAYS
    // tap a chip without difficulty.
    final int width = Math.max(iconWidth * 2,
            (int) Math.floor(paint.measureText(ellipsizedText, 0, ellipsizedText.length())) + mChipPadding * 2
                    + iconWidth);/* w  w  w. j  a v a 2 s .c o  m*/
    // Create the background of the chip.
    final Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(tmpBitmap);
    final Drawable background = getChipBackground(contact);
    if (background != null) {
        background.setBounds(0, 0, width, height);
        background.draw(canvas);
        // Don't draw photos for recipients that have been typed in OR generated on the fly.
        final long contactId = contact.getContactId();
        final boolean drawPhotos = isPhoneQuery() ? contactId != RecipientEntry.INVALID_CONTACT
                : contactId != RecipientEntry.INVALID_CONTACT && contactId != RecipientEntry.GENERATED_CONTACT
                        && !TextUtils.isEmpty(contact.getDisplayName());
        if (drawPhotos) {
            byte[] photoBytes = contact.getPhotoBytes();
            // There may not be a photo yet if anything but the first contact address
            // was selected.
            if (photoBytes == null && contact.getPhotoThumbnailUri() != null) {
                // TODO: cache this in the recipient entry?
                getAdapter().fetchPhoto(contact, contact.getPhotoThumbnailUri());
                photoBytes = contact.getPhotoBytes();
            }
            Bitmap photo;
            if (photoBytes != null)
                photo = BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length);
            else // TODO: can the scaled down default photo be cached?
                photo = mDefaultContactPhoto;
            // Draw the photo on the left side.
            if (photo != null) {
                final RectF src = new RectF(0, 0, photo.getWidth(), photo.getHeight());
                final Rect backgroundPadding = new Rect();
                mChipBackground.getPadding(backgroundPadding);
                final RectF dst = new RectF(width - iconWidth + backgroundPadding.left,
                        0 + backgroundPadding.top, width - backgroundPadding.right,
                        height - backgroundPadding.bottom);
                final Matrix matrix = new Matrix();
                matrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
                canvas.drawBitmap(photo, matrix, paint);
            }
        } else if (!leaveBlankIconSpacer || isPhoneQuery())
            iconWidth = 0;
        paint.setColor(ContextCompat.getColor(getContext(), android.R.color.black));
        // Vertically center the text in the chip.
        canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding,
                getTextYOffset((String) ellipsizedText, paint, height), paint);
    } else
        Log.w(TAG, "Unable to draw a background for the chips as it was never set");
    return tmpBitmap;
}

From source file:com.facebook.litho.LayoutState.java

void createDisplayList(LayoutOutput output) {
    ThreadUtils.assertMainThread();/*from w  w  w.j a  v  a2 s.com*/

    final Component component = output.getComponent();
    ComponentsSystrace.beginSection("createDisplayList: " + component.getSimpleName());
    final ComponentLifecycle lifecycle = component.getLifecycle();
    final DisplayList displayList = DisplayList.createDisplayList(lifecycle.getClass().getSimpleName());

    if (displayList == null) {
        ComponentsSystrace.endSection();
        return;
    }

    final ComponentContext context = mContext;

    Drawable drawable = (Drawable) ComponentsPools.acquireMountContent(context, lifecycle.getId());
    if (drawable == null) {
        drawable = (Drawable) lifecycle.createMountContent(context);
    }

    final LayoutOutput clickableOutput = findInteractiveRoot(this, output);
    boolean isStateEnabled = false;

    if (clickableOutput != null && clickableOutput.getNodeInfo() != null) {
        final NodeInfo nodeInfo = clickableOutput.getNodeInfo();

        if (nodeInfo.hasTouchEventHandlers() || nodeInfo.getFocusState() == FOCUS_SET_TRUE) {
            isStateEnabled = true;
        }
    }

    if (isStateEnabled) {
        drawable.setState(DRAWABLE_STATE_ENABLED);
    } else {
        drawable.setState(DRAWABLE_STATE_NOT_ENABLED);
    }

    lifecycle.mount(context, drawable, component);
    lifecycle.bind(context, drawable, component);

    final Rect rect = mDisplayListCreateRect;

    output.getMountBounds(rect);
    drawable.setBounds(0, 0, rect.width(), rect.height());

    try {
        final Canvas canvas = displayList.start(rect.width(), rect.height());
        drawable.draw(canvas);

        displayList.end(canvas);
        displayList.setBounds(rect.left, rect.top, rect.right, rect.bottom);

        output.setDisplayList(displayList);
    } catch (DisplayListException e) {
        // Display list creation failed. Make sure the DisplayList for this output is set
        // to null.
        output.setDisplayList(null);
    }

    lifecycle.unbind(context, drawable, component);
    lifecycle.unmount(context, drawable, component);
    ComponentsPools.release(context, lifecycle, drawable);
    ComponentsSystrace.endSection();
}

From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

/**
 * When entering and exiting the TabSwitcher mode, we fade out or fade in the browsing
 * mode of the toolbar on top of the TabSwitcher mode version of it.  We do this by
 * drawing all of the browsing mode views on top of the android view.
 *///from www  .  j  a va  2  s. c  o m
private void drawTabSwitcherAnimationOverlay(Canvas canvas, float animationProgress) {
    if (!isNativeLibraryReady())
        return;

    float floatAlpha = 1 - animationProgress;
    int rgbAlpha = (int) (255 * floatAlpha);
    canvas.save();
    canvas.translate(0, -animationProgress * mBackgroundOverlayBounds.height());
    canvas.clipRect(mBackgroundOverlayBounds);

    float previousAlpha = 0.f;
    if (mHomeButton.getVisibility() != View.GONE) {
        // Draw the New Tab button used in the URL view.
        previousAlpha = mHomeButton.getAlpha();
        mHomeButton.setAlpha(previousAlpha * floatAlpha);
        drawChild(canvas, mHomeButton, SystemClock.uptimeMillis());
        mHomeButton.setAlpha(previousAlpha);
    }

    // Draw the location/URL bar.
    previousAlpha = mLocationBar.getAlpha();
    mLocationBar.setAlpha(previousAlpha * floatAlpha);
    // If the location bar is now fully transparent, do not bother drawing it.
    if (mLocationBar.getAlpha() != 0) {
        drawChild(canvas, mLocationBar, SystemClock.uptimeMillis());
    }
    mLocationBar.setAlpha(previousAlpha);

    // Draw the tab stack button and associated text.
    translateCanvasToView(this, mToolbarButtonsContainer, canvas);

    if (mTabSwitcherAnimationTabStackDrawable != null && mToggleTabStackButton != null
            && mUrlExpansionPercent != 1f) {
        // Draw the tab stack button image.
        canvas.save();
        translateCanvasToView(mToolbarButtonsContainer, mToggleTabStackButton, canvas);

        int backgroundWidth = mToggleTabStackButton.getDrawable().getIntrinsicWidth();
        int backgroundHeight = mToggleTabStackButton.getDrawable().getIntrinsicHeight();
        int backgroundLeft = (mToggleTabStackButton.getWidth() - mToggleTabStackButton.getPaddingLeft()
                - mToggleTabStackButton.getPaddingRight() - backgroundWidth) / 2;
        backgroundLeft += mToggleTabStackButton.getPaddingLeft();
        int backgroundTop = (mToggleTabStackButton.getHeight() - mToggleTabStackButton.getPaddingTop()
                - mToggleTabStackButton.getPaddingBottom() - backgroundHeight) / 2;
        backgroundTop += mToggleTabStackButton.getPaddingTop();
        canvas.translate(backgroundLeft, backgroundTop);

        mTabSwitcherAnimationTabStackDrawable.setAlpha(rgbAlpha);
        mTabSwitcherAnimationTabStackDrawable.draw(canvas);
        canvas.restore();
    }

    // Draw the menu button if necessary.
    if (!mShowMenuBadge && mTabSwitcherAnimationMenuDrawable != null && mUrlExpansionPercent != 1f) {
        mTabSwitcherAnimationMenuDrawable.setBounds(mMenuButton.getPaddingLeft(), mMenuButton.getPaddingTop(),
                mMenuButton.getWidth() - mMenuButton.getPaddingRight(),
                mMenuButton.getHeight() - mMenuButton.getPaddingBottom());
        translateCanvasToView(mToolbarButtonsContainer, mMenuButton, canvas);
        mTabSwitcherAnimationMenuDrawable.setAlpha(rgbAlpha);
        int color = mUseLightDrawablesForTextureCapture ? mLightModeDefaultColor : mDarkModeDefaultColor;
        mTabSwitcherAnimationMenuDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        mTabSwitcherAnimationMenuDrawable.draw(canvas);
    }

    // Draw the menu badge if necessary.
    Drawable badgeDrawable = mUseLightDrawablesForTextureCapture ? mTabSwitcherAnimationMenuBadgeLightDrawable
            : mTabSwitcherAnimationMenuBadgeDarkDrawable;
    if (mShowMenuBadge && badgeDrawable != null && mUrlExpansionPercent != 1f) {
        badgeDrawable.setBounds(mMenuBadge.getPaddingLeft(), mMenuBadge.getPaddingTop(),
                mMenuBadge.getWidth() - mMenuBadge.getPaddingRight(),
                mMenuBadge.getHeight() - mMenuBadge.getPaddingBottom());
        translateCanvasToView(mToolbarButtonsContainer, mMenuBadge, canvas);
        badgeDrawable.setAlpha(rgbAlpha);
        badgeDrawable.draw(canvas);
    }

    mLightDrawablesUsedForLastTextureCapture = mUseLightDrawablesForTextureCapture;

    canvas.restore();
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

void drawOverscrollFooter(final Canvas canvas, final Drawable drawable, final Rect bounds) {
    final int height = drawable.getMinimumHeight();

    canvas.save();//from w w  w. java2  s. co m
    canvas.clipRect(bounds);

    final int span = bounds.bottom - bounds.top;
    if (span < height) {
        bounds.bottom = bounds.top + height;
    }

    drawable.setBounds(bounds);
    drawable.draw(canvas);

    canvas.restore();
}