Java Code Examples for android.graphics.Rect#centerY()

The following examples show how to use android.graphics.Rect#centerY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TilesOverlay.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * This is meant to be a "pure" tile drawing function that doesn't take into account
 * osmdroid-specific characteristics (like osmdroid's canvas's having 0,0 as the center rather
 * than the upper-left corner). Once the tile is ready to be drawn, it is passed to
 * onTileReadyToDraw where custom manipulations can be made before drawing the tile.
 */
public void drawTiles(final Canvas c, final float zoomLevel, final int tileSizePx,
                      final Rect viewPort, final Rect pClipRect) {

    mNuberOfTiles = mTileLooper.loop(c, mTileProvider.getCacheKey(), zoomLevel, tileSizePx, viewPort, pClipRect);

    // draw a cross at center in debug mode
    if (UtilConstants.DEBUGMODE) {
        ISafeCanvas canvas = (ISafeCanvas) c;
        final Point centerPoint =
                new Point(viewPort.centerX() - mWorldSize_2, viewPort.centerY() - mWorldSize_2);
        canvas.drawLine(centerPoint.x, centerPoint.y - 9, centerPoint.x, centerPoint.y + 9,
                getDebugPaint());
        canvas.drawLine(centerPoint.x - 9, centerPoint.y, centerPoint.x + 9, centerPoint.y,
                getDebugPaint());
    }
}
 
Example 2
Source File: AlmostRippleDrawable.java    From Genius-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas, Paint paint) {
    final float scale = mCurrentScale;
    if (scale > 0) {
        Rect bounds = getBounds();

        float radius = (Math.min(bounds.width(), bounds.height()) / 2.0f);
        float x = bounds.centerX();
        float y = bounds.centerY();

        // Background
        if (scale != 1f && mBackgroundAlpha > 0) {
            paint.setAlpha(mBackgroundAlpha);
            canvas.drawCircle(x, y, radius, paint);
        }

        // Ripple
        if (mRippleAlpha > 0) {
            paint.setAlpha(mRippleAlpha);
            canvas.drawCircle(x, y, radius * scale, paint);
        }
    }
}
 
Example 3
Source File: LoadingCircleDrawable.java    From Genius-Android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    if (bounds.left == 0 && bounds.top == 0 && bounds.right == 0 && bounds.bottom == 0) {
        return;
    }

    final int centerX = bounds.centerX();
    final int centerY = bounds.centerY();

    final int radius = Math.min(bounds.height(), bounds.width()) >> 1;
    final int maxStrokeRadius = ((int) Math.max(getForegroundLineSize(), getBackgroundLineSize()) >> 1) + 1;
    final int areRadius = radius - maxStrokeRadius;

    mOval.set(centerX - areRadius, centerY - areRadius, centerX + areRadius, centerY + areRadius);
}
 
Example 4
Source File: FilmstripView.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
/**
 * Layouts the view in the area assuming the center of the area is at a
 * specific point of the whole filmstrip.
 *
 * @param drawArea  The area when filmstrip will show in.
 * @param refCenter The absolute X coordination in the whole filmstrip
 *                  of the center of {@code drawArea}.
 * @param scale     The scale of the view on the filmstrip.
 */
public void layoutWithTranslationX(Rect drawArea, int refCenter, float scale)
{
    final float translationX =
            ((mTranslationXAnimator != null && mTranslationXAnimator.isRunning()) ?
                    (Float) mTranslationXAnimator.getAnimatedValue() : 0);
    int left =
            (int) (drawArea.centerX() + (mLeftPosition - refCenter + translationX) * scale);
    int top = (int) (drawArea.centerY() - (mView.getMeasuredHeight() / 2) * scale);
    layoutAt(left, top);
    mView.setScaleX(scale);
    mView.setScaleY(scale);

    // update mViewArea for touch detection.
    int l = mView.getLeft();
    int t = mView.getTop();
    mViewArea.set(l, t,
            l + mView.getMeasuredWidth() * scale,
            t + mView.getMeasuredHeight() * scale);
}
 
Example 5
Source File: UiObject.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Performs a two-pointer gesture, where each pointer moves diagonally
 * toward the other, from the edges to the center of this UiObject .
 * @param percent percentage of the object's diagonal length for the pinch gesture
 * @param steps the number of steps for the gesture. Steps are injected 
 * about 5 milliseconds apart, so 100 steps may take around 0.5 seconds to complete.
 * @return <code>true</code> if all touch events for this gesture are injected successfully,
 *         <code>false</code> otherwise
 * @throws UiObjectNotFoundException
 * @since API Level 18
 */
