Example usage for android.util TypedValue getDimension

List of usage examples for android.util TypedValue getDimension

Introduction

In this page you can find the example usage for android.util TypedValue getDimension.

Prototype

public float getDimension(DisplayMetrics metrics) 

Source Link

Document

Return the data for this value as a dimension.

Usage

From source file:devlight.io.library.ArcProgressStackView.java

public ArcProgressStackView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // Init CPSV//from  w  w w .  ja  va2  s  . c  o m

    // Always draw
    setWillNotDraw(false);
    setLayerType(LAYER_TYPE_SOFTWARE, null);
    ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, null);

    // Detect if features available
    mIsFeaturesAvailable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;

    // Retrieve attributes from xml
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcProgressStackView);
    try {
        setIsAnimated(typedArray.getBoolean(R.styleable.ArcProgressStackView_apsv_animated, true));
        setIsShadowed(typedArray.getBoolean(R.styleable.ArcProgressStackView_apsv_shadowed, true));
        setIsRounded(typedArray.getBoolean(R.styleable.ArcProgressStackView_apsv_rounded, false));
        setIsDragged(typedArray.getBoolean(R.styleable.ArcProgressStackView_apsv_dragged, false));
        setIsLeveled(typedArray.getBoolean(R.styleable.ArcProgressStackView_apsv_leveled, false));
        setTypeface(typedArray.getString(R.styleable.ArcProgressStackView_apsv_typeface));
        setTextColor(typedArray.getColor(R.styleable.ArcProgressStackView_apsv_text_color, Color.WHITE));
        setShadowRadius(typedArray.getDimension(R.styleable.ArcProgressStackView_apsv_shadow_radius,
                DEFAULT_SHADOW_RADIUS));
        setShadowDistance(typedArray.getDimension(R.styleable.ArcProgressStackView_apsv_shadow_distance,
                DEFAULT_SHADOW_DISTANCE));
        setShadowAngle(typedArray.getInteger(R.styleable.ArcProgressStackView_apsv_shadow_angle,
                (int) DEFAULT_SHADOW_ANGLE));
        setShadowColor(
                typedArray.getColor(R.styleable.ArcProgressStackView_apsv_shadow_color, DEFAULT_SHADOW_COLOR));
        setAnimationDuration(typedArray.getInteger(R.styleable.ArcProgressStackView_apsv_animation_duration,
                DEFAULT_ANIMATION_DURATION));
        setStartAngle(typedArray.getInteger(R.styleable.ArcProgressStackView_apsv_start_angle,
                (int) DEFAULT_START_ANGLE));
        setSweepAngle(typedArray.getInteger(R.styleable.ArcProgressStackView_apsv_sweep_angle,
                (int) DEFAULT_SWEEP_ANGLE));
        setProgressModelOffset(typedArray.getDimension(R.styleable.ArcProgressStackView_apsv_model_offset,
                DEFAULT_MODEL_OFFSET));
        setModelBgEnabled(typedArray.getBoolean(R.styleable.ArcProgressStackView_apsv_model_bg_enabled, false));

        // Set orientation
        final int orientationOrdinal = typedArray
                .getInt(R.styleable.ArcProgressStackView_apsv_indicator_orientation, 0);
        setIndicatorOrientation(
                orientationOrdinal == 0 ? IndicatorOrientation.VERTICAL : IndicatorOrientation.HORIZONTAL);

        // Retrieve interpolator
        Interpolator interpolator = null;
        try {
            final int interpolatorId = typedArray
                    .getResourceId(R.styleable.ArcProgressStackView_apsv_interpolator, 0);
            interpolator = interpolatorId == 0 ? null
                    : AnimationUtils.loadInterpolator(context, interpolatorId);
        } catch (Resources.NotFoundException exception) {
            interpolator = null;
            exception.printStackTrace();
        } finally {
            setInterpolator(interpolator);
        }

        // Set animation info if is available
        if (mIsFeaturesAvailable) {
            mProgressAnimator.setFloatValues(MIN_FRACTION, MAX_FRACTION);
            mProgressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(final ValueAnimator animation) {
                    mAnimatedFraction = (float) animation.getAnimatedValue();
                    if (mAnimatorUpdateListener != null)
                        mAnimatorUpdateListener.onAnimationUpdate(animation);

                    postInvalidate();
                }
            });
        }

        // Check whether draw width dimension or fraction
        if (typedArray.hasValue(R.styleable.ArcProgressStackView_apsv_draw_width)) {
            final TypedValue drawWidth = new TypedValue();
            typedArray.getValue(R.styleable.ArcProgressStackView_apsv_draw_width, drawWidth);
            if (drawWidth.type == TypedValue.TYPE_DIMENSION)
                setDrawWidthDimension(drawWidth.getDimension(context.getResources().getDisplayMetrics()));
            else
                setDrawWidthFraction(drawWidth.getFraction(MAX_FRACTION, MAX_FRACTION));
        } else
            setDrawWidthFraction(DEFAULT_DRAW_WIDTH_FRACTION);

        // Set preview models
        if (isInEditMode()) {
            String[] preview = null;
            try {
                final int previewId = typedArray
                        .getResourceId(R.styleable.ArcProgressStackView_apsv_preview_colors, 0);
                preview = previewId == 0 ? null : typedArray.getResources().getStringArray(previewId);
            } catch (Exception exception) {
                preview = null;
                exception.printStackTrace();
            } finally {
                if (preview == null)
                    preview = typedArray.getResources().getStringArray(R.array.default_preview);

                final Random random = new Random();
                for (String previewColor : preview)
                    mModels.add(
                            new Model("", random.nextInt((int) MAX_PROGRESS), Color.parseColor(previewColor)));
                measure(mSize, mSize);
            }

            // Set preview model bg color
            mPreviewModelBgColor = typedArray.getColor(R.styleable.ArcProgressStackView_apsv_preview_bg,
                    Color.LTGRAY);
        }
    } finally {
        typedArray.recycle();
    }
}

