Example usage for android.view Gravity CENTER_VERTICAL

List of usage examples for android.view Gravity CENTER_VERTICAL

Introduction

In this page you can find the example usage for android.view Gravity CENTER_VERTICAL.

Prototype

int CENTER_VERTICAL

To view the source code for android.view Gravity CENTER_VERTICAL.

Click Source Link

Document

Place object in the vertical center of its container, not changing its size.

Usage

From source file:com.woodblockwithoutco.quickcontroldock.ui.ControlService.java

public void attachTemporaryView(Drawable icon, String appName) {
    LinearLayout l = new LinearLayout(getApplicationContext());
    l.setGravity(Gravity.CENTER_VERTICAL);
    l.setOrientation(LinearLayout.HORIZONTAL);
    l.setPadding(NOTIFICATION_TOAST_PADDING_PX, NOTIFICATION_TOAST_PADDING_PX, NOTIFICATION_TOAST_PADDING_PX,
            NOTIFICATION_TOAST_PADDING_PX);

    l.setBackgroundResource(R.drawable.notification_toast_bg);

    TextView tv = new TextView(getApplicationContext());
    tv.setGravity(Gravity.CENTER);/*from  w w w. ja va2  s  .  c o  m*/
    tv.setCompoundDrawablePadding(COMPOUND_DRAWABLE_PADDING_PX);
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);

    String notificationText = getString(R.string.notification_from) + "\n" + appName;
    tv.setText(notificationText);
    tv.setTextColor(COLOR_WHITE);

    l.addView(tv);

    Toast toast = new Toast(getApplicationContext());
    toast.setView(l);
    toast.show();
}

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 ww .j  a va2  s. com*/
    }

    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;
}

From source file:org.smart.library.widget.PagerSlidingTabStrip.java

public void addTextTab(final int position, CharSequence title, Drawable mIconDrawable) {
    CustomTextView tab = new CustomTextView(getContext());
    tab.setText(title);// ww  w . ja  v  a2s  . c  om
    tab.setGravity(mIconDrawable != null ? Gravity.CENTER_VERTICAL : Gravity.CENTER);
    tab.setSingleLine();
    tab.setDrawableCenter(true);

    if (mIconDrawable != null) {
        tabPadding = 0;
        tab.setCompoundDrawablesWithIntrinsicBounds(mIconDrawable, null, null, null);
    }
    addTab(position, tab);
}

From source file:io.github.minime89.passbeam.activities.KeyboardLayoutActivity.java

private void setupKeyboardLayoutMenu() {
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        ProgressBar progressBar = new ProgressBar(this);
        progressBar.setVisibility(View.GONE);
        progressBar.setIndeterminate(true);

        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT, Gravity.END | Gravity.CENTER_VERTICAL);
        progressBar.setLayoutParams(layoutParams);

        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(progressBar);
    }/*from  w w w.  j  a va  2  s. c o m*/
}

From source file:io.plaidapp.ui.widget.CollapsingTitleLayout.java

private void recalculate(int width) {

    // reset stateful objects that might change over measure passes
    paint.setTextSize(collapsedTextSize);
    displayText = new SpannableStringBuilder(title);

    // Calculate line height; ensure it' a multiple of 4dp to sit on the grid
    final float fourDip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
            getResources().getDisplayMetrics());
    Paint.FontMetricsInt fm = paint.getFontMetricsInt();
    int fontHeight = Math.abs(fm.ascent - fm.descent) + fm.leading;
    final int baselineAlignedLineHeight = (int) (fourDip * (float) Math.ceil(lineHeightHint / fourDip));
    final int lineSpacingAdd = baselineAlignedLineHeight - fontHeight;

    // now create the layout with our desired insets & line height
    createLayout(width, lineSpacingAdd);

    // adjust the displayText top inset to vertically center text with the toolbar
    collapsedHeight = (int) Math.max(ViewUtils.getActionBarSize(getContext()),
            (fourDip + baselineAlignedLineHeight + fourDip));
    titleInsetTop = (collapsedHeight - baselineAlignedLineHeight) / 2f;

    if (lineCount == 1) { // single line mode
        layout = null;//from   ww w . j av a  2s  .  com
        collapsingText = new CollapsingTextHelper(this);
        collapsingText.setText(title);

        collapsingText.setCollapsedBounds(titleInsetStart, 0, width - titleInsetEnd, (int) collapsedHeight);

        collapsingText.setExpandedBounds(titleInsetStart, (int) titleInsetTop, width - titleInsetEnd,
                getMinimumHeight() - titleInsetBottom);
        collapsingText.setCollapsedTextColor(paint.getColor());
        collapsingText.setExpandedTextColor(paint.getColor());
        collapsingText.setCollapsedTextSize(collapsedTextSize);

        int expandedTitleTextSize = (int) Math.max(collapsedTextSize,
                ViewUtils.getSingleLineTextSize(displayText.toString(), paint,
                        width - titleInsetStart - titleInsetEnd, collapsedTextSize, maxExpandedTextSize, 0.5f,
                        getResources().getDisplayMetrics()));
        collapsingText.setExpandedTextSize(expandedTitleTextSize);

        collapsingText.setExpandedTextGravity(GravityCompat.START | Gravity.BOTTOM);
        collapsingText.setCollapsedTextGravity(GravityCompat.START | Gravity.CENTER_VERTICAL);
        collapsingText.setTypeface(paint.getTypeface());

        fm = paint.getFontMetricsInt();
        fontHeight = Math.abs(fm.ascent - fm.descent) + fm.leading;
        textTop = getHeight() - titleInsetBottom - fontHeight;
        scrollRange = getMinimumHeight() - (int) collapsedHeight;
    } else { // multi-line mode
        // bottom align the text
        textTop = getDesiredHeight() - titleInsetBottom - layout.getHeight();

        // pre-calculate at what scroll offsets lines should disappear
        scrollRange = (int) (textTop - titleInsetTop);
        final int fadeDistance = lineSpacingAdd + fm.descent; // line bottom to baseline
        lines = new Line[lineCount];
        for (int i = 1; i < lineCount; i++) {
            int lineBottomScrollOffset = scrollRange + ((lineCount - i - 1) * baselineAlignedLineHeight);
            lines[i] = new Line(layout.getLineStart(i), layout.getLineEnd(i),
                    new ForegroundColorSpan(paint.getColor()), lineBottomScrollOffset,
                    lineBottomScrollOffset + fadeDistance);
        }
    }
    calculatedWithWidth = width;
}