public boolean pinchIn(int percent, int steps) throws UiObjectNotFoundException {
    // make value between 1 and 100
    percent = (percent < 0) ? 0 : (percent > 100) ? 100 : percent;
    float percentage = percent / 100f;

    AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
    if (node == null) {
        throw new UiObjectNotFoundException(mUiSelector.toString());
    }

    Rect rect = getVisibleBounds(node);
    if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2)
        throw new IllegalStateException("Object width is too small for operation");

    Point startPoint1 = new Point(rect.centerX() - (int)((rect.width()/2) * percentage),
            rect.centerY());
    Point startPoint2 = new Point(rect.centerX() + (int)((rect.width()/2) * percentage),
            rect.centerY());

    Point endPoint1 = new Point(rect.centerX() - FINGER_TOUCH_HALF_WIDTH, rect.centerY());
    Point endPoint2 = new Point(rect.centerX() + FINGER_TOUCH_HALF_WIDTH, rect.centerY());

    return performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, steps);
}
 
Example 6
Source File: SideFragment.java    From StarWars.Android with MIT License 6 votes vote down vote up
@Override
public void onSideSwitch(SwitchCompat v) {
    Rect rect = new Rect();
    v.getGlobalVisibleRect(rect);
    final int cy = rect.centerY() - getStatusBarHeight();
    final int halfThumbWidth = v.getThumbDrawable().getIntrinsicWidth() / 2;
    final int cx;

    if (this instanceof BrightSideFragment && v.isChecked()) {
        cx = rect.right - halfThumbWidth;
        postGoToSide(cy, cx, "dark");
    } else if (!v.isChecked()){
        cx = rect.left + halfThumbWidth;
        postGoToSide(cy, cx, "bright");
    }
}
 
Example 7
Source File: ClockDrawable.java    From ClockDrawableAnimation with Apache License 2.0 6 votes vote down vote up
@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);

    rimRadius = Math.min(bounds.width(), bounds.height()) / 2f - rimPaint.getStrokeWidth();
    faceRadius = rimRadius - rimPaint.getStrokeWidth();
    screwRadius = rimPaint.getStrokeWidth() * 2;
    float hourHandLength = (float) (0.5 * faceRadius);
    float minuteHandLength = (float) (0.7 * faceRadius);
    float top = bounds.centerY() - screwRadius;

    hourHandPath.reset();
    hourHandPath.moveTo(bounds.centerX(), bounds.centerY());
    hourHandPath.addRect(bounds.centerX(), top, bounds.centerX(), top - hourHandLength, Direction.CCW);
    hourHandPath.close();

    minuteHandPath.reset();
    minuteHandPath.moveTo(bounds.centerX(), bounds.centerY());
    minuteHandPath.addRect(bounds.centerX(), top, bounds.centerX(), top - minuteHandLength, Direction.CCW);
    minuteHandPath.close();
}
 
Example 8
Source File: PlayPauseDrawable.java    From Material-Animation-Samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);

    int padding = bounds.centerX()/2;
    shiftY = bounds.centerY()/8;
    mBounds.left = bounds.left + padding;
    mBounds.right = bounds.right - padding;
    mBounds.top = bounds.top + padding;
    mBounds.bottom = bounds.bottom - padding;

    setupPauseMode();
}
 
Example 9
Source File: Emoji.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public Rect getDrawRect() {
    Rect original = getBounds();
    int cX = original.centerX(), cY = original.centerY();
    rect.left = cX - (fullSize ? bigImgSize : drawImgSize) / 2;
    rect.right = cX + (fullSize ? bigImgSize : drawImgSize) / 2;
    rect.top = cY - (fullSize ? bigImgSize : drawImgSize) / 2;
    rect.bottom = cY + (fullSize ? bigImgSize : drawImgSize) / 2;
    return rect;
}
 
Example 10
Source File: PlayPauseDrawable.java    From PlayPauseDrawable with Apache License 2.0 5 votes vote down vote up
@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);

    int padding = bounds.centerX()/2;
    shiftY = bounds.centerY()/8;
    mBounds.left = bounds.left + padding;
    mBounds.right = bounds.right - padding;
    mBounds.top = bounds.top + padding;
    mBounds.bottom = bounds.bottom - padding;

    setupPauseMode();
}
 
