GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BetterVectorDrawable/docs/BitmapUtil.md at master · a-student/BetterVectorDrawable

Expand file tree

/

BitmapUtil.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
25 lines (22 loc) · 1.45 KB
/

BitmapUtil.md

Copy path

File metadata and controls

25 lines (22 loc) · 1.45 KB
Raw
Copy raw file
Download raw file
Edit and raw actions

Converting vector drawable to Bitmap

The library contains the util class to allow you to convert almost any drawable to a Bitmap. For example:

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Bitmap bitmap = BitmapUtil.toBitmap(sourceDrawable, metrics, 60f, 120f);

That snippet will convert sourceDrawable to a Bitmap with size depending on device screen density.

You can also set size only for one side then the other side will match aspect ratio if the Drawable's getIntrinsicWidth() and getIntrinsicHeight() return values greater than zero. For example:

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Bitmap bitmap = BitmapUtil.toBitmap(sourceDrawable, metrics, 60f, 0);

Result Drawable will have 60dp width and the height is determined by its aspect ratio.

For cases when you are not sure that Drawable's getIntrinsicWidth() and getIntrinsicHeight() return values greater than zero you can provide fallback values that will be used if method fails to acquire the aspect ratio of source drawable. For example:

GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.BLUE);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Bitmap bitmap = BitmapUtil.toBitmap(gradientDrawable, metrics, 60f, 0, 60f, 60f);

In that case resultant Bitmap will have 60dp x 60dp size, because GradientDrawable#getIntrinsicHeight() returns -1.

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.