From source file:org.wheelmap.android.activity.MainMultiPaneActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    ActionBar bar = getSupportActionBar();
    if (bar == null) {
        return true;
    }// w  w w  . j  a  v  a  2s  . c  o  m

    LayoutInflater inflater = LayoutInflater.from(this);
    View customView = inflater.inflate(R.layout.actionbar_tablet, null);
    bar.setCustomView(customView, new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL | Gravity.END));

    boolean isPortraitMode = getResources()
            .getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    if (isPortraitMode) {

        ImageView addItem = (ImageView) customView.findViewById(R.id.menu_new_poi);
        addItem.setVisibility(View.VISIBLE);
        OnClickListener addClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                createNewPoi();
            }
        };
        addItem.setOnClickListener(addClickListener);

        LinearLayout l = (LinearLayout) findViewById(R.id.actionbar_bottom);
        for (int i = 0; i < l.getChildCount(); i++) {
            l.getChildAt(i).setOnTouchListener(new PressSelector());
        }

        bar.setDisplayShowCustomEnabled(true);
        View v = findViewById(R.id.menu_filter);
        MapActivityUtils.setAccessFilterOptionDrawable(this, null, v);

        View filterWc = findViewById(R.id.menu_wc);
        MapActivityUtils.setWcFilterOptionsDrawable(this, null, filterWc);

        UserCredentials credentials = new UserCredentials(getApplicationContext());
        ImageView image = (ImageView) findViewById(R.id.menu_login);
        image.setImageResource(
                credentials.isLoggedIn() ? R.drawable.start_icon_logged_in : R.drawable.start_icon_login);
    } else {
        MenuInflater inflaterMenu = getMenuInflater();
        inflaterMenu.inflate(R.menu.ab_multi_activity, menu);
        MenuItem item = menu.findItem(R.id.menu_filter);
        MapActivityUtils.setAccessFilterOptionDrawable(this, item, null);
        item = menu.findItem(R.id.menu_wc);
        MapActivityUtils.setWcFilterOptionsDrawable(this, item, null);

    }

    if (mapModeType == MapModeType.MAP_MODE_ENGAGE) {
        MenuItem itemFilterWheelChairs = menu.findItem(R.id.menu_filter);
        itemFilterWheelChairs.setEnabled(false);
        //TODO Disable it - doesn't work yet
    }

    ImageView listMapToggle = (ImageView) findViewById(R.id.switch_view);
    if (listMapToggle != null) {
        listMapToggle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleMovableResize();
            }
        });
    }

    return true;
}

From source file:com.collcloud.frame.viewpager.PagerSlidingTabStrip.java

private void addTab(final int position, View tab) {
    tab.setFocusable(true);//from  ww w . j ava 2  s.c  om
    tab.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            pager.setCurrentItem(position);
        }
    });

    tab.setPadding(tabPadding, 0, tabPadding, 0);
    defaultTabLayoutParams.gravity = Gravity.CENTER_VERTICAL;
    tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);
}

