Example usage for android.graphics Rect width

List of usage examples for android.graphics Rect width

Introduction

In this page you can find the example usage for android.graphics Rect width.

Prototype

public final int width() 

Source Link

Usage

From source file:xyz.klinker.blur.launcher3.Workspace.java

private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY, DragView dragView, CellLayout layout,
        ItemInfo info, int[] targetCell, boolean scale) {
    // Now we animate the dragView, (ie. the widget or shortcut preview) into its final
    // location and size on the home screen.
    int spanX = info.spanX;
    int spanY = info.spanY;

    Rect r = estimateItemPosition(layout, targetCell[0], targetCell[1], spanX, spanY);
    loc[0] = r.left;//  w w w.j  a v  a2s  . co m
    loc[1] = r.top;

    setFinalTransitionTransform(layout);
    float cellLayoutScale = mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true);
    resetTransitionTransform(layout);

    float dragViewScaleX;
    float dragViewScaleY;
    if (scale) {
        dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth();
        dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight();
    } else {
        dragViewScaleX = 1f;
        dragViewScaleY = 1f;
    }

    // The animation will scale the dragView about its center, so we need to center about
    // the final location.
    loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2
            - Math.ceil(layout.getUnusedHorizontalSpace() / 2f);
    loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2;

    scaleXY[0] = dragViewScaleX * cellLayoutScale;
    scaleXY[1] = dragViewScaleY * cellLayoutScale;
}

From source file:com.android.launcher3.Workspace.java

public void onExternalDragStartedWithItem(View v) {
    // Compose a drag bitmap with the view scaled to the icon size
    LauncherAppState app = LauncherAppState.getInstance();
    DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
    int iconSize = grid.iconSizePx;
    int bmpWidth = v.getMeasuredWidth();
    int bmpHeight = v.getMeasuredHeight();

    // If this is a text view, use its drawable instead
    if (v instanceof TextView) {
        TextView tv = (TextView) v;//from  w  ww. jav a2s .  c  o  m
        Drawable d = tv.getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        bmpWidth = bounds.width();
        bmpHeight = bounds.height();
    }

    // Compose the bitmap to create the icon from
    Bitmap b = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    mCanvas.setBitmap(b);
    drawDragView(v, mCanvas, 0);
    mCanvas.setBitmap(null);

    // The outline is used to visualize where the item will land if dropped
    mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, iconSize, iconSize, true);
}

From source file:com.android.launcher3.Workspace.java

/**
 * Returns a new bitmap to show when the given View is being dragged around.
 * Responsibility for the bitmap is transferred to the caller.
 * @param expectedPadding padding to add to the drag view. If a different padding was used
 * its value will be changed/*from   w  w w .  j a v  a 2 s .co m*/
 */
public Bitmap createDragBitmap(View v, AtomicInteger expectedPadding) {
    Bitmap b;

    int padding = expectedPadding.get();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        b = Bitmap.createBitmap(bounds.width() + padding, bounds.height() + padding, Bitmap.Config.ARGB_8888);
        expectedPadding.set(padding - bounds.left - bounds.top);
    } else {
        b = Bitmap.createBitmap(v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
    }

    mCanvas.setBitmap(b);
    drawDragView(v, mCanvas, padding);
    mCanvas.setBitmap(null);

    return b;
}

From source file:xyz.klinker.blur.launcher3.Launcher.java

