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:github.madmarty.madsonic.util.Util.java

public static Bitmap createBitmapFromDrawable(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }/*from  w  w w .  j  av  a  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:com.android.contacts.DynamicShortcuts.java

private Bitmap getFallbackAvatar(String displayName, String lookupKey) {
    // Use a circular icon if we're not on O or higher.
    final boolean circularIcon = !BuildCompat.isAtLeastO();

    final ContactPhotoManager.DefaultImageRequest request = new ContactPhotoManager.DefaultImageRequest(
            displayName, lookupKey, circularIcon);
    if (BuildCompat.isAtLeastO()) {
        // On O, scale the image down to add the padding needed by AdaptiveIcons.
        request.scale = LetterTileDrawable.getAdaptiveIconScale();
    }//from   w ww . ja  v  a 2s . co m
    final Drawable avatar = ContactPhotoManager.getDefaultAvatarDrawableForContact(mContext.getResources(),
            true, request);
    final Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Bitmap.Config.ARGB_8888);
    // The avatar won't draw unless it thinks it is visible
    avatar.setVisible(true, true);
    final Canvas canvas = new Canvas(result);
    avatar.setBounds(0, 0, mIconSize, mIconSize);
    avatar.draw(canvas);
    return result;
}

From source file:vc908.stickerfactory.ui.advancedrecyclerview.swipeable.RemovingItemDecorator.java

private void fillSwipingItemBackground(Canvas c, Drawable drawable, int height) {
    final Rect bounds = mSwipingItemBounds;
    final int translationX = mInitialTranslationX;
    final int translationY = mTranslationY;

    if (height < 0) {
        height = bounds.height();/*from  w  w w . j a  v a2s.c o m*/
    }

    if ((height == 0) || (drawable == null)) {
        return;
    }

    final int savedCount = c.save();

    c.clipRect(bounds.left + translationX, bounds.top + translationY, bounds.right + translationX,
            bounds.top + translationY + height);

    // c.drawColor(0xffff0000); // <-- debug

    c.translate(bounds.left + translationX, bounds.top + translationY - (bounds.height() - height) / 2);
    drawable.setBounds(0, 0, bounds.width(), bounds.height());

    drawable.draw(c);

    c.restoreToCount(savedCount);
}

From source file:com.goka.flickableview.ImageViewTouchBase.java

@Override
protected void onDraw(final Canvas canvas) {
    if (getScaleType() == ScaleType.FIT_XY) {
        final Drawable drawable = getDrawable();
        if (null != drawable) {
            drawable.draw(canvas);
        }/*from  w w w.  ja v  a 2  s. c o  m*/
    } else {
        super.onDraw(canvas);
    }
}