From source file:rdx.andro.forexcapplugins.childBrowser.ChildBrowser.java

/**
 * Display a new browser with the specified URL.
 * //w ww  . j a v  a  2  s  .c  o m
 * @param url
 *            The url to load.
 * @param jsonObject
 */
public String showWebPage(final String url, JSONObject options) {
    // Determine if we should hide the location bar.
    if (options != null) {
        showLocationBar = options.optBoolean("showLocationBar", true);
    }

    // Create dialog in new thread
    Runnable runnable = new Runnable() {
        /**
         * Convert our DIP units to Pixels
         * 
         * @return int
         */
        private int dpToPixels(int dipValue) {
            int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) dipValue,
                    cordova.getActivity().getResources().getDisplayMetrics());

            return value;
        }

        public void run() {
            // Let's create the main dialog
            dialog = new Dialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar);
            dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                public void onDismiss(DialogInterface dialog) {
                    try {
                        JSONObject obj = new JSONObject();
                        obj.put("type", CLOSE_EVENT);

                        sendUpdate(obj, false);
                    } catch (JSONException e) {
                        Log.d(LOG_TAG, "Should never happen");
                    }
                }
            });

            // Main container layout
            LinearLayout main = new LinearLayout(cordova.getActivity());
            main.setOrientation(LinearLayout.VERTICAL);

            // Toolbar layout
            RelativeLayout toolbar = new RelativeLayout(cordova.getActivity());
            toolbar.setLayoutParams(
                    new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, this.dpToPixels(44)));
            toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
            toolbar.setHorizontalGravity(Gravity.LEFT);
            toolbar.setVerticalGravity(Gravity.TOP);

            // Action Button Container layout
            RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity());
            actionButtonContainer.setLayoutParams(
                    new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            actionButtonContainer.setHorizontalGravity(Gravity.LEFT);
            actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
            actionButtonContainer.setId(1);

            // Back button
            ImageButton back = new ImageButton(cordova.getActivity());
            RelativeLayout.LayoutParams backLayoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
            backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT);
            back.setLayoutParams(backLayoutParams);
            back.setContentDescription("Back Button");
            back.setId(2);
            try {
                back.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_left.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            back.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goBack();
                }
            });

            // Forward button
            ImageButton forward = new ImageButton(cordova.getActivity());
            RelativeLayout.LayoutParams forwardLayoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
            forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF, 2);
            forward.setLayoutParams(forwardLayoutParams);
            forward.setContentDescription("Forward Button");
            forward.setId(3);
            try {
                forward.setImageBitmap(loadDrawable("www/childbrowser/icon_arrow_right.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            forward.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    goForward();
                }
            });

            // Edit Text Box
            edittext = new EditText(cordova.getActivity());
            RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            textLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);
            textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5);
            edittext.setLayoutParams(textLayoutParams);
            edittext.setId(4);
            edittext.setSingleLine(true);
            edittext.setText(url);
            edittext.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
            edittext.setImeOptions(EditorInfo.IME_ACTION_GO);
            edittext.setInputType(InputType.TYPE_NULL); // Will not except
            // input... Makes
            // the text
            // NON-EDITABLE
            edittext.setOnKeyListener(new View.OnKeyListener() {
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    // If the event is a key-down event on the "enter"
                    // button
                    if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        navigate(edittext.getText().toString());
                        return true;
                    }
                    return false;
                }
            });

            // Close button
            ImageButton close = new ImageButton(cordova.getActivity());
            RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
            closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            close.setLayoutParams(closeLayoutParams);
            forward.setContentDescription("Close Button");
            close.setId(5);
            try {
                close.setImageBitmap(loadDrawable("www/childbrowser/icon_close.png"));
            } catch (IOException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
            }
            close.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    closeDialog();
                }
            });

            // WebView
            webview = new WebView(cordova.getActivity());
            webview.setLayoutParams(
                    new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            webview.setWebChromeClient(new WebChromeClient());
            WebViewClient client = new ChildBrowserClient(edittext);
            webview.setWebViewClient(client);
            WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setBuiltInZoomControls(true);
            // settings.setPluginState(true);
            settings.setDomStorageEnabled(true);
            webview.loadUrl(url);
            webview.setId(6);
            webview.getSettings().setLoadWithOverviewMode(true);
            webview.getSettings().setUseWideViewPort(true);
            webview.requestFocus();
            webview.requestFocusFromTouch();

            // Add the back and forward buttons to our action button
            // container layout
            actionButtonContainer.addView(back);
            actionButtonContainer.addView(forward);

            // Add the views to our toolbar
            toolbar.addView(actionButtonContainer);
            toolbar.addView(edittext);
            toolbar.addView(close);

            // Don't add the toolbar if its been disabled
            if (getShowLocationBar()) {
                // Add our toolbar to our main view/layout
                main.addView(toolbar);
            }

            // Add our webview to our main view/layout
            main.addView(webview);

            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;

            dialog.setContentView(main);
            dialog.show();
            dialog.getWindow().setAttributes(lp);
        }

        private Bitmap loadDrawable(String filename) throws java.io.IOException {
            InputStream input = cordova.getActivity().getAssets().open(filename);
            return BitmapFactory.decodeStream(input);
        }
    };
    this.cordova.getActivity().runOnUiThread(runnable);
    return "";
}

