public static boolean parseFloatAttribute ( String attribute , String value , TypedValue outValue , boolean requireUnit ) { assert requireUnit == false || attribute != null ; // remove the space before and after value = value . trim ( ) ; int len = value . length ( ) ; if ( len <= 0 ) { return false ; } // check that there's no non ascii characters. char [ ] buf = value . toCharArray ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( buf [ i ] > 255 ) { return false ; } } // check the first character if ( buf [ 0 ] < ' ' && buf [ 0 ] > ' ' && buf [ 0 ] != ' ' && buf [ 0 ] != ' ' ) { return false ; } // now look for the string that is after the float... Matcher m = sFloatPattern . matcher ( value ) ; if ( m . matches ( ) ) { String f_str = m . group ( 1 ) ; String end = m . group ( 2 ) ; float f ; try { f = Float . parseFloat ( f_str ) ; } catch ( NumberFormatException e ) { // this shouldn't happen with the regexp above. return false ; } if ( end . length ( ) > 0 && end . charAt ( 0 ) != ' ' ) { // Might be a unit... if ( parseUnit ( end , outValue , sFloatOut ) ) { computeTypedValue ( outValue , f , sFloatOut [ 0 ] ) ; return true ; } return false ; } // make sure it's only spaces at the end. end = end . trim ( ) ; if ( end . length ( ) == 0 ) { if ( outValue != null ) { outValue . assetCookie = 0 ; outValue . string = null ; if ( requireUnit == false ) { outValue . type = TypedValue . TYPE_FLOAT ; outValue . data = Float . floatToIntBits ( f ) ; } else { // no unit when required? Use dp and out an error. applyUnit ( sUnitNames [ 1 ] , outValue , sFloatOut ) ; computeTypedValue ( outValue , f , sFloatOut [ 0 ] ) ; System . out . println ( String . format ( "Dimension \"%1$s\" in attribute \"%2$s\" is missing unit!" , value , attribute ) ) ; } return true ; } } } return false ; }
Parse a float attribute and return the parsed value into a given TypedValue .