下面列出了android.util.TypedValue#TYPE_DIMENSION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Retrieve a dimensional unit attribute at <var>index</var>. Unit
* conversions are based on the current {@link DisplayMetrics}
* associated with the resources this {@link TypedArray} object
* came from.
* <p>
* This method will throw an exception if the attribute is defined but is
* not a dimension.
*
* @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, or defValue if not defined.
* @throws RuntimeException if the TypedArray has already been recycled.
* @throws UnsupportedOperationException if the attribute is defined but is
* not an integer.
*
* @see #getDimensionPixelOffset
* @see #getDimensionPixelSize
*/
public float getDimension(@StyleableRes int index, float defValue) {
if (mRecycled) {
throw new RuntimeException("Cannot make calls to a recycled instance!");
}
final int attrIndex = index;
index *= STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index + STYLE_TYPE];
if (type == TypedValue.TYPE_NULL) {
return defValue;
} else if (type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimension(data[index + STYLE_DATA], mMetrics);
} else if (type == TypedValue.TYPE_ATTRIBUTE) {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to dimension: type=0x" + Integer.toHexString(type));
}
/**
* 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.
* <p>
* This method will throw an exception if the attribute is defined but is
* not a dimension.
*
* @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.
* @throws RuntimeException if the TypedArray has already been recycled.
* @throws UnsupportedOperationException if the attribute is defined but is
* not an integer.
*
* @see #getDimension
* @see #getDimensionPixelSize
*/
public int getDimensionPixelOffset(@StyleableRes int index, int defValue) {
if (mRecycled) {
throw new RuntimeException("Cannot make calls to a recycled instance!");
}
final int attrIndex = index;
index *= STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index + STYLE_TYPE];
if (type == TypedValue.TYPE_NULL) {
return defValue;
} else if (type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimensionPixelOffset(data[index + STYLE_DATA], mMetrics);
} else if (type == TypedValue.TYPE_ATTRIBUTE) {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to dimension: type=0x" + Integer.toHexString(type));
}
/**
* Retrieve a dimensional unit attribute at <var>index</var> for use
* as a size in raw pixels. This is the same as
* {@link #getDimension}, except the returned value is converted to
* integer pixels for use as a size. A size conversion involves
* rounding the base value, and ensuring that a non-zero base value
* is at least one pixel in size.
* <p>
* This method will throw an exception if the attribute is defined but is
* not a dimension.
*
* @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.
* @throws RuntimeException if the TypedArray has already been recycled.
* @throws UnsupportedOperationException if the attribute is defined but is
* not a dimension.
*
* @see #getDimension
* @see #getDimensionPixelOffset
*/
public int getDimensionPixelSize(@StyleableRes int index, int defValue) {
if (mRecycled) {
throw new RuntimeException("Cannot make calls to a recycled instance!");
}
final int attrIndex = index;
index *= STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index + STYLE_TYPE];
if (type == TypedValue.TYPE_NULL) {
return defValue;
} else if (type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimensionPixelSize(data[index + STYLE_DATA], mMetrics);
} else if (type == TypedValue.TYPE_ATTRIBUTE) {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
}
throw new UnsupportedOperationException("Can't convert value at index " + attrIndex
+ " to dimension: type=0x" + Integer.toHexString(type));
}
/**
* Special version of {@link #getDimensionPixelSize} for retrieving
* {@link android.view.ViewGroup}'s layout_width and layout_height
* attributes. This is only here for performance reasons; applications
* should use {@link #getDimensionPixelSize}.
* <p>
* This method will throw an exception if the attribute is defined but is
* not a dimension or integer (enum).
*
* @param index Index of the attribute to retrieve.
* @param name Textual name of attribute for error reporting.
*
* @return Attribute dimension value multiplied by the appropriate
* metric and truncated to integer pixels.
* @throws RuntimeException if the TypedArray has already been recycled.
* @throws UnsupportedOperationException if the attribute is defined but is
* not a dimension or integer (enum).
*/
public int getLayoutDimension(@StyleableRes int index, String name) {
if (mRecycled) {
throw new RuntimeException("Cannot make calls to a recycled instance!");
}
final int attrIndex = index;
index *= STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index + STYLE_TYPE];
if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) {
return data[index + STYLE_DATA];
} else if (type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimensionPixelSize(data[index + STYLE_DATA], mMetrics);
} else if (type == TypedValue.TYPE_ATTRIBUTE) {
final TypedValue value = mValue;
getValueAt(index, value);
throw new UnsupportedOperationException(
"Failed to resolve attribute at index " + attrIndex + ": " + value);
}
throw new UnsupportedOperationException(getPositionDescription()
+ ": You must supply a " + name + " attribute.");
}
@NonNull
private static CornerSize getCornerSize(
TypedArray a, int index, @NonNull CornerSize defaultValue) {
TypedValue value = a.peekValue(index);
if (value == null) {
return defaultValue;
}
if (value.type == TypedValue.TYPE_DIMENSION) {
// Eventually we might want to change this to call getDimension() since corner sizes support
// floats.
return new AbsoluteCornerSize(
TypedValue.complexToDimensionPixelSize(value.data, a.getResources().getDisplayMetrics()));
} else if (value.type == TypedValue.TYPE_FRACTION) {
return new RelativeCornerSize(value.getFraction(1.0f, 1.0f));
} else {
return defaultValue;
}
}
private static boolean isPxVal(TypedValue val)
{
if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX)
{
return true;
}
return false;
}
private static String getAttributeValue(XmlResourceParser parser, int index) {
int type = parser.getAttributeValueType(index);
int data = parser.getAttributeValueData(index);
if (type == TypedValue.TYPE_STRING) {
return parser.getAttributeValue(index);
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
return String.format("?%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_REFERENCE) {
return String.format("@%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_FLOAT) {
return String.valueOf(Float.intBitsToFloat(data));
}
if (type == TypedValue.TYPE_INT_HEX) {
return String.format("0x%08X", data);
}
if (type == TypedValue.TYPE_INT_BOOLEAN) {
return data != 0 ? "true" : "false";
}
if (type == TypedValue.TYPE_DIMENSION) {
return Float.toString(complexToFloat(data))
+ DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type == TypedValue.TYPE_FRACTION) {
return Float.toString(complexToFloat(data))
+ FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT
&& type <= TypedValue.TYPE_LAST_COLOR_INT) {
return String.format("#%08X", data);
}
if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) {
return String.valueOf(data);
}
return String.format("<0x%X, type 0x%02X>", data, type);
}
static float getDimensionOrFraction(TypedArray a, int index, int base, float defValue) {
TypedValue value = a.peekValue(index);
if (value == null) return defValue;
if (value.type == TypedValue.TYPE_DIMENSION) {
return a.getDimensionPixelOffset(index, Math.round(defValue));
} else if (value.type == TypedValue.TYPE_FRACTION) {
// Round it to avoid values like 47.9999 from getting truncated
//return Math.round(a.getFraction(index, base, base, defValue));
return a.getFraction(index, base, base, defValue);
}
return defValue;
}
private static String getAttributeValue(XmlResourceParser parser, int index) {
int type = parser.getAttributeValueType(index);
int data = parser.getAttributeValueData(index);
if (type == TypedValue.TYPE_STRING) {
return parser.getAttributeValue(index);
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
return String.format("?%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_REFERENCE) {
return String.format("@%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_FLOAT) {
return String.valueOf(Float.intBitsToFloat(data));
}
if (type == TypedValue.TYPE_INT_HEX) {
return String.format("0x%08X", data);
}
if (type == TypedValue.TYPE_INT_BOOLEAN) {
return data != 0 ? "true" : "false";
}
if (type == TypedValue.TYPE_DIMENSION) {
return Float.toString(complexToFloat(data)) + DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type == TypedValue.TYPE_FRACTION) {
return Float.toString(complexToFloat(data)) + FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
return String.format("#%08X", data);
}
if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
return String.valueOf(data);
}
if (type == 0x07) {
return String.format("@%s%08X", getPackage(data), data);
}
return String.format("<0x%X, type 0x%02X>", data, type);
}
private static boolean isPxVal(TypedValue val) {
if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX) {
return true;
}
return false;
}
private static String getAttributeValue(AXmlResourceParser parser, int index) {
int type = parser.getAttributeValueType(index);
int data = parser.getAttributeValueData(index);
if (type == TypedValue.TYPE_STRING) {
return parser.getAttributeValue(index);
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
return String.format("?%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_REFERENCE) {
return String.format("@%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_FLOAT) {
return String.valueOf(Float.intBitsToFloat(data));
}
if (type == TypedValue.TYPE_INT_HEX) {
return String.format("0x%08X", data);
}
if (type == TypedValue.TYPE_INT_BOOLEAN) {
return data == 0 ? "false" : "true";
}
if (type == TypedValue.TYPE_DIMENSION) {
return Float.toString(complexToFloat(data)) +
DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type == TypedValue.TYPE_FRACTION) {
return Float.toString(complexToFloat(data)) +
FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
return String.format("#%08X", data);
}
if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
return String.valueOf(data);
}
return String.format("<0x%X, type 0x%02X>", data, type);
}
public static boolean isPxVal(TypedValue val)
{
if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX)
{
return true;
}
return false;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
boolean measure = false;
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);
final TypedValue tv = isPortrait ? mMinWidthMinor : mMinWidthMajor;
if (tv.type != TypedValue.TYPE_NULL) {
final int min;
if (tv.type == TypedValue.TYPE_DIMENSION) {
min = (int)tv.getDimension(metrics);
} else if (tv.type == TypedValue.TYPE_FRACTION) {
min = (int)tv.getFraction(metrics.widthPixels, metrics.widthPixels);
} else {
min = 0;
}
if (width < min) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY);
measure = true;
}
}
// TODO: Support height?
if (measure) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public ParallaxViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
mParallaxTransformer = new ParallaxTransformer();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ParallaxViewPager, 0,
0);
mMode = Mode.values()[a.getInt(R.styleable.ParallaxViewPager_mode, 0)];
setMode(mMode);
if (a.hasValue(R.styleable.ParallaxViewPager_right_shadow))
{
mRightShadow = a.getDrawable(R.styleable.ParallaxViewPager_right_shadow);
}
if (a.hasValue(R.styleable.ParallaxViewPager_left_shadow))
{
mLeftShadow = a.getDrawable(R.styleable.ParallaxViewPager_left_shadow);
}
mShadowWidth = a.getDimensionPixelSize(R.styleable.ParallaxViewPager_shadow_width, (int) dp2px(20, getContext()));
TypedValue tv = a.peekValue(R.styleable.ParallaxViewPager_outset);
if (tv != null)
{
if (tv.type == TypedValue.TYPE_FRACTION)
{
mOutsetFraction = a.getFraction(R.styleable.ParallaxViewPager_outset, 1, 1, 0);
setOutsetFraction(mOutsetFraction);
} else if (tv.type == TypedValue.TYPE_DIMENSION)
{
mOutset = (int) TypedValue.complexToDimension(tv.data, getResources().getDisplayMetrics());
setOutset(mOutset);
}
}
final int resID = a.getResourceId(R.styleable.ParallaxViewPager_interpolator, 0);
if (resID > 0)
{
setInterpolator(context, resID);
}
a.recycle();
}
public ResScalarValue factory(int type, int value, String rawValue) throws IOException {
switch (type) {
case TypedValue.TYPE_NULL:
return new ResReferenceValue(mPackage, 0, null);
case TypedValue.TYPE_REFERENCE:
return newReference(value, rawValue);
case TypedValue.TYPE_ATTRIBUTE:
return newReference(value, rawValue, true);
case TypedValue.TYPE_STRING:
return new ResStringValue(rawValue, value);
case TypedValue.TYPE_FLOAT:
return new ResFloatValue(Float.intBitsToFloat(value), value, rawValue);
case TypedValue.TYPE_DIMENSION:
return new ResDimenValue(value, rawValue);
case TypedValue.TYPE_FRACTION:
return new ResFractionValue(value, rawValue);
case TypedValue.TYPE_INT_BOOLEAN:
return new ResBoolValue(value != 0, value, rawValue);
case 0x07:
return newReference(value, rawValue);
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
return new ResColorValue(value, rawValue);
}
if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
return new ResIntValue(value, rawValue, type);
}
throw new IOException("Invalid value type: " + type);
}
static int getDimensionOrFraction(TypedArray a, int index, int base, int defValue) {
TypedValue value = a.peekValue(index);
if (value == null) return defValue;
if (value.type == TypedValue.TYPE_DIMENSION) {
return a.getDimensionPixelOffset(index, defValue);
} else if (value.type == TypedValue.TYPE_FRACTION) {
// Round it to avoid values like 47.9999 from getting truncated
return Math.round(a.getFraction(index, base, base, defValue));
}
return defValue;
}
public static boolean isDimensionValue(final TypedValue v) {
return v.type == TypedValue.TYPE_DIMENSION;
}
public static boolean isDimensionValue(final TypedValue v) {
return v.type == TypedValue.TYPE_DIMENSION;
}
public MenuStyle(Context context, int resID){
TypedArray a = context.obtainStyledAttributes(resID, R.styleable.SlideMenuStyle);
for (int i = 0, count = a.getIndexCount(); i < count; i++){
int attr = a.getIndex(i);
switch (attr){
case R.styleable.SlideMenuStyle_sm_overDrag:
mOverDrag = a.getBoolean(attr, false);
break;
case R.styleable.SlideMenuStyle_sm_menuBorder:
TypedValue value = a.peekValue(attr);
if(value.type == TypedValue.TYPE_DIMENSION)
mMenuBorder = a.getDimensionPixelSize(attr, 50);
else
mMenuBorderPercent = Math.max(0f, Math.min(1f, a.getFloat(attr, 0f)));
break;
case R.styleable.SlideMenuStyle_sm_menuOverDragBorder:
value = a.peekValue(attr);
if(value.type == TypedValue.TYPE_DIMENSION)
mMenuOverDragBorder = a.getDimensionPixelSize(attr, 50);
else
mMenuOverDragBorderPercent = Math.max(0f, Math.min(1f, a.getFloat(attr, 0f)));
break;
case R.styleable.SlideMenuStyle_sm_slideRatio:
mSlideRatio = a.getFloat(attr, 0.5f);
break;
case R.styleable.SlideMenuStyle_sm_menuShadow:
mMenuShadow = a.getDimensionPixelSize(attr, 10);
break;
case R.styleable.SlideMenuStyle_sm_dragEdge:
value = a.peekValue(attr);
if(value.type == TypedValue.TYPE_DIMENSION)
mDragEdge = a.getDimensionPixelSize(attr, 30);
else{
mDragEdge = a.getInt(attr, -1);
if(mDragEdge == -1)
mDragEdge = Integer.MAX_VALUE;
}
break;
case R.styleable.SlideMenuStyle_sm_touchSlop:
mTouchSlop = a.getDimensionPixelSize(attr, 30);
break;
case R.styleable.SlideMenuStyle_sm_maxDim:
mMaxDim = Math.max(0f, Math.min(1f, a.getFloat(attr, 0f)));
break;
case R.styleable.SlideMenuStyle_sm_velocitySlop:
mVelocitySlop = Math.max(500f, a.getFloat(attr, 500f));
break;
case R.styleable.SlideMenuStyle_sm_closeEdge:
value = a.peekValue(attr);
if(value.type == TypedValue.TYPE_DIMENSION)
mCloseEdge = a.getDimensionPixelSize(attr, 50);
else
mCloseEdgePercent = Math.max(0f, Math.min(1f, a.getFloat(attr, 0.75f)));
break;
case R.styleable.SlideMenuStyle_sm_animDuration:
mAnimDuration = Math.max(0, a.getInt(attr, 1000));
break;
case R.styleable.SlideMenuStyle_sm_animInterpolator:
mInterpolatorId = a.getResourceId(attr, 0);
break;
}
}
a.recycle();
}
/**
* Retrieve a dimensional for a particular resource ID. Unit
* conversions are based on the current {@link DisplayMetrics} associated
* with the resources.
*
* @param id The desired resource identifier, as generated by the aapt
* tool. This integer encodes the package, type, and resource
* entry. The value 0 is an invalid identifier.
*
* @return Resource dimension value multiplied by the appropriate
* metric.
*
* @throws NotFoundException Throws NotFoundException if the given ID does not exist.
*
* @see #getDimensionPixelOffset
* @see #getDimensionPixelSize
*/
public float getDimension(@DimenRes int id) throws NotFoundException {
final TypedValue value = obtainTempTypedValue();
try {
final ResourcesImpl impl = mResourcesImpl;
impl.getValue(id, value, true);
if (value.type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimension(value.data, impl.getDisplayMetrics());
}
throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id)
+ " type #0x" + Integer.toHexString(value.type) + " is not valid");
} finally {
releaseTempTypedValue(value);
}
}