Java Code Examples for android.graphics.drawable.Drawable#draw()

The following examples show how to use android.graphics.drawable.Drawable#draw() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrawableFactory.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
private synchronized Bitmap getUserBadge(UserHandle user, Context context) {
    Bitmap badgeBitmap = mUserBadges.get(user);
    if (badgeBitmap != null) {
        return badgeBitmap;
    }

    final Resources res = context.getApplicationContext().getResources();
    int badgeSize = res.getDimensionPixelSize(R.dimen.profile_badge_size);
    badgeBitmap = Bitmap.createBitmap(badgeSize, badgeSize, Bitmap.Config.ARGB_8888);

    Drawable drawable = context.getPackageManager().getUserBadgedDrawableForDensity(
            new BitmapDrawable(res, badgeBitmap), user, new Rect(0, 0, badgeSize, badgeSize),
            0);
    if (drawable instanceof BitmapDrawable) {
        badgeBitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        badgeBitmap.eraseColor(Color.TRANSPARENT);
        Canvas c = new Canvas(badgeBitmap);
        drawable.setBounds(0, 0, badgeSize, badgeSize);
        drawable.draw(c);
        c.setBitmap(null);
    }

    mUserBadges.put(user, badgeBitmap);
    return badgeBitmap;
}
 
Example 2
Source File: HyperLibUtils.java    From YCCustomText with Apache License 2.0 6 votes vote down vote up
public static Bitmap toBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }
    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    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;
}
 
Example 3
Source File: Utils.java    From android-utils with MIT License 6 votes vote down vote up
/**
 * Converts the passed in drawable to Bitmap representation
 *
 * @throws NullPointerException If the parameter drawable is null.
 **/
public static Bitmap drawableToBitmap(Drawable drawable) {

    if (drawable == null) {
        throw new NullPointerException("Drawable to convert should NOT be null");
    }

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    if (drawable.getIntrinsicWidth() <= 0 && drawable.getIntrinsicHeight() <= 0) {
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}
 
Example 4
Source File: PropertyToggle.java    From remoteyourcam-usb with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final Drawable d = drawable;
    if (d != null) {
        int h = d.getIntrinsicHeight();
        int w = d.getIntrinsicWidth();
        int y = (getHeight() - h) / 2;
        int x = (getWidth() - w) / 2;
        d.setBounds(x, y, x + w, y + h);
        d.draw(canvas);
    }
}
 
Example 5
Source File: IconHintView.java    From styT with Apache License 2.0 5 votes vote down vote up
private Bitmap drawableToBitmap(Drawable drawable) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;
    Bitmap bitmap = Bitmap.createBitmap(width, height, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);
    return bitmap;
}
 
Example 6
Source File: AppWidgetManagerCompatVL.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap getBadgeBitmap(AppWidgetProviderInfo info, Bitmap bitmap) {
    if (info.getProfile().equals(android.os.Process.myUserHandle())) {
        return bitmap;
    }

    // Add a user badge in the bottom right of the image.
    final Resources res = mContext.getResources();
    final int badgeSize = res.getDimensionPixelSize(R.dimen.profile_badge_size);
    final int badgeMargin = res.getDimensionPixelSize(R.dimen.profile_badge_margin);
    final Rect badgeLocation = new Rect(0, 0, badgeSize, badgeSize);

    final int top = bitmap.getHeight() - badgeSize - badgeMargin;
    if (res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
        badgeLocation.offset(badgeMargin, top);
    } else {
        badgeLocation.offset(bitmap.getWidth() - badgeSize - badgeMargin, top);
    }

    Drawable drawable = mPm.getUserBadgedDrawableForDensity(
            new BitmapDrawable(res, bitmap), info.getProfile(), badgeLocation, 0);

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    bitmap.eraseColor(Color.TRANSPARENT);
    Canvas c = new Canvas(bitmap);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    drawable.draw(c);
    c.setBitmap(null);
    return bitmap;
}
 
