android.graphics.drawable.VectorDrawable Java Examples

The following examples show how to use android.graphics.drawable.VectorDrawable. 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: ContextUtils.java    From kimai-android with MIT License 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

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

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #2
Source File: ContextUtils.java    From Stringlate with MIT License 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

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

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #3
Source File: ContextUtils.java    From openlauncher with Apache License 2.0 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

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

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #4
Source File: ContextUtils.java    From memetastic with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get a {@link Bitmap} out of a {@link Drawable}
 */
public Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;
    if (drawable instanceof VectorDrawableCompat
            || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
            || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable))) {

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

        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);
    } else if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }
    return bitmap;
}
 
Example #5
Source File: Utils.java    From MangoBloggerAndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static Bitmap getBitmap(Drawable drawable, int width, int height) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawableCompat) {
        return getBitmap((VectorDrawableCompat) drawable, width, height);
    } else if (drawable instanceof VectorDrawable) {
        return getBitmap((VectorDrawable) drawable, width, height);
    } else {
        throw new IllegalArgumentException("Unsupported drawable type");
    }
}
 
Example #6
Source File: ResourceUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawableCompat) {
        return getBitmap((VectorDrawableCompat) drawable);
    } else if (drawable instanceof VectorDrawable) {
        return getBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("Unsupported drawable type");
    }
}
 
Example #7
Source File: ResourceUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #8
Source File: TSnackbar.java    From TSnackBar with Apache License 2.0 5 votes vote down vote up
private static Bitmap getBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawable) {
        return getBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}
 
Example #9
Source File: TSnackbar.java    From TSnackBar with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #10
Source File: ResourceUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawableCompat) {
        return getBitmap((VectorDrawableCompat) drawable);
    } else if (drawable instanceof VectorDrawable) {
        return getBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("Unsupported drawable type");
    }
}
 
Example #11
Source File: ResourceUtil.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #12
Source File: BitmapHelper.java    From RxGpsService with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Bitmap getBitmap(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #13
Source File: BitmapHelper.java    From RxGpsService with Apache License 2.0 5 votes vote down vote up
public Bitmap getBitmap(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawable) {
        return getBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}
 
Example #14
Source File: App.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
public static Drawable getVecDrawable(Context context, @DrawableRes int id) {
    Drawable drawable = AppCompatResources.getDrawable(context, id);
    if (!(drawable instanceof VectorDrawableCompat || drawable instanceof VectorDrawable)) {
        throw new RuntimeException();
    }
    return drawable;
}
 
Example #15
Source File: Utils.java    From MangoBloggerAndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #16
Source File: SegmentedButton.java    From SegmentedButton with Apache License 2.0 5 votes vote down vote up
private Drawable readCompatDrawable(Context context, int drawableResId)
{
    Drawable drawable = AppCompatResources.getDrawable(context, drawableResId);

    // API 28 has a bug with vector drawables where the selected tint color is always applied to the drawable
    // To prevent this, the vector drawable is converted to a bitmap
    if ((VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
        || drawable instanceof VectorDrawableCompat)
    {
        Bitmap bitmap = getBitmapFromVectorDrawable(drawable);
        return new BitmapDrawable(context.getResources(), bitmap);
    }
    else
        return drawable;
}
 
Example #17
Source File: RoundedImageView.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

	Drawable drawable = getDrawable();

	if (drawable == null) {
		return;
	}

	if (getWidth() == 0 || getHeight() == 0) {
		return;
	}

	Bitmap b = null;
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
			&& drawable instanceof VectorDrawable) {
		((VectorDrawable) drawable).draw(canvas);
		b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas();
		c.setBitmap(b);
		drawable.draw(c);
	} else if (drawable instanceof BitmapDrawable){
		b = ((BitmapDrawable) drawable).getBitmap();
	} else if (drawable instanceof LayerDrawable) {
		LayerDrawable layerDrawable = (LayerDrawable) drawable;
		b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
		layerDrawable.draw(new Canvas(b));
	}
	Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
	int w = getWidth(), h = getHeight();

	Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);
	canvas.drawBitmap(roundBitmap, 0,0, null);
}
 
Example #18
Source File: ViewUtils.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get bitmap from resource
 */
