diff options
author | Steve Slaven <bpk@hoopajoo.net> | 2011-01-06 17:22:50 (GMT) |
---|---|---|
committer | Steve Slaven <bpk@hoopajoo.net> | 2011-01-06 17:22:50 (GMT) |
commit | ada798c9a9234a6070ffa2c617c191b1f881999d (patch) | |
tree | cc04579c24bf9b6bc2d442eb547428dd73347a3b /src/net | |
parent | dca95469c23583f8dc824859480cb21eb8fc9e8e (diff) | |
download | SoftKeys-ada798c9a9234a6070ffa2c617c191b1f881999d.zip SoftKeys-ada798c9a9234a6070ffa2c617c191b1f881999d.tar.gz SoftKeys-ada798c9a9234a6070ffa2c617c191b1f881999d.tar.bz2 |
Get width and height early for non-bitmap drawables
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/hoopajoo/android/SoftKeys/Generator.java | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/src/net/hoopajoo/android/SoftKeys/Generator.java b/src/net/hoopajoo/android/SoftKeys/Generator.java index 1c0d5fc..274dc00 100644 --- a/src/net/hoopajoo/android/SoftKeys/Generator.java +++ b/src/net/hoopajoo/android/SoftKeys/Generator.java @@ -216,32 +216,47 @@ public class Generator { // this will return a new drawable scaled to the new size, so you don't have to mutable the source public static Drawable resizeImage( Drawable d, int w, int h) { - Bitmap b; - if( d instanceof BitmapDrawable ) { - // I found that the resources are already bitmapdrawables so we can do this, - // I assume it it's not created from a bitmap like it's a shape or something - // then this won't work? - b = ((BitmapDrawable)d).getBitmap(); - }else{ - // this was the way more people said to do it, just render the drawable to a canvas - // backed by your dest bitmap. I assume if you're using a bitmapdrawable - // then this is slower than just pulling in the drawable backed bitmap - d.mutate(); // we change the setbounds() so lets not mess with the original - b = Bitmap.createBitmap( w, h, Config.ARGB_8888 ); - Canvas c = new Canvas( b ); - d.setBounds( 0, 0, w, h ); - d.draw( c ); + int width = d.getIntrinsicWidth(); + int height = d.getIntrinsicHeight(); + + // catch colors/etc + if( width < 1 ) { + width = 1; + } + + if( height < 1 ) { + height = 1; } - - int width = b.getWidth(); - int height = b.getHeight(); // if w/h is zero them it means to scale based on the non-zero one and // maintain aspect if( w == 0 ) { - w = (int)((float)h * width / height); + w = (int)( (float)h * width / height ); }else if( h == 0 ) { - h = (int)((float)w * height / width); + h = (int)( (float)w * height / width ); + } + + Bitmap b; + if( d instanceof BitmapDrawable ) { + // I found that the resources are already bitmapdrawables so we can + // do this, + // I assume it it's not created from a bitmap like it's a shape or + // something + // then this won't work? + b = ( (BitmapDrawable)d ).getBitmap(); + }else { + // this was the way more people said to do it, just render the + // drawable to a canvas + // backed by your dest bitmap. I assume if you're using a + // bitmapdrawable + // then this is slower than just pulling in the drawable backed + // bitmap + d.mutate(); // we change the setbounds() so lets not mess with the + // original + b = Bitmap.createBitmap( width, height, Config.ARGB_8888 ); + Canvas c = new Canvas( b ); + d.setBounds( 0, 0, width, height ); + d.draw( c ); } float scaleWidth = ((float) w) / width; |