Example #1
0
  /**
   * Retrieve a dimensional unit attribute at <var>index</var> for use as an offset in raw pixels.
   * This is the same as {@link #getDimension}, except the returned value is converted to integer
   * pixels for you. An offset conversion involves simply truncating the base value to an integer.
   *
   * @param index Index of attribute to retrieve.
   * @param defValue Value to return if the attribute is not defined or not a resource.
   * @return Attribute dimension value multiplied by the appropriate metric and truncated to integer
   *     pixels, or defValue if not defined.
   * @see #getDimension
   * @see #getDimensionPixelSize
   */
  public int getDimensionPixelOffset(int index, int defValue) {
    index *= AssetManager.STYLE_NUM_ENTRIES;
    final int[] data = mData;
    final int type = data[index + AssetManager.STYLE_TYPE];
    if (type == TypedValue.TYPE_NULL) {
      return defValue;
    } else if (type == TypedValue.TYPE_DIMENSION) {
      return TypedValue.complexToDimensionPixelOffset(
          data[index + AssetManager.STYLE_DATA], mResources.mMetrics);
    }

    throw new UnsupportedOperationException(
        "Can't convert to dimension: type=0x" + Integer.toHexString(type));
  }
  @Override
  public int getDimensionPixelOffset(int id) throws NotFoundException {
    IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

    if (value != null) {
      String v = value.getValue();

      if (v != null) {
        if (ResourceHelper.stringToFloat(v, mTmpValue)
            && mTmpValue.type == TypedValue.TYPE_DIMENSION) {
          return TypedValue.complexToDimensionPixelOffset(mTmpValue.data, mMetrics);
        }
      }
    }

    // id was not found or not resolved. Throw a NotFoundException.
    throwException(id);

    // this is not used since the method above always throws
    return 0;
  }