Skip to content

Commit

Permalink
Rename VectorDrawableHelper -> DrawableHelper. Add method for set Dra…
Browse files Browse the repository at this point in the history
…wable as icon.
  • Loading branch information
hluhovskyi committed Jan 24, 2018
1 parent 06d056a commit 98dd5b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;

final class VectorDrawableHelper {
final class DrawableHelper {

static Bitmap getBitmap(Context context, int drawableId) {
static Bitmap getBitmap(Context context, @DrawableRes int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
return getBitmap(drawable);
}

static Bitmap getBitmap(Drawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
Expand Down Expand Up @@ -397,7 +398,11 @@ public void setSecondRadius(float radius) {
}

public void setIcon(@DrawableRes int drawableRes) {
setIcon(VectorDrawableHelper.getBitmap(getContext(), drawableRes));
setIcon(DrawableHelper.getBitmap(getContext(), drawableRes));
}

public void setIcon(Drawable drawable) {
setIcon(DrawableHelper.getBitmap(drawable));
}

public void setIcon(Bitmap bitmap) {
Expand Down

0 comments on commit 98dd5b0

Please sign in to comment.