public static Bitmap getBitmapFromResource(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (drawable instanceof BitmapDrawable) {
        return BitmapFactory.decodeResource(context.getResources(), drawableId);
    } else if (drawable instanceof VectorDrawable) {
        return getBitmapFromResource((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}
 
Example #19
Source File: ViewUtils.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap getBitmapFromResource(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #20
Source File: Helper.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
static private Bitmap getBitmap(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #21
Source File: Helper.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
static public Bitmap getBitmap(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (drawable instanceof BitmapDrawable) {
        return BitmapFactory.decodeResource(context.getResources(), drawableId);
    } else if (drawable instanceof VectorDrawable) {
        return getBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}
 
Example #22
Source File: FloatingLabelSpinner.java    From FloatingLabelSpinner with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Bitmap getVectorBitmap(VectorDrawable vectorDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
            vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vectorDrawable.draw(canvas);
    return bitmap;
}
 
Example #23
Source File: FloatingLabelSpinner.java    From FloatingLabelSpinner with Apache License 2.0 5 votes vote down vote up
private Bitmap createBitmap(Drawable drawable, Resources resources, int drawableId, BitmapFactory.Options options) {
    if (drawable instanceof BitmapDrawable) {
        return getBitmap(resources, drawableId, options);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) {
        return getVectorBitmap((VectorDrawable) drawable);
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}
 
Example #24
Source File: FloatingLabelSpinner.java    From FloatingLabelSpinner with Apache License 2.0 5 votes vote down vote up
public void setRightIcon(int drawableID, int clear_btn_width, int alpha) {
        this.rightIconSize = (short) clear_btn_width;

        iconPaint.setAlpha(alpha);
        Drawable drawable = ContextCompat.getDrawable(getContext(), drawableID);
//        Drawable drawable = VectorDrawableCompat.create(getResources(),drawableID,null) ;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap sampleBitmap = createBitmap(drawable, getResources(), drawableID, options);
        int sampleSize = 1;
        int width = options.outWidth;
        int height = options.outHeight;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) {
            width = drawable.getIntrinsicWidth();
            height = drawable.getIntrinsicHeight();
        }
        bitmap_height = (short) (height * clear_btn_width / width);
        int destinationHeight = bitmap_height;
        if (height > destinationHeight || width > clear_btn_width) {
            int halfHeight = height >> 1;
            int halfWidth = width >> 1;
            while ((halfHeight / sampleSize) > destinationHeight && (halfWidth / sampleSize) > clear_btn_width) {
                sampleSize *= 2;
            }
        }
        if (sampleBitmap != null)
            sampleBitmap.recycle();
        options.inSampleSize = sampleSize;
        options.inJustDecodeBounds = false;
        Bitmap oldBitmap = createBitmap(drawable, getResources(), drawableID, options);
        width = oldBitmap.getWidth();
        height = oldBitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleX = ((float) clear_btn_width / width);
        float scaleY = ((float) destinationHeight / height);
        matrix.postScale(scaleX, scaleY);
        rightIconBitmap = new SoftReference<>(
                Bitmap.createBitmap(oldBitmap, 0, 0, width, height, matrix, true)).get();
    }
 
Example #25
Source File: AmountColorizer.java    From fingen with Apache License 2.0 5 votes vote down vote up
public Drawable getTransactionIcon(int transactionType) {
    switch (transactionType) {
        case Transaction.TRANSACTION_TYPE_INCOME:
            return iconIncome;
        case Transaction.TRANSACTION_TYPE_EXPENSE:
            return iconExpense;
        case Transaction.TRANSACTION_TYPE_TRANSFER:
            return iconTransfer;
        default:
            return new VectorDrawable();
    }
}
 
Example #26
Source File: VectorDrawableCompat.java    From VectorChildFinder with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Drawable newDrawable(Resources res, Resources.Theme theme) {
    VectorDrawableCompat drawableCompat = new VectorDrawableCompat();
    drawableCompat.mDelegateDrawable =
            (VectorDrawable) mDelegateState.newDrawable(res, theme);
    return drawableCompat;
}
 
Example #27
Source File: VectorDrawableCompat.java    From VectorChildFinder with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Drawable newDrawable(Resources res) {
    VectorDrawableCompat drawableCompat = new VectorDrawableCompat();
    drawableCompat.mDelegateDrawable = (VectorDrawable) mDelegateState.newDrawable(res);
    return drawableCompat;
}
 
Example #28
Source File: VectorDrawableCompat.java    From VectorChildFinder with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Drawable newDrawable() {
    VectorDrawableCompat drawableCompat = new VectorDrawableCompat();
    drawableCompat.mDelegateDrawable = (VectorDrawable) mDelegateState.newDrawable();
    return drawableCompat;
}
 
Example #29
Source File: RoundedImageView.java    From Twire with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }

    Bitmap b = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && drawable instanceof VectorDrawable) {
        drawable.draw(canvas);
        b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas();
        c.setBitmap(b);
        drawable.draw(c);
    } else if (drawable instanceof BitmapDrawable) {
        b = ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable layerDrawable = (LayerDrawable) drawable;
        b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        layerDrawable.draw(new Canvas(b));
    }
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
    int w = getWidth(), h = getHeight();

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
    canvas.drawBitmap(roundBitmap, 0, 0, null);
}