Example 11
Source File: TapTargetView.java    From styT with Apache License 2.0 5 votes vote down vote up
int getOuterCircleRadius(int centerX, int centerY, Rect textBounds, Rect targetBounds) {
    final int targetCenterX = targetBounds.centerX();
    final int targetCenterY = targetBounds.centerY();
    final int expandedRadius = (int) (1.1f * TARGET_RADIUS);
    final Rect expandedBounds = new Rect(targetCenterX, targetCenterY, targetCenterX, targetCenterY);
    expandedBounds.inset(-expandedRadius, -expandedRadius);

    final int textRadius = maxDistanceToPoints(centerX, centerY, textBounds);
    final int targetRadius = maxDistanceToPoints(centerX, centerY, expandedBounds);
    return Math.max(textRadius, targetRadius) + CIRCLE_PADDING;
}
 
Example 12
Source File: TapTargetView.java    From TapTargetView with Apache License 2.0 5 votes vote down vote up
int getOuterCircleRadius(int centerX, int centerY, Rect textBounds, Rect targetBounds) {
  final int targetCenterX = targetBounds.centerX();
  final int targetCenterY = targetBounds.centerY();
  final int expandedRadius = (int) (1.1f * TARGET_RADIUS);
  final Rect expandedBounds = new Rect(targetCenterX, targetCenterY, targetCenterX, targetCenterY);
  expandedBounds.inset(-expandedRadius, -expandedRadius);

  final int textRadius = maxDistanceToPoints(centerX, centerY, textBounds);
  final int targetRadius = maxDistanceToPoints(centerX, centerY, expandedBounds);
  return Math.max(textRadius, targetRadius) + CIRCLE_PADDING;
}
 
Example 13
Source File: FloatDrawable.java    From UPMiss with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    mCenterX = bounds.centerX();
    mCenterY = bounds.centerY();

    mEndRadius = Math.min(bounds.right - bounds.left, bounds.bottom - bounds.top) >> 1;
}
 
Example 14
Source File: CellLayout.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
        int spanY, View dragView, int[] resultDirection) {
    int[] targetDestination = new int[2];

    findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
    Rect dragRect = new Rect();
    regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
    dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());

    Rect dropRegionRect = new Rect();
    getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
            dragView, dropRegionRect, mIntersectingViews);

    int dropRegionSpanX = dropRegionRect.width();
    int dropRegionSpanY = dropRegionRect.height();

    regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
            dropRegionRect.height(), dropRegionRect);

    int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
    int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;

    if (dropRegionSpanX == mCountX || spanX == mCountX) {
        deltaX = 0;
    }
    if (dropRegionSpanY == mCountY || spanY == mCountY) {
        deltaY = 0;
    }

    if (deltaX == 0 && deltaY == 0) {
        // No idea what to do, give a random direction.
        resultDirection[0] = 1;
        resultDirection[1] = 0;
    } else {
        computeDirectionVector(deltaX, deltaY, resultDirection);
    }
}
 
Example 15
Source File: FreshDownloadView.java    From FreshDownloadView with Apache License 2.0 5 votes vote down vote up
private void makeErrorPath() {
    final Rect bounds = this.bounds;
    final int w2 = bounds.centerX();
    final int h2 = bounds.centerY();
    path1.reset();
    path1.moveTo((float) (w2 - Math.cos(Math.toRadians(45)) * getRadius()), (float) (h2 - Math.sin(Math.toRadians(45)) * getRadius()));
    path1.lineTo((float) (w2 + Math.cos(Math.toRadians(45)) * getRadius()), (float) (h2 + Math.sin(Math.toRadians(45)) * getRadius()));
    pathMeasure1.setPath(path1, false);
    mErrorPathLengthLeft = pathMeasure1.getLength();
    path1.reset();
    path1.moveTo((float) (w2 + Math.cos(Math.toRadians(45)) * getRadius()), (float) (h2 - Math.sin(Math.toRadians(45)) * getRadius()));
    path1.lineTo((float) (w2 - Math.cos(Math.toRadians(45)) * getRadius()), (float) (h2 + Math.sin(Math.toRadians(45)) * getRadius()));
    pathMeasure2.setPath(path1, false);
    mErrorPathLengthRight = pathMeasure2.getLength();
}
 