Example 7
Source File: OneDrawable.java    From OneDrawable with Apache License 2.0 5 votes vote down vote up
private static Drawable kitkatUnableDrawable(Context context, @NonNull Drawable pressed) {
    Bitmap bitmap = Bitmap.createBitmap(pressed.getIntrinsicWidth(), pressed.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas myCanvas = new Canvas(bitmap);
    pressed.setAlpha(convertAlphaToInt(0.5f));
    pressed.setBounds(0, 0, pressed.getIntrinsicWidth(), pressed.getIntrinsicHeight());
    pressed.draw(myCanvas);
    return new BitmapDrawable(context.getResources(), bitmap);
}
 
Example 8
Source File: IcsProgressBar.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
@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();
        canvas.translate(getPaddingLeft() + mIndeterminateRealLeft, getPaddingTop() + mIndeterminateRealTop);
        long time = getDrawingTime();
        if (mAnimation != null) {
            mAnimation.getTransformation(time, mTransformation);
            float scale = mTransformation.getAlpha();
            try {
                mInDrawing = true;
                d.setLevel((int) (scale * MAX_LEVEL));
            } finally {
                mInDrawing = false;
            }
            if (SystemClock.uptimeMillis() - mLastDrawTime >= mAnimationResolution) {
                mLastDrawTime = SystemClock.uptimeMillis();
                postInvalidateDelayed(mAnimationResolution);
            }
        }
        d.draw(canvas);
        canvas.restore();
        if (mShouldStartAnimationDrawable && d instanceof Animatable) {
            ((Animatable) d).start();
            mShouldStartAnimationDrawable = false;
        }
    }
}
 
Example 9
Source File: DpadAwareRecyclerView.java    From dpad-aware-recycler-view with Apache License 2.0 5 votes vote down vote up
private void drawSelectorIfVisible(@Selector int index, Canvas canvas) {
    enforceSelectorIndexBounds(index);

    Drawable selector = mSelectorDrawables[index];
    if (selector != null && selector.isVisible()) {
        selector.draw(canvas);
    }
}
 
Example 10
Source File: ThemeUtils.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
private static Bitmap getBitmapFromVectorDrawable(@NonNull Context context, int drawableId) {
    Drawable drawable = getVectorDrawable(context, drawableId);

    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;
}
 
Example 11
Source File: PlayingNotificationImpl.java    From RetroMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap createBitmap(Drawable drawable, float sizeMultiplier) {
    Bitmap bitmap = Bitmap.createBitmap((int) (drawable.getIntrinsicWidth() * sizeMultiplier), (int) (drawable.getIntrinsicHeight() * sizeMultiplier), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
    drawable.draw(c);
    return bitmap;
}
 
Example 12
Source File: RangeProgressBar.java    From RangeSeekBar with MIT License 5 votes vote down vote up
void drawTrack(Canvas canvas) {
    final Drawable d = mProgressDrawable;
    if (d != null) {
        // Translate canvas so a indeterminate circular progress bar with padding
        // rotates properly in its animation
        final int saveCount = canvas.save();

        if (isLayoutRtl() && mMirrorForRtl) {
            canvas.translate(getWidth() - mPaddingRight, mPaddingTop);
            canvas.scale(-1.0f, 1.0f);
        } else {
            canvas.translate(mPaddingLeft, mPaddingTop);
        }

        if (null != mProgressIndicatorBounds) {
            final int w = mComputedWidth - mProgressOffset;
            final int start = (int) (mVisualStartProgress * w);
            final int end = (int) (mVisualEndProgress * w);

            mProgressDrawableIndicator
                .setBounds(
                    start,
                    mProgressIndicatorBounds.top,
                    mProgressOffset + end,
                    mProgressIndicatorBounds.bottom
                );
        }

        d.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}
 
Example 13
Source File: ImageUtils.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
public static Bitmap getVectorBitmap(Context context, @DrawableRes int id) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), id, context.getTheme());
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        drawable = (DrawableCompat.wrap(drawable)).mutate();

    Bitmap result = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return result;
}
 