private boolean startActivity(View v, Intent intent, Object tag) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {//w  ww .  j a  v a  2 s  .  c om
        // Only launch using the new animation if the shortcut has not opted out (this is a
        // private contract between launcher and may be ignored in the future).
        boolean useLaunchAnimation = (v != null) && !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
        LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
        UserManagerCompat userManager = UserManagerCompat.getInstance(this);

        UserHandleCompat user = null;
        if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
            long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
            user = userManager.getUserForSerialNumber(serialNumber);
        }

        Bundle optsBundle = null;
        if (useLaunchAnimation) {
            ActivityOptions opts = null;
            if (Utilities.ATLEAST_MARSHMALLOW) {
                int left = 0, top = 0;
                int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
                if (v instanceof TextView) {
                    // Launch from center of icon, not entire view
                    Drawable icon = Workspace.getTextViewIcon((TextView) v);
                    if (icon != null) {
                        Rect bounds = icon.getBounds();
                        left = (width - bounds.width()) / 2;
                        top = v.getPaddingTop();
                        width = bounds.width();
                        height = bounds.height();
                    }
                }
                opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
            } else if (!Utilities.ATLEAST_LOLLIPOP) {
                // Below L, we use a scale up animation
                opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(),
                        v.getMeasuredHeight());
            } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
                // On L devices, we use the device default slide-up transition.
                // On L MR1 devices, we a custom version of the slide-up transition which
                // doesn't have the delay present in the device default.
                opts = ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim);
            }
            optsBundle = opts != null ? opts.toBundle() : null;
        }

        if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
            StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
            try {
                // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
                // containing file Uris would cause a crash as penaltyDeathOnFileUriExposure
                // is enabled by default on NYC.
                StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
                // Could be launching some bookkeeping activity
                startActivity(intent, optsBundle);
            } finally {
                StrictMode.setVmPolicy(oldPolicy);
            }
        } else {
            // TODO Component can be null when shortcuts are supported for secondary user
            launcherApps.startActivityForProfile(intent.getComponent(), user, intent.getSourceBounds(),
                    optsBundle);
        }
        return true;
    } catch (SecurityException e) {
        if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
            // Due to legacy reasons, direct call shortcuts require Launchers to have the
            // corresponding permission. Show the appropriate permission prompt if that
            // is the case.
            if (intent.getComponent() == null && Intent.ACTION_CALL.equals(intent.getAction())
                    && checkSelfPermission(
                            Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Rename sPendingAddItem to a generic name.
                sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent, 0,
                        (ItemInfo) tag);
                requestPermissions(new String[] { Manifest.permission.CALL_PHONE },
                        REQUEST_PERMISSION_CALL_PHONE);
                return false;
            }
        }
        Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
        Log.e(TAG, "Launcher does not have the permission to launch " + intent
                + ". Make sure to create a MAIN intent-filter for the corresponding activity "
                + "or use the exported attribute for this activity. " + "tag=" + tag + " intent=" + intent, e);
    }
    return false;
}

From source file:com.skytree.epubtest.BookViewActivity.java

public void moveSkyBox(SkyBox box, int boxWidth, int boxHeight, Rect startRect, Rect endRect) {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) box.getLayoutParams();
    int topMargin = ps(80);
    int bottomMargin = ps(80);
    int boxTop = 0;
    int boxLeft = 0;
    int arrowX;// w  ww .  j a  v  a  2  s  . c o m
    boolean isArrowDown;

    if (startRect.top - topMargin > boxHeight) {
        boxTop = startRect.top - boxHeight - ps(10);
        boxLeft = (startRect.left + startRect.width() / 2 - boxWidth / 2);
        arrowX = (startRect.left + startRect.width() / 2);
        isArrowDown = true;
    } else if ((this.getHeight() - endRect.bottom) - bottomMargin > boxHeight) { // ????????? ????????? ????????? ?????????. 
        boxTop = endRect.bottom + ps(10);
        boxLeft = (endRect.left + endRect.width() / 2 - boxWidth / 2);
        arrowX = (endRect.left + endRect.width() / 2);
        isArrowDown = false;
    } else {
        boxTop = ps(100);
        boxLeft = (startRect.left + startRect.width() / 2 - boxWidth / 2);
        arrowX = (startRect.left + startRect.width() / 2);
        isArrowDown = true;
    }

    if (boxLeft + boxWidth > this.getWidth() * .9) {
        boxLeft = (int) (this.getWidth() * .9) - boxWidth;
    } else if (boxLeft < this.getWidth() * .1) {
        boxLeft = (int) (this.getWidth() * .1);
    }

    box.setArrowPosition(arrowX, boxLeft, boxWidth);
    box.setArrowDirection(isArrowDown);
    params.leftMargin = boxLeft;
    params.topMargin = boxTop;
    params.width = boxWidth;
    params.height = boxHeight;
    box.setLayoutParams(params);
    box.invalidate();

    boxFrame = new Rect();
    boxFrame.left = boxLeft;
    boxFrame.top = boxTop;
    boxFrame.right = boxLeft + boxWidth;
    boxFrame.bottom = boxTop + boxHeight;
}

