Android : Use Android scaleGravity flags programmatically for ScaleDrawable constructor

on Friday, July 25, 2014


Problem:


How can I access the scaleGravity flag values programmatically?


Situation:


I am using ScaleDrawables in a custom View class.

These ScaleDrawables need to have their scaleGravity set to center.


If I was inflating my ScaleDrawables from XML I would use android:scaleGravity="center".


This sets the flag "center" which has the value 0x11 for the global attribute resource symbol scaleGravity.


I am however creating the ScaleDrawables dynamically, so I use the constructor which is of the form:



ScaleDrawable(Drawable drawable, int gravity, float scaleWidth, float scaleHeight)


The integer equivalent of the scaleGravity "center" flag value 0x11 is 17, so I can use:



ScaleDrawable scaleDrawable = new ScaleDrawable(sourceDrawable, 17, 1f, 1f);


Hard coding the value 17 isn't that good for readability though.

I could define constants for the custom View class:



private static final int CENTER = 17;


but this seems like unnecessary duplication.


So how can I get the flag values directly from android.R.attr.scaleGravity ?


0 comments:

Post a Comment