From source file:am.widget.tabstrip.HorizontalLinearTabStripLayout.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final int count = getChildCount();
    if (mCenter == null && !isShowingDividers() && count <= 0)
        return;//from www . j  a  v  a 2  s  . c  o m
    final int paddingStart = ViewCompat.getPaddingStart(this);
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();
    final int childWidth = mChildWidth;
    final boolean show = isShowingDividers();
    final Drawable dd = mDivider;
    final int divider = show ? dd.getIntrinsicWidth() : 0;
    final int padding = mDividerPadding;
    int start = paddingStart;
    if (show)
        dd.setBounds(1, paddingTop + padding, divider + 1, getHeight() - paddingBottom - padding);
    if (count == 1) {
        if (show && (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) {
            canvas.save();
            canvas.translate(start, 0);
            dd.draw(canvas);
            canvas.restore();
            start += divider;
        }
        start += childWidth;
        if (show && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) {
            canvas.save();
            canvas.translate(start, 0);
            dd.draw(canvas);
            canvas.restore();
        }
    } else {
        final boolean middle = (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE;
        for (int i = 0; i < count; i++) {
            if (i == 0) {
                if (show && (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) {
                    canvas.save();
                    canvas.translate(start, 0);
                    dd.draw(canvas);
                    canvas.restore();
                    start += divider;
                }
                start += childWidth;
            } else if (i == count - 1) {
                if (show && middle) {
                    canvas.save();
                    canvas.translate(start, 0);
                    dd.draw(canvas);
                    canvas.restore();
                    start += divider;
                }
                start += childWidth;
                if (show && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) {
                    start = getWidth() - ViewCompat.getPaddingEnd(this) - divider;
                    canvas.save();
                    canvas.translate(start, 0);
                    dd.draw(canvas);
                    canvas.restore();
                    break;
                }
            } else {
                if (count % 2 == 0 && count / 2 == i && mCenter != null) {
                    if (show && middle && mCenterAsItem) {
                        canvas.save();
                        canvas.translate(start, 0);
                        dd.draw(canvas);
                        canvas.restore();
                        start += divider;
                    }
                    final Drawable center = mCenter;
                    final int p = mCenterPadding;
                    center.setBounds(0, paddingTop + p, center.getIntrinsicWidth(),
                            getHeight() - paddingBottom - p);
                    canvas.save();
                    canvas.translate(start, 0);
                    center.draw(canvas);
                    canvas.restore();
                    start += center.getIntrinsicWidth();
                    if (show && middle && mCenterAsItem) {
                        canvas.save();
                        canvas.translate(start, 0);
                        dd.draw(canvas);
                        canvas.restore();
                        start += divider;
                    }
                } else {
                    if (show && middle) {
                        canvas.save();
                        canvas.translate(start, 0);
                        dd.draw(canvas);
                        canvas.restore();
                        start += divider;
                    }
                }
                start += childWidth;
            }
        }
    }
}

From source file:com.kmagic.solitaire.DrawMaster.java

/**
 * Draw cards with big single center suite
 * @param r application resources reference
 *//*from w  w  w  . j ava2s  .c o  m*/
private void drawBigCards(final Resources r) {

    final Bitmap[] bigSuit = new Bitmap[4];
    final Bitmap[] suit = new Bitmap[4];
    Canvas canvas;
    final String[] card_values = mResources.getStringArray(R.array.card_values);
    final Paint cardFrontPaint = new Paint();
    final Paint cardBorderPaint = new Paint();
    final Paint textPaintLeft = getTextPaint(mSuitsSize, Paint.Align.LEFT);

    Drawable drawable = ResourcesCompat.getDrawable(r, R.drawable.cardback, null);

    mCardHidden = Bitmap.createBitmap(Card.WIDTH, Card.HEIGHT, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(mCardHidden);
    drawable.setBounds(0, 0, Card.WIDTH, Card.HEIGHT);
    drawable.draw(canvas);

    for (int i = 0; i < 4; i++) {
        suit[i] = Bitmap.createBitmap((int) mSuitsSizeHalf, (int) mSuitsSizeHalf, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(suit[i]);
        drawSuit(i, canvas, mSuitsSizeHalf);
    }

    for (int i = 0; i < 4; i++) {
        bigSuit[i] = Bitmap.createBitmap((int) mSuitsSize, (int) mSuitsSize, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bigSuit[i]);
        drawSuit(i, canvas, mSuitsSize);
    }

    cardBorderPaint.setARGB(255, 0, 0, 0);
    cardFrontPaint.setARGB(255, 255, 255, 255);
    RectF rectf = new RectF();
    for (int suitIdx = 0; suitIdx < 4; suitIdx++) {
        for (int valueIdx = 0; valueIdx < 13; valueIdx++) {
            mCardBitmap[suitIdx * 13 + valueIdx] = Bitmap.createBitmap(Card.WIDTH, Card.HEIGHT,
                    Bitmap.Config.ARGB_8888);
            canvas = new Canvas(mCardBitmap[suitIdx * 13 + valueIdx]);

            drawCardBackground(rectf, canvas, cardBorderPaint, cardFrontPaint);
            drawCardValue(textPaintLeft, canvas, card_values[valueIdx], suit[suitIdx], suitIdx);
            // Middle
            canvas.drawBitmap(bigSuit[suitIdx], Card.WIDTH / 2 - mSuitsSizeHalf,
                    Card.HEIGHT / 2 - mSuitsSizeHalf, mSuitPaint);
        }
    }
}

From source file:it.ndorigatti.android.view.MulticolorProgressBar.java

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Drawable d = mCurrentDrawable;
    if (d != null) {
        // Translate canvas so a indeterminate circular progress bar with padding
        // rotates properly in its animation
        canvas.save();//  www.jav a 2s.  c o  m
        canvas.translate(getPaddingLeft(), getPaddingTop());
        d.draw(canvas);
        canvas.restore();
    }
}

From source file:com.woxthebox.draglistview.DragItemRecyclerView.java

private void init() {
    mAutoScroller = new AutoScroller(getContext(), this);
    mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

    addItemDecoration(new ItemDecoration() {
        @Override/*from   ww  w.j  av  a  2 s.c o m*/
        public void onDraw(Canvas c, RecyclerView parent, State state) {
            super.onDraw(c, parent, state);
            drawDecoration(c, parent, mDropTargetBackgroundDrawable);
        }

        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, State state) {
            super.onDrawOver(c, parent, state);
            drawDecoration(c, parent, mDropTargetForegroundDrawable);
        }

        private void drawDecoration(Canvas c, RecyclerView parent, Drawable drawable) {
            if (mAdapter.getDropTargetId() == NO_ID || drawable == null) {
                return;
            }

            for (int i = 0; i < parent.getChildCount(); i++) {
                View item = parent.getChildAt(i);
                int pos = getChildAdapterPosition(item);
                if (pos != NO_POSITION && mAdapter.getItemId(pos) == mAdapter.getDropTargetId()) {
                    drawable.setBounds(item.getLeft(), item.getTop(), item.getRight(), item.getBottom());
                    drawable.draw(c);
                }
            }
        }
    });
}

From source file:org.addhen.smssync.presentation.view.ui.fragment.PublishedMessageFragment.java

private void drawSwipeListItemBackground(Canvas c, int dX, View itemView, int actionState) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        // Fade out the view as it is swiped out of the parent's bounds
        final float alpha = 1.0f - Math.abs(dX) / (float) itemView.getWidth();
        ViewHelper.setAlpha(itemView, alpha);
        ViewHelper.setTranslationX(itemView, dX);
        Drawable d;
        // Swiping right
        if (dX > 0) {
            d = ContextCompat.getDrawable(getAppContext(), R.drawable.swipe_right_publish_list_item_background);
            d.setBounds(itemView.getLeft(), itemView.getTop(), dX, itemView.getBottom());
        } else { // Swiping left
            d = ContextCompat.getDrawable(getAppContext(), R.drawable.swipe_left_publish_list_item_background);
            d.setBounds(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
        }// w  w  w.  j  a  v  a  2 s  . com
        d.draw(c);
    }
}

From source file:kr.wdream.storyshop.AndroidUtilities.java

private static Intent createShortcutIntent(long did, boolean forDelete) {
    Intent shortcutIntent = new Intent(ApplicationLoader.applicationContext, OpenChatReceiver.class);

    int lower_id = (int) did;
    int high_id = (int) (did >> 32);

    TLRPC.User user = null;// w  w w .  j  ava  2s .  c o m
    TLRPC.Chat chat = null;
    if (lower_id == 0) {
        shortcutIntent.putExtra("encId", high_id);
        TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(high_id);
        if (encryptedChat == null) {
            return null;
        }
        user = MessagesController.getInstance().getUser(encryptedChat.user_id);
    } else if (lower_id > 0) {
        shortcutIntent.putExtra("userId", lower_id);
        user = MessagesController.getInstance().getUser(lower_id);
    } else if (lower_id < 0) {
        chat = MessagesController.getInstance().getChat(-lower_id);
        shortcutIntent.putExtra("chatId", -lower_id);
    } else {
        return null;
    }
    if (user == null && chat == null) {
        return null;
    }

    String name;
    TLRPC.FileLocation photo = null;

    if (user != null) {
        name = ContactsController.formatName(user.first_name, user.last_name);
        if (user.photo != null) {
            photo = user.photo.photo_small;
        }
    } else {
        name = chat.title;
        if (chat.photo != null) {
            photo = chat.photo.photo_small;
        }
    }

    shortcutIntent.setAction("com.tmessages.openchat" + did);
    shortcutIntent.addFlags(0x4000000);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    addIntent.putExtra("duplicate", false);
    if (!forDelete) {
        Bitmap bitmap = null;
        if (photo != null) {
            try {
                File path = FileLoader.getPathToAttach(photo, true);
                bitmap = BitmapFactory.decodeFile(path.toString());
                if (bitmap != null) {
                    int size = AndroidUtilities.dp(58);
                    Bitmap result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                    result.eraseColor(Color.TRANSPARENT);
                    Canvas canvas = new Canvas(result);
                    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                            Shader.TileMode.CLAMP);
                    if (roundPaint == null) {
                        roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                        bitmapRect = new RectF();
                    }
                    float scale = size / (float) bitmap.getWidth();
                    canvas.save();
                    canvas.scale(scale, scale);
                    roundPaint.setShader(shader);
                    bitmapRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    canvas.drawRoundRect(bitmapRect, bitmap.getWidth(), bitmap.getHeight(), roundPaint);
                    canvas.restore();
                    Drawable drawable = ApplicationLoader.applicationContext.getResources()
                            .getDrawable(R.drawable.book_logo);
                    int w = AndroidUtilities.dp(15);
                    int left = size - w - AndroidUtilities.dp(2);
                    int top = size - w - AndroidUtilities.dp(2);
                    drawable.setBounds(left, top, left + w, top + w);
                    drawable.draw(canvas);
                    try {
                        canvas.setBitmap(null);
                    } catch (Exception e) {
                        //don't promt, this will crash on 2.x
                    }
                    bitmap = result;
                }
            } catch (Throwable e) {
                FileLog.e("tmessages", e);
            }
        }
        if (bitmap != null) {
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
        } else {
            if (user != null) {
                if (user.bot) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_bot));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_user));
                }
            } else if (chat != null) {
                if (ChatObject.isChannel(chat) && !chat.megagroup) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_channel));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_group));
                }
            }
        }
    }
    return addIntent;
}