From source file:com.appunite.list.AbsHorizontalListView.java

/**
 * What is the distance between the source and destination rectangles given the direction of
 * focus navigation between them? The direction basically helps figure out more quickly what is
 * self evident by the relationship between the rects...
 *
 * @param source the source rectangle//  w  w  w . ja  v a2 s  . co  m
 * @param dest the destination rectangle
 * @param direction the direction
 * @return the distance between the rectangles
 */
static int getDistance(Rect source, Rect dest, int direction) {
    int sX, sY; // source x, y
    int dX, dY; // dest x, y
    switch (direction) {
    case View.FOCUS_RIGHT:
        sX = source.right;
        sY = source.top + source.height() / 2;
        dX = dest.left;
        dY = dest.top + dest.height() / 2;
        break;
    case View.FOCUS_DOWN:
        sX = source.left + source.width() / 2;
        sY = source.bottom;
        dX = dest.left + dest.width() / 2;
        dY = dest.top;
        break;
    case View.FOCUS_LEFT:
        sX = source.left;
        sY = source.top + source.height() / 2;
        dX = dest.right;
        dY = dest.top + dest.height() / 2;
        break;
    case View.FOCUS_UP:
        sX = source.left + source.width() / 2;
        sY = source.top;
        dX = dest.left + dest.width() / 2;
        dY = dest.bottom;
        break;
    case View.FOCUS_FORWARD:
    case View.FOCUS_BACKWARD:
        sX = source.right + source.width() / 2;
        sY = source.top + source.height() / 2;
        dX = dest.left + dest.width() / 2;
        dY = dest.top + dest.height() / 2;
        break;
    default:
        throw new IllegalArgumentException("direction must be one of "
                + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, " + "FOCUS_FORWARD, FOCUS_BACKWARD}.");
    }
    int deltaX = dX - sX;
    int deltaY = dY - sY;
    return deltaY * deltaY + deltaX * deltaX;
}

From source file:com.aliasapps.seq.scroller.TwoWayView.java

/**
 * What is the distance between the source and destination rectangles given the direction of
 * focus navigation between them? The direction basically helps figure out more quickly what is
 * self evident by the relationship between the rects...
 *
 * @param source the source rectangle//  w ww .  j  a  v a 2 s . c  o  m
 * @param dest the destination rectangle
 * @param direction the direction
 * @return the distance between the rectangles
 */
private static int getDistance(Rect source, Rect dest, int direction) {
    int sX, sY; // source x, y
    int dX, dY; // dest x, y

    switch (direction) {
    case View.FOCUS_RIGHT:
        sX = source.right;
        sY = source.top + source.height() / 2;
        dX = dest.left;
        dY = dest.top + dest.height() / 2;
        break;

    case View.FOCUS_DOWN:
        sX = source.left + source.width() / 2;
        sY = source.bottom;
        dX = dest.left + dest.width() / 2;
        dY = dest.top;
        break;

    case View.FOCUS_LEFT:
        sX = source.left;
        sY = source.top + source.height() / 2;
        dX = dest.right;
        dY = dest.top + dest.height() / 2;
        break;

    case View.FOCUS_UP:
        sX = source.left + source.width() / 2;
        sY = source.top;
        dX = dest.left + dest.width() / 2;
        dY = dest.bottom;
        break;

    case View.FOCUS_FORWARD:
    case View.FOCUS_BACKWARD:
        sX = source.right + source.width() / 2;
        sY = source.top + source.height() / 2;
        dX = dest.left + dest.width() / 2;
        dY = dest.top + dest.height() / 2;
        break;

    default:
        throw new IllegalArgumentException("direction must be one of "
                + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, " + "FOCUS_FORWARD, FOCUS_BACKWARD}.");
    }

    int deltaX = dX - sX;
    int deltaY = dY - sY;

    return deltaY * deltaY + deltaX * deltaX;
}