From source file:com.lgh.tool.myview.PagerSlidingTabStrip.java

/**
 * ?/*from   w  ww  .  j a  v  a2 s .  c o  m*/
 */
private ViewGroup getTabsLayout() {
    if (tabsLayout == null) {
        if (getChildCount() > 0) {
            tabsLayout = (ViewGroup) getChildAt(0);
        } else {
            removeAllViews();
            LinearLayout tabsLayout = new LinearLayout(getContext());
            tabsLayout.setGravity(Gravity.CENTER_VERTICAL);
            this.tabsLayout = tabsLayout;
            addView(tabsLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER_VERTICAL));
        }
    }
    return tabsLayout;
}

From source file:com.amitupadhyay.aboutexample.ui.widget.CollapsingTitleLayout.java

private void recalculate(int width) {

    // reset stateful objects that might change over measure passes
    paint.setTextSize(collapsedTextSize);
    displayText = new SpannableStringBuilder(title);

    // Calculate line height; ensure it' a multiple of 4dp to sit on the grid
    final float fourDip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
            getResources().getDisplayMetrics());
    Paint.FontMetricsInt fm = paint.getFontMetricsInt();
    int fontHeight = Math.abs(fm.ascent - fm.descent) + fm.leading;
    final int baselineAlignedLineHeight = (int) (fourDip * (float) Math.ceil(lineHeightHint / fourDip));
    lineSpacingAdd = Math.max(0, baselineAlignedLineHeight - fontHeight);

    // now create the layout with our desired insets & line height
    createLayout(width, lineSpacingAdd);

    // adjust the title top inset to vertically center text with the toolbar
    collapsedHeight = (int) Math.max(ViewUtils.getActionBarSize(getContext()),
            (fourDip + baselineAlignedLineHeight + fourDip));
    titleInsetTop = (collapsedHeight - baselineAlignedLineHeight) / 2f;

    if (lineCount == 1) { // single line mode
        layout = null;/*from   www . ja va2 s.  c o  m*/
        collapsingText = new CollapsingTextHelper(this);
        collapsingText.setText(title);

        collapsingText.setCollapsedBounds(titleInsetStart, 0, width - titleInsetEnd, (int) collapsedHeight);

        collapsingText.setExpandedBounds(titleInsetStart, (int) titleInsetTop, width - titleInsetEnd,
                getMinimumHeight() - titleInsetBottom);
        collapsingText.setCollapsedTextColor(paint.getColor());
        collapsingText.setExpandedTextColor(paint.getColor());
        collapsingText.setCollapsedTextSize(collapsedTextSize);

        int expandedTitleTextSize = (int) Math.max(collapsedTextSize,
                ViewUtils.getSingleLineTextSize(displayText.toString(), paint,
                        width - titleInsetStart - titleInsetEnd, collapsedTextSize, maxExpandedTextSize, 0.5f,
                        getResources().getDisplayMetrics()));
        collapsingText.setExpandedTextSize(expandedTitleTextSize);

        collapsingText.setExpandedTextGravity(GravityCompat.START | Gravity.BOTTOM);
        collapsingText.setCollapsedTextGravity(GravityCompat.START | Gravity.CENTER_VERTICAL);
        collapsingText.setTypeface(paint.getTypeface());

        textTop = getHeight() - titleInsetBottom - fontHeight;
        scrollRange = getMinimumHeight() - (int) collapsedHeight;
    } else { // multi-line mode
        // bottom align the text
        textTop = getDesiredHeight() - titleInsetBottom - layout.getHeight();

        // pre-calculate at what scroll offsets lines should disappear
        scrollRange = (int) (textTop - titleInsetTop);
        final int fadeDistance = layout.getLineBottom(0) - layout.getLineBaseline(0);
        lines = new Line[lineCount];
        for (int i = 1; i < lineCount; i++) {
            int lineBottomScrollOffset = scrollRange + ((lineCount - i - 1) * baselineAlignedLineHeight);
            lines[i] = new Line(layout.getLineStart(i), layout.getLineEnd(i),
                    new TextColorSpan(paint.getColor()), lineBottomScrollOffset,
                    lineBottomScrollOffset + fadeDistance);
        }
    }
    calculatedWithWidth = width;
}