下面列出了android.util.TypedValue#getDimension ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void setDeviceScaleFactor(float dipScale, WeakReference<Context> displayContext) {
mDeviceScaleFactor = dipScale;
// The wheel scroll factor depends on the theme in the context.
// This code assumes that the theme won't change between this call and
// getWheelScrollFactor().
Context context = displayContext.get();
TypedValue outValue = new TypedValue();
// This is the same attribute used by Android Views to scale wheel
// event motion into scroll deltas.
if (context != null && context.getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, outValue, true)) {
mWheelScrollFactor = outValue.getDimension(context.getResources().getDisplayMetrics());
} else {
// If attribute retrieval fails, just use a sensible default.
mWheelScrollFactor = 64 * mDeviceScaleFactor;
}
}
private int getListPreferredItemHeight() {
final TypedValue typedValue = new TypedValue();
// Resolve list item preferred height theme attribute into typedValue
getActivity().getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, typedValue, true);
// Create a new DisplayMetrics object
final DisplayMetrics metrics = new DisplayMetrics();
// Populate the DisplayMetrics
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
// Return theme value based on DisplayMetrics
return (int) typedValue.getDimension(metrics);
}
private int getListPreferredItemHeight() {
final TypedValue typedValue = new TypedValue();
// Resolve list item preferred height theme attribute into typedValue
getActivity().getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, typedValue, true);
// Create a new DisplayMetrics object
final DisplayMetrics metrics = new DisplayMetrics();
// Populate the DisplayMetrics
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
// Return theme value based on DisplayMetrics
return (int) typedValue.getDimension(metrics);
}
/**
* Gets the preferred height for each item in the ListView, in pixels, after accounting for
* screen density. ImageLoader uses this value to resize thumbnail images to match the ListView
* item height.
*
* @return The preferred height in pixels, based on the current theme.
*/
private int getListPreferredItemHeight() {
final TypedValue typedValue = new TypedValue();
// Resolve list item preferred height theme attribute into typedValue
getActivity().getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, typedValue, true);
// Create a new DisplayMetrics object
final DisplayMetrics metrics = new DisplayMetrics();
// Populate the DisplayMetrics
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
// Return theme value based on DisplayMetrics
return (int) typedValue.getDimension(metrics);
}
public static float getThemedDimen(@NonNull Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
if (theme.resolveAttribute(attr, typedValue, true)) {
return typedValue.getDimension(context.getResources().getDisplayMetrics());
}
return 0;
}
protected float getHorizontalScrollFactor() {
if (this.mHorizontalScrollFactor == 0.0f) {
TypedValue outValue = new TypedValue();
if (getContext().getTheme().resolveAttribute(2130771970, outValue, true)) {
this.mHorizontalScrollFactor = outValue.getDimension(getContext().getResources().getDisplayMetrics());
} else {
throw new IllegalStateException("Expected theme to define hlv_listPreferredItemWidth.");
}
}
return this.mHorizontalScrollFactor;
}
private float getVerticalScrollFactorCompat() {
if (this.mVerticalScrollFactor == 0.0f) {
TypedValue outValue = new TypedValue();
Context context = getContext();
if (context.getTheme().resolveAttribute(16842829, outValue, true)) {
this.mVerticalScrollFactor = outValue.getDimension(context.getResources().getDisplayMetrics());
} else {
throw new IllegalStateException("Expected theme to define listPreferredItemHeight.");
}
}
return this.mVerticalScrollFactor;
}
private float getVerticalScrollFactorCompat() {
if (mVerticalScrollFactor == 0) {
TypedValue outValue = new TypedValue();
final Context context = getContext();
if (!context.getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, outValue, true)) {
throw new IllegalStateException(
"Expected theme to define listPreferredItemHeight.");
}
mVerticalScrollFactor = outValue.getDimension(
context.getResources().getDisplayMetrics());
}
return mVerticalScrollFactor;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
final Activity ctx = (Activity)plugin.cordova;
final Item item = items.get(position);
IconTextView view;
if(convertView instanceof IconTextView)
{
view = (IconTextView)convertView;
view.Icon.setImageDrawable(item.Icon);
view.Text.setText(item.Text);
}
else
{
view = new IconTextView(ctx.getActionBar().getThemedContext(), item.Icon, item.Text);
}
// Get preferred list height
if(listPreferredItemHeight == -1)
{
DisplayMetrics metrics = new DisplayMetrics();
ctx.getWindowManager().getDefaultDisplay().getMetrics(metrics);
TypedValue value = new TypedValue();
ctx.getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
listPreferredItemHeight = (int)value.getDimension(metrics);
}
view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
return view;
}
/**
* Returns the pixel value of the dimension specified by {@code attributeResId}. Defaults to
* {@code defaultDimenResId} if {@code attributeResId} cannot be found or is not a dimension
* within the given {@code context}.
*/
@Px
public static int resolveDimension(
@NonNull Context context, @AttrRes int attributeResId, @DimenRes int defaultDimenResId) {
TypedValue dimensionValue = resolve(context, attributeResId);
if (dimensionValue == null || dimensionValue.type != TypedValue.TYPE_DIMENSION) {
return (int) context.getResources().getDimension(defaultDimenResId);
} else {
return (int) dimensionValue.getDimension(context.getResources().getDisplayMetrics());
}
}
/**
* Gets a scale factor that determines the distance the view should scroll
* vertically in response to {@link MotionEvent#ACTION_SCROLL}.
* @return The vertical scroll scale factor.
*/
protected float getVerticalScrollFactor() {
if (mVerticalScrollFactor == 0) {
Context context = getContext();
TypedValue outValue = new TypedValue();
if (!context.getTheme().resolveAttribute(16842829, outValue, true)) { // listPreferredItemHeight
throw new IllegalStateException(
"Expected theme to define listPreferredItemHeight.");
}
mVerticalScrollFactor = outValue.getDimension(
context.getResources().getDisplayMetrics());
}
return mVerticalScrollFactor;
}
private int calcSlideDistance() {
DisplayMetrics metrics = getResources().getDisplayMetrics();
int height = metrics.heightPixels;
TypedValue value = new TypedValue();
getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
float itemHeight = value.getDimension(metrics);
float paddingTop = getResources().getDimension(R.dimen.list_vertical_padding);
return (int) ((height - itemHeight + paddingTop) / 2);
}
private float getVerticalScrollFactorCompat() {
if (mVerticalScrollFactor == 0) {
TypedValue outValue = new TypedValue();
final Context context = getContext();
if (!context.getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, outValue, true)) {
throw new IllegalStateException(
"Expected theme to define listPreferredItemHeight.");
}
mVerticalScrollFactor = outValue.getDimension(
context.getResources().getDisplayMetrics());
}
return mVerticalScrollFactor;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference(PrefUtil.PREF_GRID_SIZE));
mAppContext = getActivity().getApplicationContext();
// get listPreferredItemHeight value in pixel and set it to mImageSize
TypedValue value = new TypedValue();
getActivity().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
mImageSize = (int) value.getDimension(getResources().getDisplayMetrics());
mImagePreference = (ImagePreference) findPreference(PrefUtil.PREF_DESIGN_IMAGE_URI);
mImagePreference.setOnPreferenceClickListener(this);
// load image if already set
Uri imageUri = PrefUtil.getDesignImageUri(mAppContext);
if (imageUri != null) {
loadDesignImage(imageUri);
}
// Set application version
Preference appVer = findPreference("pref_app_version");
appVer.setSummary(AppUtil.getVersion(mAppContext) + " - " + BuildConfig.BUILD_NUMBER);
}
private int getListPreferredItemHeight() {
final TypedValue typedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, typedValue, true);
final DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
return (int) typedValue.getDimension(metrics);
}
private int getListPreferredItemHeight() {
final TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(
android.R.attr.listPreferredItemHeight, typedValue, true);
final DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
return (int) typedValue.getDimension(metrics);
}
@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);
}
}
/**
* Gets a scale factor that determines the distance the view should scroll vertically in response to
* {@link MotionEvent#ACTION_SCROLL}.
*
* @return The vertical scroll scale factor.
*/
protected float getHorizontalScrollFactor() {
if ( mHorizontalScrollFactor == 0 ) {
TypedValue outValue = new TypedValue();
if ( !getContext().getTheme().resolveAttribute( R.attr.sephiroth_listPreferredItemWidth, outValue, true ) ) {
throw new IllegalStateException(
"Expected theme to define listPreferredItemHeight." );
}
mHorizontalScrollFactor = outValue.getDimension( getContext().getResources().getDisplayMetrics() );
}
return mHorizontalScrollFactor;
}
public static float getThemeDimen(Context context, int attr) {
Resources.Theme theme = context.getTheme();
TypedValue typedValueAttr = new TypedValue();
theme.resolveAttribute(attr, typedValueAttr, true);
return typedValueAttr.getDimension(context.getResources().getDisplayMetrics());
}
@SuppressLint("LongLogTag")
private void initThemeVars(Resources.Theme theme) {
DisplayMetrics dm = getResources().getDisplayMetrics();
// init theme params
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
primaryColor = typedValue.data;
theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
primaryDarkColor = typedValue.data;
theme.resolveAttribute(R.attr.autoDarkStatusBar, typedValue, true);
autoDarkStatusbar = typedValue.data != 0;
theme.resolveAttribute(R.attr.uniqueToolbarColor, typedValue, false);
uniqueToolbarColor = typedValue.data != 0;
theme.resolveAttribute(R.attr.drawerColor, typedValue, true);
drawerColor = typedValue.data;
/* theme.resolveAttribute(R.attr.dividerColor, typedValue, true);
dividerColor = typedValue.data;
*/
theme.resolveAttribute(R.attr.drawerWidth, typedValue, true);
drawerWidth = (int) typedValue.getDimension(dm);
//TypedValue.complexToDimensionPixelSize(typedValue.data, getResources().getDisplayMetrics());
if (drawerWidth < 0) {
drawerWidth = 1;
} else {
setDrawerStateListener(new DefaultDrawerListener(drawerWidth));
}
theme.resolveAttribute(R.attr.dividerStrokeWidth, typedValue, true);
dividerStrokeThickness = (int) typedValue.getDimension(dm);
//TypedValue.complexToDimensionPixelSize(typedValue.data, getResources().getDisplayMetrics());
if (dividerStrokeThickness < 0) {
dividerStrokeThickness = 1;
}
theme.resolveAttribute(R.attr.belowToolbar, typedValue, true);
belowToolbar = typedValue.data != 0;
theme.resolveAttribute(R.attr.multipaneSupport, typedValue, false);
multiPaneSupport = typedValue.data != 0;
theme.resolveAttribute(R.attr.titleColor, typedValue, true);
theme.resolveAttribute(R.attr.headItemStyle, typedValue, true);
// Values of the NavigationDrawer
TypedArray valuesNavigationDrawer = theme.obtainStyledAttributes(typedValue.resourceId, R.styleable.MaterialNavigationDrawer);
dividerColor = valuesNavigationDrawer.getDrawable(R.styleable.MaterialNavigationDrawer_dividerColor);
// Values of the Account
TypedArray values = theme.obtainStyledAttributes(typedValue.resourceId, R.styleable.MaterialHeadItem);
theme.resolveAttribute(R.attr.actionBarOverlay, typedValue, false);
actionBarOverlay = typedValue.data != 0;
theme.resolveAttribute(R.attr.actionBarOverlayAlpha, typedValue, false);
actionBarOverlayAlpha = typedValue.getFloat();
//actionbar style customization
theme.resolveAttribute(R.attr.drawerActionBarStyle, typedValue, true);
drawerActionBarStyle = typedValue.data;
}