From source file:t3.giftbook.util.FlipViewPager.java

/**
 * ??/*from   w  w  w. ja va 2 s .  c  o m*/
 */
private void drawFlippingPage(Canvas canvas) {
    if (mOffset == 0) {
        //?????????????Flip?????(draw??)
        return;
    }

    int scrollX = this.getScrollX();

    if (currentPageBmp == null || currentPageBmp.isRecycled() || nextPageBmp.isRecycled()) {
        currentPageBmp = prepareBmp(canvas);
        currentPageCnv = new Canvas(currentPageBmp);
        nextPageBmp = prepareBmp(canvas);
        nextPageCnv = new Canvas(nextPageBmp);
    }

    if (currentPageBmp != null) {
        final int currentPage = mPosition;
        final int nextPage = currentPage + 1;

        if (currentPageCnv != null) {
            if (lastDrawedPage != currentPage) {
                try {
                    //mPosition????????bounds????
                    //1???mOffset??????return.
                    //???????????mPosition???????

                    final long time = System.currentTimeMillis();
                    drawChildTo(currentPageCnv, currentPage, time);
                    drawChildTo(nextPageCnv, nextPage, time);

                    //??currentPageBmp, nextPageBmp????????????
                    lastDrawedPage = currentPage;

                } catch (IllegalArgumentException e) {
                    Log.w(TAG, e);
                }
            }
        }

        /*
         * canvas?????????????
         * ???????????
         * ?????????????
         */

        //?
        final Rect screenLeftSide = new Rect(scrollX, 0, scrollX + this.getWidth() / 2, this.getHeight());
        //??
        final Rect screenRightSide = new Rect(scrollX + this.getWidth() / 2, 0, scrollX + this.getWidth() / 1,
                this.getHeight());

        //bmp?
        final Rect pageLeftSide = new Rect((int) (this.getWidth() * 0.0f), 0, (int) (this.getWidth() * 0.5f),
                this.getHeight());

        //bmp??
        final Rect pageRightSide = new Rect((int) (this.getWidth() * 0.5f), 0, (int) (this.getWidth() * 1.0f),
                this.getHeight());

        /*
         * ???Flip??
         */

        //?
        Paint white = new Paint() {
            {
                this.setColor(Color.WHITE);
            }
        };
        canvas.drawRect(screenLeftSide, white);
        canvas.drawRect(screenRightSide, white);

        //???????
        canvas.drawBitmap(currentPageBmp, pageLeftSide, screenLeftSide, new Paint());

        //????????
        canvas.drawBitmap(nextPageBmp, pageRightSide, screenRightSide, new Paint());

        //
        Paint shadow = new Paint() {
            {
                this.setColor(Color.BLACK);
            }
        };

        final int meshCols = 30;
        final float meshColsf = (float) meshCols;
        final int meshRows = 30;
        final float meshRowsf = (float) meshRows;

        //??????????
        if (mOffset <= 0.5) {
            final float offsetRate = (0.5f - mOffset) * 2;
            final float invOffsetRate = 1f - offsetRate;

            //???
            shadow.setAlpha((int) (90 * offsetRate));
            canvas.drawRect(screenRightSide, shadow);

            //???????????
            final float[] rVerts = new float[(meshCols + 1) * (meshRows + 1) * 2];
            for (int meshY = 0; meshY <= meshRows; meshY++) {
                final float meshYf = (float) meshY;
                for (int meshX = 0; meshX <= meshCols; meshX++) {
                    final float meshXf = (float) meshX;

                    final int meshPosition = (meshX + meshY * (meshCols + 1)) * 2;
                    final int xPosition = meshPosition;
                    final int yPosition = xPosition + 1;

                    if (meshX <= meshCols / 2) {
                        rVerts[xPosition] = screenRightSide.left; //??????????
                        rVerts[yPosition] = screenRightSide.top + screenRightSide.height() / meshRowsf * meshYf;
                    } else {
                        rVerts[xPosition] = screenRightSide.left + offsetRate * screenRightSide.width()
                                / meshColsf * (meshXf - meshColsf / 2f) * 2f;
                        rVerts[yPosition] = screenRightSide.top + screenRightSide.height() / 2f
                                - screenRightSide.height() / meshRowsf * (meshRowsf / 2f - meshYf)
                                        * (1f + invOffsetRate / meshColsf * (meshXf - meshColsf / 2f));
                    }
                }
            }
            canvas.drawBitmapMesh(currentPageBmp, meshCols, meshRows, rVerts, 0, null, 0, null);

            //????
            final Paint pageRootPaint = new Paint() {
                {
                    int shadow1 = Color.argb((int) (30 * invOffsetRate), 0, 0, 0);
                    int shadow2 = Color.argb(0, 0, 0, 0);
                    this.setShader(new LinearGradient((float) screenRightSide.left,
                            (float) screenRightSide.top + screenRightSide.height() / 2,
                            (float) screenRightSide.left + screenRightSide.width() * offsetRate,
                            (float) screenRightSide.top + screenRightSide.height() / 2, shadow1, shadow2,
                            Shader.TileMode.CLAMP));
                }
            };
            canvas.drawRect(screenRightSide.left, screenRightSide.top, screenRightSide.right,
                    screenRightSide.bottom, pageRootPaint);

        } else {
            final float offsetRate = (mOffset - 0.5f) * 2;
            final float invOffsetRate = 1f - offsetRate;

            //??
            shadow.setAlpha((int) (90 * offsetRate));
            canvas.drawRect(screenLeftSide, shadow);

            //????????
            final float[] rVerts = new float[(meshCols + 1) * (meshRows + 1) * 2];
            for (int meshY = 0; meshY <= meshRows; meshY++) {
                final float meshYf = (float) meshY;
                for (int meshX = 0; meshX <= meshCols; meshX++) {
                    final float meshXf = (float) meshX;
                    final float meshXfInv = meshColsf / 2f - meshXf;

                    final int meshPosition = (meshX + meshY * (meshCols + 1)) * 2;
                    final int xPosition = meshPosition;
                    final int yPosition = xPosition + 1;

                    if (meshX >= meshCols / 2) {
                        rVerts[xPosition] = screenLeftSide.right; //???????????
                        rVerts[yPosition] = screenLeftSide.top + screenLeftSide.height() / meshRowsf * meshYf;
                    } else {
                        rVerts[xPosition] = screenLeftSide.right - offsetRate * screenLeftSide.width()
                                + offsetRate * screenLeftSide.width() / meshColsf * (meshXf) * 2f;
                        rVerts[yPosition] = screenLeftSide.top + screenLeftSide.height() / 2f
                                - screenLeftSide.height() / meshRowsf * (meshRowsf / 2f - meshYf)
                                        * (1f + invOffsetRate / meshColsf * (meshColsf / 2f - meshXf));
                    }
                }
            }
            canvas.drawBitmapMesh(nextPageBmp, meshCols, meshRows, rVerts, 0, null, 0, null);

            //???
            final Paint pageRootPaint = new Paint() {
                {
                    int shadow2 = Color.argb((int) (30 * invOffsetRate), 0, 0, 0);
                    int shadow1 = Color.argb(0, 0, 0, 0);
                    this.setShader(new LinearGradient(
                            (float) screenLeftSide.right - screenRightSide.width() * offsetRate,
                            (float) screenLeftSide.top + screenRightSide.height() / 2,
                            (float) screenLeftSide.right,
                            (float) screenLeftSide.top + screenRightSide.height() / 2, shadow1, shadow2,
                            Shader.TileMode.CLAMP));
                }
            };
            canvas.drawRect(screenLeftSide.left, screenLeftSide.top, screenLeftSide.right,
                    screenLeftSide.bottom, pageRootPaint);
        }

        //           int y = 0;
        //           canvas.drawRect(0,0,canvas.getWidth(), 300, new Paint() {{this.setColor(Color.WHITE); this.setAlpha(100);}});
        //           Paint debugPrint = new Paint() {{this.setColor(Color.RED); this.setTextSize(30);}};
        //           canvas.drawText("b.width     = " + bkbmp.getWidth()   , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("b.height    = " + bkbmp.getHeight()  , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("c.width     = " + canvas.getWidth()  , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("c.height    = " + canvas.getHeight() , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //         canvas.drawText("scrollX       = " + getScrollX()     , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("mPosition     = " + mPosition        , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("lastPosition  = " + lastDrawedPage   , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("mOffset       = " + mOffset          , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("offsetRate    = " + offsetRate       , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("invOffsetRate = " + invOffsetRate    , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("mOffsetPixels = " + mOffsetPixels    , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);
        //           canvas.drawText("index         = " + getCurrentItem() , scrollX, (++y) * debugPrint.getTextSize(), debugPrint);

    }
}