Example 16
Source File: UiObject.java    From za-Farmer with MIT License 5 votes vote down vote up
/**
 * Performs a two-pointer gesture, where each pointer moves diagonally
 * opposite across the other, from the center out towards the edges of the
 * this UiObject.
 * @param percent percentage of the object's diagonal length for the pinch gesture
 * @param steps the number of steps for the gesture. Steps are injected 
 * about 5 milliseconds apart, so 100 steps may take around 0.5 seconds to complete.
 * @return <code>true</code> if all touch events for this gesture are injected successfully,
 *         <code>false</code> otherwise
 * @throws UiObjectNotFoundException
 * @since API Level 18
 */
public boolean pinchOut(int percent, int steps) throws UiObjectNotFoundException {
    // make value between 1 and 100
    percent = (percent < 0) ? 1 : (percent > 100) ? 100 : percent;
    float percentage = percent / 100f;

    AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
    if (node == null) {
        throw new UiObjectNotFoundException(mUiSelector.toString());
    }

    Rect rect = getVisibleBounds(node);
    if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2)
        throw new IllegalStateException("Object width is too small for operation");

    // start from the same point at the center of the control
    Point startPoint1 = new Point(rect.centerX() - FINGER_TOUCH_HALF_WIDTH, rect.centerY());
    Point startPoint2 = new Point(rect.centerX() + FINGER_TOUCH_HALF_WIDTH, rect.centerY());

    // End at the top-left and bottom-right corners of the control
    Point endPoint1 = new Point(rect.centerX() - (int)((rect.width()/2) * percentage),
            rect.centerY());
    Point endPoint2 = new Point(rect.centerX() + (int)((rect.width()/2) * percentage),
            rect.centerY());

    return performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, steps);
}
 
Example 17
Source File: CellContainer.java    From openlauncher with Apache License 2.0 5 votes vote down vote up
private void drawCachedOutlineBitmap(Canvas canvas, Rect cell) {
    if (_cachedOutlineBitmap != null) {
        Bitmap bitmap = _cachedOutlineBitmap;
        float centerX = cell.centerX();
        float centerY = cell.centerY();
        canvas.drawBitmap(bitmap, centerX - (bitmap.getWidth() / 2), centerY - (bitmap.getHeight() / 2), _outlinePaint);
    }
}
 
Example 18
Source File: SidePropagation.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public long getStartDelay(ViewGroup sceneRoot, Transition transition,
        TransitionValues startValues, TransitionValues endValues) {
    if (startValues == null && endValues == null) {
        return 0;
    }
    int directionMultiplier = 1;
    Rect epicenter = transition.getEpicenter();
    TransitionValues positionValues;
    if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
        positionValues = startValues;
        directionMultiplier = -1;
    } else {
        positionValues = endValues;
    }

    int viewCenterX = getViewX(positionValues);
    int viewCenterY = getViewY(positionValues);

    int[] loc = new int[2];
    sceneRoot.getLocationOnScreen(loc);
    int left = loc[0] + Math.round(sceneRoot.getTranslationX());
    int top = loc[1] + Math.round(sceneRoot.getTranslationY());
    int right = left + sceneRoot.getWidth();
    int bottom = top + sceneRoot.getHeight();

    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        epicenterX = (left + right) / 2;
        epicenterY = (top + bottom) / 2;
    }

    float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY,
            left, top, right, bottom);
    float maxDistance = getMaxDistance(sceneRoot);
    float distanceFraction = distance/maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}
 