Example 14
Source File: RotatingProgressDrawable.java    From FloatingMusicMenu with Apache License 2.0 5 votes vote down vote up
private void circleBitmapFromDrawable(Drawable drawable) {
    Bitmap mBitmap;
    if (drawable instanceof ColorDrawable) {
        mBitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,
                COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
    } else {
        mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), BITMAP_CONFIG);
    }
    Canvas canvas = new Canvas(mBitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    circleBitmap(mBitmap);
}
 
Example 15
Source File: DragPreviewProvider.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the {@link #mView} into the given {@param destCanvas}.
 */
private void drawDragView(Canvas destCanvas) {
    destCanvas.save();
    if (mView instanceof TextView) {
        Drawable d = Workspace.getTextViewIcon((TextView) mView);
        Rect bounds = getDrawableBounds(d);
        destCanvas.translate(blurSizeOutline / 2 - bounds.left,
                blurSizeOutline / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        final Rect clipRect = mTempRect;
        mView.getDrawingRect(clipRect);

        boolean textVisible = false;
        if (mView instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) mView).getTextVisible()) {
                ((FolderIcon) mView).setTextVisible(false);
                textVisible = true;
            }
        }
        destCanvas.translate(-mView.getScrollX() + blurSizeOutline / 2,
                -mView.getScrollY() + blurSizeOutline / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        mView.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) mView).setTextVisible(true);
        }
    }
    destCanvas.restore();
}
 
Example 16
Source File: MoonView.java    From moon with GNU General Public License v3.0 5 votes vote down vote up
private void loadMoonImage(Context context) {
	Drawable monDrawable = context.getResources().getDrawable(
			R.drawable.moon);
	int w = monDrawable.getIntrinsicWidth();
	int h = monDrawable.getIntrinsicHeight();
	moonImage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
	Canvas canvas = new Canvas(moonImage);
	monDrawable.setBounds(0, 0, w, h);
	monDrawable.draw(canvas);
}
 
Example 17
Source File: MapImageUtils.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
  if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable) drawable).getBitmap();
  } else {
    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;
  }
}
 
Example 18
Source File: LayerDrawable.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    final ChildDrawable[] array = mLayerState.mChildren;
    final int N = mLayerState.mNum;
    for (int i = 0; i < N; i++) {
        final Drawable dr = array[i].mDrawable;
        if (dr != null) {
            dr.draw(canvas);
        }
    }
}
 
Example 19
Source File: CircleImageView.java    From gank.io-unofficial-android-client with Apache License 2.0 5 votes vote down vote up
private Bitmap getBitmapFromDrawable(Drawable drawable) {
  if (drawable == null) {
    return null;
  }

  if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable) drawable).getBitmap();
  }

  try {
    Bitmap bitmap;

    if (drawable instanceof ColorDrawable) {
      bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION,
          BITMAP_CONFIG);
    } else {
      bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
          BITMAP_CONFIG);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
  } catch (OutOfMemoryError e) {
    return null;
  }
}
 
Example 20
Source File: DragSortListView.java    From chromadoze with GNU General Public License v3.0 5 votes vote down vote up
private void drawDivider(int expPosition, Canvas canvas) {

        final Drawable divider = getDivider();
        final int dividerHeight = getDividerHeight();
        // Log.d("mobeta", "div="+divider+" divH="+dividerHeight);

        if (divider != null && dividerHeight != 0) {
            final ViewGroup expItem = (ViewGroup) getChildAt(expPosition
                    - getFirstVisiblePosition());
            if (expItem != null) {
                final int l = getPaddingLeft();
                final int r = getWidth() - getPaddingRight();
                final int t;
                final int b;

                final int childHeight = expItem.getChildAt(0).getHeight();

                if (expPosition > mSrcPos) {
                    t = expItem.getTop() + childHeight;
                    b = t + dividerHeight;
                } else {
                    b = expItem.getBottom() - childHeight;
                    t = b - dividerHeight;
                }
                // Log.d("mobeta", "l="+l+" t="+t+" r="+r+" b="+b);

                // Have to clip to support ColorDrawable on <= Gingerbread
                canvas.save();
                canvas.clipRect(l, t, r, b);
                divider.setBounds(l, t, r, b);
                divider.draw(canvas);
                canvas.restore();
            }
        }
    }