From source file:android.com.example.contactslist.ui.ContactsListFragment.java

/**
 * 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.// w w w  .ja  v a  2  s  .com
 *
 * @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);
}

From source file:br.com.mybaby.contatos.ContactsListFragment.java

/**
 * 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./*from   w w w .ja v a  2 s .c  om*/
 *
 * @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 android.util.DisplayMetrics();

    // Populate the DisplayMetrics
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    // Return theme value based on DisplayMetrics
    return (int) typedValue.getDimension(metrics);
}

From source file:android.support.v7.app.AppCompatDelegateImplV7.java

private void applyFixedSizeWindow() {
    TypedArray a = mContext.obtainStyledAttributes(R.styleable.Theme);

    TypedValue mFixedWidthMajor = null;//from w w  w .  j  a va2 s. c  o m
    TypedValue mFixedWidthMinor = null;
    TypedValue mFixedHeightMajor = null;
    TypedValue mFixedHeightMinor = null;

    if (a.hasValue(R.styleable.Theme_windowFixedWidthMajor)) {
        if (mFixedWidthMajor == null)
            mFixedWidthMajor = new TypedValue();
        a.getValue(R.styleable.Theme_windowFixedWidthMajor, mFixedWidthMajor);
    }
    if (a.hasValue(R.styleable.Theme_windowFixedWidthMinor)) {
        if (mFixedWidthMinor == null)
            mFixedWidthMinor = new TypedValue();
        a.getValue(R.styleable.Theme_windowFixedWidthMinor, mFixedWidthMinor);
    }
    if (a.hasValue(R.styleable.Theme_windowFixedHeightMajor)) {
        if (mFixedHeightMajor == null)
            mFixedHeightMajor = new TypedValue();
        a.getValue(R.styleable.Theme_windowFixedHeightMajor, mFixedHeightMajor);
    }
    if (a.hasValue(R.styleable.Theme_windowFixedHeightMinor)) {
        if (mFixedHeightMinor == null)
            mFixedHeightMinor = new TypedValue();
        a.getValue(R.styleable.Theme_windowFixedHeightMinor, mFixedHeightMinor);
    }

    final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
    final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
    int w = ViewGroup.LayoutParams.MATCH_PARENT;
    int h = ViewGroup.LayoutParams.MATCH_PARENT;

    final TypedValue tvw = isPortrait ? mFixedWidthMinor : mFixedWidthMajor;
    if (tvw != null && tvw.type != TypedValue.TYPE_NULL) {
        if (tvw.type == TypedValue.TYPE_DIMENSION) {
            w = (int) tvw.getDimension(metrics);
        } else if (tvw.type == TypedValue.TYPE_FRACTION) {
            w = (int) tvw.getFraction(metrics.widthPixels, metrics.widthPixels);
        }
    }

    final TypedValue tvh = isPortrait ? mFixedHeightMajor : mFixedHeightMinor;
    if (tvh != null && tvh.type != TypedValue.TYPE_NULL) {
        if (tvh.type == TypedValue.TYPE_DIMENSION) {
            h = (int) tvh.getDimension(metrics);
        } else if (tvh.type == TypedValue.TYPE_FRACTION) {
            h = (int) tvh.getFraction(metrics.heightPixels, metrics.heightPixels);
        }
    }

    if (w != ViewGroup.LayoutParams.MATCH_PARENT || h != ViewGroup.LayoutParams.MATCH_PARENT) {
        mWindow.setLayout(w, h);
    }

    a.recycle();
}

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

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.");
        }//from  w  w w.ja  va  2s  .co  m
        mVerticalScrollFactor = outValue.getDimension(context.getResources().getDisplayMetrics());
    }
    return mVerticalScrollFactor;
}

From source file:com.hippo.widget.BothScrollView.java

/**
 * 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.
 *//*from w w  w.j a  v  a 2  s  .c  om*/
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;
}

From source file:android.support.v71.widget.RecyclerView.java

/**
 * Ported from View.getVerticalScrollFactor.
 *///  ww w  .ja  v  a 2 s . c om
private float getScrollFactor() {
    if (mScrollFactor == Float.MIN_VALUE) {
        TypedValue outValue = new TypedValue();
        if (getContext().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, outValue, true)) {
            mScrollFactor = outValue.getDimension(getContext().getResources().getDisplayMetrics());
        } else {
            return 0; //listPreferredItemHeight is not defined, no generic scrolling
        }

    }
    return mScrollFactor;
}

From source file:cn.ismartv.tvrecyclerview.widget.RecyclerView.java

/**
 * Ported from View.getVerticalScrollFactor.
 *//*from  w  ww .  ja v  a  2 s . c o  m*/
private float getScrollFactor() {
    if (mScrollFactor == Float.MIN_VALUE) {
        TypedValue outValue = new TypedValue();
        if (getContext().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, outValue, true)) {
            mScrollFactor = outValue.getDimension(getContext().getResources().getDisplayMetrics());
        } else {
            return 0; //listPreferredItemHeight is not defined, no generic scrolling
        }
    }
    return mScrollFactor;
}