Example 19
Source File: OverlayController.java    From talkback with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
void showToolTip(int toolTipToShowViewId, Rect itemBounds, SwitchAccessMenuLayout menuLayout) {
  View toolTipToShow = menuOverlay.findViewById(toolTipToShowViewId);
  if (toolTipToShow == null) {
    return;
  }

  // We don't need to hide the previous tool tip if it is the same one we want to show now.
  // However, we still need to adjust the margins of the tool tip (below) because the horizontal
  // position of the item may have changed even if the vertical position hasn't.
  if (lastToolTipViewIdShown != toolTipToShowViewId) {
    menuOverlay.findViewById(lastToolTipViewIdShown).setVisibility(View.GONE);
    toolTipToShow.setVisibility(View.VISIBLE);
    lastToolTipViewIdShown = toolTipToShowViewId;
  }

  // The length of the tool tip is the longer dimension between its height and width. Since all
  // tool tips are just transposed versions of each other, the width of the tool tip up
  // represents the longest dimension for all tool tips.
  int toolTipLength = menuOverlay.findViewById(R.id.tooltip_up).getWidth();
  MarginLayoutParams toolTipMargins = (MarginLayoutParams) toolTipToShow.getLayoutParams();
  toolTipMargins.setMargins(0, 0, 0, 0);
  if ((toolTipToShowViewId == R.id.tooltip_up) || (toolTipToShowViewId == R.id.tooltip_down)) {
    Point screenSize = ScreenUtils.getRealScreenSize(getContext());
    boolean isItemNearLeftEdge = itemBounds.centerX() < (screenSize.x / 2);
    int distanceFromItemCenterToScreenEdge =
        isItemNearLeftEdge ? itemBounds.centerX() : (screenSize.x - itemBounds.centerX());
    int distanceFromToolTipCenterToScreenEdge =
        (toolTipLength / 2) + menuOverlay.findViewById(R.id.menu_layout).getLeft();
    if (distanceFromItemCenterToScreenEdge > minDistanceFromToolTipToScreenEdge) {
      // Place the center of the tooltip at the center of the item.
      toolTipMargins.leftMargin = itemBounds.centerX() - distanceFromToolTipCenterToScreenEdge;
    } else {
      // Place the center of the tooltip at the item edge furthest from the screen edge.
      int itemEdge = isItemNearLeftEdge ? itemBounds.right : itemBounds.left;
      toolTipMargins.leftMargin = itemEdge - distanceFromToolTipCenterToScreenEdge;
    }
  } else {
    MarginLayoutParams menuMargins =
        (MarginLayoutParams) menuOverlay.findViewById(R.id.menu_layout).getLayoutParams();
    toolTipMargins.topMargin = itemBounds.centerY() - (toolTipLength / 2) - menuMargins.topMargin;
  }
  toolTipToShow.setLayoutParams(toolTipMargins);

  // Set the tooltip and content so a border can be drawn around the menu.
  menuLayout.setToolTipView(toolTipToShow);
}
 
Example 20
Source File: HighlightView.java    From bither-android with Apache License 2.0 4 votes vote down vote up
public int getHit(float x, float y) {
	Rect r = computeLayout();
	final float hysteresis = 35F;
	int retval = GROW_NONE;

	if (mCircle) {
		float distX = x - r.centerX();
		float distY = y - r.centerY();
		int distanceFromCenter = (int) Math.sqrt(distX * distX + distY
				* distY);
		int radius = mDrawRect.width() / 2;
		int delta = distanceFromCenter - radius;
		if (Math.abs(delta) <= hysteresis) {
			if (Math.abs(distY) > Math.abs(distX)) {
				if (distY < 0) {
					retval = GROW_TOP_EDGE;
				} else {
					retval = GROW_BOTTOM_EDGE;
				}
			} else {
				if (distX < 0) {
					retval = GROW_LEFT_EDGE;
				} else {
					retval = GROW_RIGHT_EDGE;
				}
			}
		} else if (distanceFromCenter < radius) {
			retval = MOVE;
		} else {
			retval = GROW_NONE;
		}
	} else {
		// verticalCheck makes sure the position is between the top and
		// the bottom edge (with some tolerance). Similar for horizCheck.
		boolean verticalCheck = (y >= r.top - hysteresis)
				&& (y < r.bottom + hysteresis);
		boolean horizCheck = (x >= r.left - hysteresis)
				&& (x < r.right + hysteresis);

		// Check whether the position is near some edge(s).
		if ((Math.abs(r.left - x) < hysteresis) && verticalCheck) {
			retval |= GROW_LEFT_EDGE;
		}
		if ((Math.abs(r.right - x) < hysteresis) && verticalCheck) {
			retval |= GROW_RIGHT_EDGE;
		}
		if ((Math.abs(r.top - y) < hysteresis) && horizCheck) {
			retval |= GROW_TOP_EDGE;
		}
		if ((Math.abs(r.bottom - y) < hysteresis) && horizCheck) {
			retval |= GROW_BOTTOM_EDGE;
		}

		// Not near any edge but inside the rectangle: move.
		if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
			retval = MOVE;
		}
	}
	return retval;
}