Search in sources :

Example 41 with Insets

use of android.graphics.Insets in project platform_frameworks_base by android.

the class View method measure.

/**
     * <p>
     * This is called to find out how big a view should be. The parent
     * supplies constraint information in the width and height parameters.
     * </p>
     *
     * <p>
     * The actual measurement work of a view is performed in
     * {@link #onMeasure(int, int)}, called by this method. Therefore, only
     * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
     * </p>
     *
     *
     * @param widthMeasureSpec Horizontal space requirements as imposed by the
     *        parent
     * @param heightMeasureSpec Vertical space requirements as imposed by the
     *        parent
     *
     * @see #onMeasure(int, int)
     */
public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
    boolean optical = isLayoutModeOptical(this);
    if (optical != isLayoutModeOptical(mParent)) {
        Insets insets = getOpticalInsets();
        int oWidth = insets.left + insets.right;
        int oHeight = insets.top + insets.bottom;
        widthMeasureSpec = MeasureSpec.adjust(widthMeasureSpec, optical ? -oWidth : oWidth);
        heightMeasureSpec = MeasureSpec.adjust(heightMeasureSpec, optical ? -oHeight : oHeight);
    }
    // Suppress sign extension for the low bytes
    long key = (long) widthMeasureSpec << 32 | (long) heightMeasureSpec & 0xffffffffL;
    if (mMeasureCache == null)
        mMeasureCache = new LongSparseLongArray(2);
    final boolean forceLayout = (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
    // Optimize layout by avoiding an extra EXACTLY pass when the view is
    // already measured as the correct size. In API 23 and below, this
    // extra pass is required to make LinearLayout re-distribute weight.
    final boolean specChanged = widthMeasureSpec != mOldWidthMeasureSpec || heightMeasureSpec != mOldHeightMeasureSpec;
    final boolean isSpecExactly = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY;
    final boolean matchesSpecSize = getMeasuredWidth() == MeasureSpec.getSize(widthMeasureSpec) && getMeasuredHeight() == MeasureSpec.getSize(heightMeasureSpec);
    final boolean needsLayout = specChanged && (sAlwaysRemeasureExactly || !isSpecExactly || !matchesSpecSize);
    if (forceLayout || needsLayout) {
        // first clears the measured dimension flag
        mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
        resolveRtlPropertiesIfNeeded();
        int cacheIndex = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
        if (cacheIndex < 0 || sIgnoreMeasureCache) {
            // measure ourselves, this should set the measured dimension flag back
            onMeasure(widthMeasureSpec, heightMeasureSpec);
            mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
        } else {
            long value = mMeasureCache.valueAt(cacheIndex);
            // Casting a long to int drops the high 32 bits, no mask needed
            setMeasuredDimensionRaw((int) (value >> 32), (int) value);
            mPrivateFlags3 |= PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
        }
        // an exception to warn the developer
        if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
            throw new IllegalStateException("View with id " + getId() + ": " + getClass().getName() + "#onMeasure() did not set the" + " measured dimension by calling" + " setMeasuredDimension()");
        }
        mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
    }
    mOldWidthMeasureSpec = widthMeasureSpec;
    mOldHeightMeasureSpec = heightMeasureSpec;
    mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 | // suppress sign extension
    (long) mMeasuredHeight & 0xffffffffL);
}
Also used : LongSparseLongArray(android.util.LongSparseLongArray) Insets(android.graphics.Insets) Paint(android.graphics.Paint) Point(android.graphics.Point)

Example 42 with Insets

use of android.graphics.Insets in project platform_frameworks_base by android.

the class View method setOpticalFrame.

private boolean setOpticalFrame(int left, int top, int right, int bottom) {
    Insets parentInsets = mParent instanceof View ? ((View) mParent).getOpticalInsets() : Insets.NONE;
    Insets childInsets = getOpticalInsets();
    return setFrame(left + parentInsets.left - childInsets.left, top + parentInsets.top - childInsets.top, right + parentInsets.left + childInsets.right, bottom + parentInsets.top + childInsets.bottom);
}
Also used : Insets(android.graphics.Insets)

Example 43 with Insets

use of android.graphics.Insets in project platform_frameworks_base by android.

the class GridLayout method onDebugDraw.

/**
     * @hide
     */
@Override
protected void onDebugDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.argb(50, 255, 255, 255));
    Insets insets = getOpticalInsets();
    int top = getPaddingTop() + insets.top;
    int left = getPaddingLeft() + insets.left;
    int right = getWidth() - getPaddingRight() - insets.right;
    int bottom = getHeight() - getPaddingBottom() - insets.bottom;
    int[] xs = mHorizontalAxis.locations;
    if (xs != null) {
        for (int i = 0, length = xs.length; i < length; i++) {
            int x = left + xs[i];
            drawLine(canvas, x, top, x, bottom, paint);
        }
    }
    int[] ys = mVerticalAxis.locations;
    if (ys != null) {
        for (int i = 0, length = ys.length; i < length; i++) {
            int y = top + ys[i];
            drawLine(canvas, left, y, right, y, paint);
        }
    }
    super.onDebugDraw(canvas);
}
Also used : Insets(android.graphics.Insets) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 44 with Insets

use of android.graphics.Insets in project platform_frameworks_base by android.

the class Switch method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {
        padding.setEmpty();
    }
    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;
    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Insets insets = thumbDrawable.getOpticalInsets();
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;
            final int saveCount = canvas.save();
            canvas.clipRect(padding, Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }
    final int saveCount = canvas.save();
    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }
    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int[] drawableState = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;
        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }
        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }
    canvas.restoreToCount(saveCount);
}
Also used : Rect(android.graphics.Rect) Insets(android.graphics.Insets) StaticLayout(android.text.StaticLayout) Layout(android.text.Layout) Drawable(android.graphics.drawable.Drawable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 45 with Insets

use of android.graphics.Insets in project platform_frameworks_base by android.

the class Switch method onMeasure.

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (mShowText) {
        if (mOnLayout == null) {
            mOnLayout = makeLayout(mTextOn);
        }
        if (mOffLayout == null) {
            mOffLayout = makeLayout(mTextOff);
        }
    }
    final Rect padding = mTempRect;
    final int thumbWidth;
    final int thumbHeight;
    if (mThumbDrawable != null) {
        // Cached thumb width does not include padding.
        mThumbDrawable.getPadding(padding);
        thumbWidth = mThumbDrawable.getIntrinsicWidth() - padding.left - padding.right;
        thumbHeight = mThumbDrawable.getIntrinsicHeight();
    } else {
        thumbWidth = 0;
        thumbHeight = 0;
    }
    final int maxTextWidth;
    if (mShowText) {
        maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth()) + mThumbTextPadding * 2;
    } else {
        maxTextWidth = 0;
    }
    mThumbWidth = Math.max(maxTextWidth, thumbWidth);
    final int trackHeight;
    if (mTrackDrawable != null) {
        mTrackDrawable.getPadding(padding);
        trackHeight = mTrackDrawable.getIntrinsicHeight();
    } else {
        padding.setEmpty();
        trackHeight = 0;
    }
    // Adjust left and right padding to ensure there's enough room for the
    // thumb's padding (when present).
    int paddingLeft = padding.left;
    int paddingRight = padding.right;
    if (mThumbDrawable != null) {
        final Insets inset = mThumbDrawable.getOpticalInsets();
        paddingLeft = Math.max(paddingLeft, inset.left);
        paddingRight = Math.max(paddingRight, inset.right);
    }
    final int switchWidth = Math.max(mSwitchMinWidth, 2 * mThumbWidth + paddingLeft + paddingRight);
    final int switchHeight = Math.max(trackHeight, thumbHeight);
    mSwitchWidth = switchWidth;
    mSwitchHeight = switchHeight;
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    final int measuredHeight = getMeasuredHeight();
    if (measuredHeight < switchHeight) {
        setMeasuredDimension(getMeasuredWidthAndState(), switchHeight);
    }
}
Also used : Rect(android.graphics.Rect) Insets(android.graphics.Insets) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Aggregations

Insets (android.graphics.Insets)73 Paint (android.graphics.Paint)53 Rect (android.graphics.Rect)35 TextPaint (android.text.TextPaint)26 Drawable (android.graphics.drawable.Drawable)15 Point (android.graphics.Point)10 NinePatch (android.graphics.NinePatch)5 Layout (android.text.Layout)5 StaticLayout (android.text.StaticLayout)5 LongSparseLongArray (android.util.LongSparseLongArray)4