From source file:com.appunite.list.AbsHorizontalListView.java

/**
 * Allows RemoteViews to scroll relatively to a position.
 *///from   w  ww .j  av  a  2  s.c  o m
void smoothScrollByOffset(int position) {
    int index = -1;
    if (position < 0) {
        index = getFirstVisiblePosition();
    } else if (position > 0) {
        index = getLastVisiblePosition();
    }

    if (index > -1) {
        View child = getChildAt(index - getFirstVisiblePosition());
        if (child != null) {
            Rect visibleRect = new Rect();
            if (child.getGlobalVisibleRect(visibleRect)) {
                // the child is partially visible
                int childRectArea = child.getWidth() * child.getHeight();
                int visibleRectArea = visibleRect.width() * visibleRect.height();
                float visibleArea = (visibleRectArea / (float) childRectArea);
                final float visibleThreshold = 0.75f;
                if ((position < 0) && (visibleArea < visibleThreshold)) {
                    // the top index is not perceivably visible so offset
                    // to account for showing that top index as well
                    ++index;
                } else if ((position > 0) && (visibleArea < visibleThreshold)) {
                    // the bottom index is not perceivably visible so offset
                    // to account for showing that bottom index as well
                    --index;
                }
            }
            smoothScrollToPosition(Math.max(0, Math.min(getCount(), index + position)));
        }
    }
}

From source file:com.zoffcc.applications.zanavi.Navit.java

synchronized static int find_max_font_size_for_width(String sample_text, int width, int max_font_size,
        int padding_in_dp) {
    final String s = sample_text;
    int bh = 0;/*  w  w  w  .j a va  2 s .  co  m*/
    Paint p = new Paint();
    Rect bounds = new Rect();
    p.setTextSize(max_font_size);
    p.getTextBounds(s, 0, s.length(), bounds);

    int ret_font_size = max_font_size;

    int loop_counter_max = 400;
    int loop_counter = 0;
    int padding_in_px = 0;
    if (padding_in_dp > 0) {
        padding_in_px = NavitGraphics.dp_to_px(padding_in_dp);
    }

    bh = bounds.width();
    while ((bh + padding_in_px) > width) {
        loop_counter++;
        if (loop_counter > loop_counter_max) {
            break;
        }

        ret_font_size--;
        p.setTextSize(ret_font_size);
        p.getTextBounds(s, 0, s.length(), bounds);
        bh = bounds.width();
        // mt = p.measureText(s);
    }

    return ret_font_size;
}