Example usage for android.util TypedValue getFraction

List of usage examples for android.util TypedValue getFraction

Introduction

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

Prototype

public float getFraction(float base, float pbase) 

Source Link

Document

Return the data for this value as a fraction.

Usage

From source file:com.actionbarsherlock.internal.widget.FakeDialogPhoneWindow.java

@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 {/* w  w  w . j a v  a 2  s .  c o m*/
            min = 0;
        }

        if (width < min) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY);
            measure = true;
        }
    }

    // TODO: Support height?

    if (measure) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

From source file:com.bilibili.magicasakura.utils.GradientDrawableUtils.java

void setGradientRadius(Context context, AttributeSet attrs, GradientDrawable drawable, int gradientType)
        throws XmlPullParserException {
    TypedArray a = obtainAttributes(context.getResources(), context.getTheme(), attrs,
            new int[] { android.R.attr.gradientRadius });
    TypedValue value = a.peekValue(0);
    if (value != null) {
        boolean radiusRel = value.type == TypedValue.TYPE_FRACTION;
        drawable.setGradientRadius(radiusRel ? value.getFraction(1.0f, 1.0f) : value.getFloat());
    } else if (gradientType == GradientDrawable.RADIAL_GRADIENT) {
        throw new XmlPullParserException(
                "<gradient> tag requires 'gradientRadius' " + "attribute with radial type");
    }/* w w  w .j a v a2 s .  c o  m*/
    a.recycle();
}

From source file:com.bilibili.magicasakura.utils.GradientDrawableUtils.java

float getAttrFloatOrFraction(Context context, AttributeSet attrs, int attr, float defaultValue, float base,
        float pbase) {
    TypedArray a = obtainAttributes(context.getResources(), context.getTheme(), attrs, new int[] { attr });
    TypedValue tv = a.peekValue(0);
    float v = defaultValue;
    if (tv != null) {
        boolean isFraction = tv.type == TypedValue.TYPE_FRACTION;
        v = isFraction ? tv.getFraction(base, pbase) : tv.getFloat();
    }/*  w w w  . ja v  a  2  s  .  com*/
    a.recycle();
    return v;
}

From source file:com.bilibili.magicasakura.utils.GradientDrawableInflateImpl.java

void setGradientRadius(Context context, AttributeSet attrs, GradientDrawable drawable, int gradientType)
        throws XmlPullParserException {
    TypedArray a = DrawableUtils.obtainAttributes(context.getResources(), context.getTheme(), attrs,
            new int[] { android.R.attr.gradientRadius });
    TypedValue value = a.peekValue(0);
    if (value != null) {
        boolean radiusRel = value.type == TypedValue.TYPE_FRACTION;
        drawable.setGradientRadius(radiusRel ? value.getFraction(1.0f, 1.0f) : value.getFloat());
    } else if (gradientType == GradientDrawable.RADIAL_GRADIENT) {
        throw new XmlPullParserException(
                "<gradient> tag requires 'gradientRadius' " + "attribute with radial type");
    }//from  w  w  w  .  java 2 s  .  c om
    a.recycle();
}

From source file:com.bilibili.magicasakura.utils.GradientDrawableInflateImpl.java

float getAttrFloatOrFraction(Context context, AttributeSet attrs, int attr, float defaultValue, float base,
        float pbase) {
    TypedArray a = DrawableUtils.obtainAttributes(context.getResources(), context.getTheme(), attrs,
            new int[] { attr });
    TypedValue tv = a.peekValue(0);
    float v = defaultValue;
    if (tv != null) {
        boolean isFraction = tv.type == TypedValue.TYPE_FRACTION;
        v = isFraction ? tv.getFraction(base, pbase) : tv.getFloat();
    }/*  w  ww  .  ja  v  a2 s.c  o m*/
    a.recycle();
    return v;
}

From source file:com.hippo.conductor.dialog.DialogContentView.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    final boolean isPortrait = getResources()
            .getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    final TypedValue tv = isPortrait ? minWidthMinor : minWidthMajor;
    boolean measure = false;

    if (widthMode == MeasureSpec.AT_MOST && tv.type != TypedValue.TYPE_NULL) {
        final int min;
        final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        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 {//  w w  w  .  ja v a  2s  .c o  m
            min = 0;
        }

        if (getMeasuredWidth() < min) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, MeasureSpec.EXACTLY);
            measure = true;
        }
    }

    if (measure) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

From source file:android.support.v7ox.widget.ContentFrameLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;

    final int widthMode = getMode(widthMeasureSpec);
    final int heightMode = getMode(heightMeasureSpec);

    boolean fixedWidth = false;
    if (widthMode == AT_MOST) {
        final TypedValue tvw = isPortrait ? mFixedWidthMinor : mFixedWidthMajor;
        if (tvw != null && tvw.type != TypedValue.TYPE_NULL) {
            int w = 0;
            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);
            }/*from  ww  w.  ja v a2s . c o m*/
            if (w > 0) {
                w -= (mDecorPadding.left + mDecorPadding.right);
                final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(w, widthSize), EXACTLY);
                fixedWidth = true;
            }
        }
    }

    if (heightMode == AT_MOST) {
        final TypedValue tvh = isPortrait ? mFixedHeightMajor : mFixedHeightMinor;
        if (tvh != null && tvh.type != TypedValue.TYPE_NULL) {
            int h = 0;
            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 (h > 0) {
                h -= (mDecorPadding.top + mDecorPadding.bottom);
                final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(h, heightSize), EXACTLY);
            }
        }
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    boolean measure = false;

    widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);

    if (!fixedWidth && widthMode == AT_MOST) {
        final TypedValue tv = isPortrait ? mMinWidthMinor : mMinWidthMajor;
        if (tv != null && tv.type != TypedValue.TYPE_NULL) {
            int min = 0;
            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);
            }
            if (min > 0) {
                min -= (mDecorPadding.left + mDecorPadding.right);
            }
            if (width < min) {
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY);
                measure = true;
            }
        }
    }

    if (measure) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

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//w w w.ja  va 2 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.support.v7.app.AppCompatDelegateImplV7.java

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

    TypedValue mFixedWidthMajor = null;/*from www .  java2s . c  om*/
    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();
}