Skip to content

Instantly share code, notes, and snippets.

@ZionPi
Created February 14, 2016 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZionPi/4d3910f4889945c02474 to your computer and use it in GitHub Desktop.
Save ZionPi/4d3910f4889945c02474 to your computer and use it in GitHub Desktop.
Set NinePatchDrawable as background using Code
Bitmap bitmap = loadBitmapAsset("my_nine_patch_image.9.png", this);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap preview_bitmap = BitmapFactory.decodeStream(bs, null, options);
NinePatchDrawable drawable = NinePatchBitmapFactory
.createNinePatchDrawable(getResources(), preview_bitmap);//bitmap will be fine.
mTarget.setBackgroundDrawable(drawable);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment