Example usage for android.widget ImageView getBaseline

List of usage examples for android.widget ImageView getBaseline

Introduction

In this page you can find the example usage for android.widget ImageView getBaseline.

Prototype

@Override
@ViewDebug.ExportedProperty(category = "layout")
public int getBaseline() 

Source Link

Document

Return the offset of the widget's text baseline from the widget's top boundary.

Usage

From source file:org.dmfs.android.view.DrawablePagerTitleStrip.java

@SuppressLint("NewApi")
void updateDrawablePositions(int position, float positionOffset, boolean force) {
    if (mImageViews == null || mImageViews.length == 0) {
        return;//from w  w w .java 2 s. c o  m
    }

    if (position != mLastKnownCurrentPage) {
        updateImages(position, mPager.getAdapter());
    } else if (!force && positionOffset == mLastKnownPositionOffset) {
        return;
    }

    mUpdatingPositions = true;

    final int[] widths = new int[mImageViews.length];
    for (int i = 0, l = mImageViews.length; i < l; ++i) {
        widths[i] = mImageViews[i].getMeasuredWidth();
    }

    final int stripWidth = getWidth();
    final int stripHeight = getHeight();
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();

    float currOffset = positionOffset + 0.5f;
    if (currOffset > 1.f) {
        currOffset -= 1.f;
    }
    final int currCenter = stripWidth / 2
            - (int) ((widths[widths.length / 2] + mScaledTextSpacing) * (currOffset - 0.5f));
    final int currLeft = currCenter - widths[widths.length / 2] / 2;

    int maxBaseline = 0;
    for (ImageView iv : mImageViews) {
        maxBaseline = Math.max(maxBaseline, iv.getBaseline());
    }

    int maxTextHeight = 0;
    for (ImageView iv : mImageViews) {
        maxTextHeight = Math.max(maxTextHeight, maxBaseline - iv.getBaseline() + iv.getMeasuredHeight());
    }

    final int vgrav = mGravity & Gravity.VERTICAL_GRAVITY_MASK;

    int left = currLeft;
    for (int i = mImageViews.length / 2, l = mImageViews.length; i < l; ++i) {
        int baseline = mImageViews[i].getBaseline();
        int top;
        switch (vgrav) {
        default:
        case Gravity.TOP:
            top = paddingTop + maxBaseline - baseline;
            break;
        case Gravity.CENTER_VERTICAL:
            final int paddedHeight = stripHeight - paddingTop - paddingBottom;
            final int centeredTop = (paddedHeight - maxTextHeight) / 2;
            top = centeredTop + maxBaseline - baseline;
            break;
        case Gravity.BOTTOM:
            final int bottomGravTop = stripHeight - paddingBottom - maxTextHeight;
            top = bottomGravTop + maxBaseline - baseline;
            break;
        }

        int right = left + widths[i];
        mImageViews[i].layout(left, top, right, top + mImageViews[i].getMeasuredHeight());
        left += widths[i] + mScaledTextSpacing;

        if (VERSION.SDK_INT >= 14) {
            if (stripWidth - right < paddingRight) {
                mImageViews[i].setAlpha(
                        1 - ((float) Math.abs(Math.min(stripWidth - right - paddingRight, 0))) / widths[i]);
            } else {
                mImageViews[i].setAlpha(1f);
            }
        }
    }

    left = currLeft;
    for (int i = mImageViews.length / 2 - 1; i >= 0; --i) {
        int baseline = mImageViews[i].getBaseline();
        int top;
        switch (vgrav) {
        default:
        case Gravity.TOP:
            top = paddingTop + maxBaseline - baseline;
            break;
        case Gravity.CENTER_VERTICAL:
            final int paddedHeight = stripHeight - paddingTop - paddingBottom;
            final int centeredTop = (paddedHeight - maxTextHeight) / 2;
            top = centeredTop + maxBaseline - baseline;
            break;
        case Gravity.BOTTOM:
            final int bottomGravTop = stripHeight - paddingBottom - maxTextHeight;
            top = bottomGravTop + maxBaseline - baseline;
            break;
        }
        left -= widths[i] + mScaledTextSpacing;

        mImageViews[i].layout(left, top, left + widths[i], top + mImageViews[i].getMeasuredHeight());

        if (VERSION.SDK_INT >= 14) {
            if (left < paddingLeft) {
                mImageViews[i].setAlpha(1 - ((float) Math.abs(Math.min(left - paddingLeft, 0))) / widths[i]);
            } else {
                mImageViews[i].setAlpha(1f);
            }
        }
    }

    mLastKnownPositionOffset = positionOffset;
    mUpdatingPositions = false;
}