List of usage examples for android.graphics Rect centerY
public final int centerY()
From source file:com.albedinsky.android.ui.widget.SeekBarWidget.java
/** * Draws discrete interval, its thick marks depending on the current {@link #mDiscreteIntervalRatio} * value.//w w w.j a va 2s. c o m * * @param canvas Canvas on which to draw discrete interval. */ private void drawDiscreteInterval(Canvas canvas) { if (mDiscreteIntervalRatio == 0 || mProgressDrawable == null) { return; } final Rect trackBounds = mProgressDrawable.getBounds(); final int trackLeft = getPaddingLeft(); final int cy = trackBounds.centerY(); float trackWidth = trackBounds.width(); final float discreteInterval = mDiscreteIntervalRatio * trackWidth; trackWidth += DISCRETE_INTERVAL_TICK_MARK_INFO.radius; final Rect thumbBounds = mThumb != null ? mThumb.getBounds() : null; final int thumbOffset = getThumbOffset(); float cx = 0; while (cx <= trackWidth) { // Ensure to not draw over thumb if it is not expected behaviour. final boolean isAtThumbPosition = thumbBounds != null && trackLeft + cx >= thumbBounds.left + thumbOffset && trackLeft + cx <= thumbBounds.right + thumbOffset; if (CAN_DRAW_DISCRETE_INTERVAL_OVER_THUMB || !isAtThumbPosition) { canvas.drawCircle(trackLeft + cx, cy, DISCRETE_INTERVAL_TICK_MARK_INFO.radius, DISCRETE_INTERVAL_TICK_MARK_INFO.paint); } cx += discreteInterval; } }
From source file:com.android.launcher3.CellLayout.java
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);//from w ww .j av a 2s.c o m 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); } }
From source file:com.albedinsky.android.ui.widget.SeekBarWidget.java
/** * Draws discrete indicator of this SeekBarWidget at its current position updated by * {@link #updateDiscreteIndicatorPosition(int, int)} according to the current progress. * * @param canvas Canvas on which to draw discrete indicator's drawable. *//*from w ww . j ava 2 s. c o m*/ private void drawDiscreteIndicator(Canvas canvas) { if (mDiscreteIndicatorHeight == 0) { return; } // todo: draw according to LTR/RTL layout direction. mDiscreteIndicator.draw(canvas); // Draw current progress over indicator's graphics. final Rect indicatorBounds = mDiscreteIndicator.getBounds(); final Paint textPaint = DISCRETE_INDICATOR_TEXT_INFO.paint; textPaint.getTextBounds("0", 0, 1, mRect); final float textSize = mRect.height(); final Rect textPadding = DISCRETE_INDICATOR_TEXT_INFO.padding; final int absoluteTextGravity = WidgetGravity.getAbsoluteGravity(DISCRETE_INDICATOR_TEXT_INFO.gravity, ViewCompat.getLayoutDirection(this)); final float textX, textY; // Resolve horizontal text position according to the requested gravity. switch (absoluteTextGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { case Gravity.CENTER_HORIZONTAL: textPaint.setTextAlign(Paint.Align.CENTER); textX = indicatorBounds.centerX(); break; case Gravity.RIGHT: textPaint.setTextAlign(Paint.Align.RIGHT); textX = indicatorBounds.right - textPadding.right; break; case Gravity.LEFT: default: textPaint.setTextAlign(Paint.Align.LEFT); textX = indicatorBounds.left + textPadding.left; break; } // Resolve vertical text position according to the requested gravity. switch (absoluteTextGravity & Gravity.VERTICAL_GRAVITY_MASK) { case Gravity.CENTER_VERTICAL: textY = indicatorBounds.centerY() + textSize / 2f; break; case Gravity.BOTTOM: textY = indicatorBounds.bottom - textPadding.bottom; break; case Gravity.TOP: default: textY = indicatorBounds.top + textSize + textPadding.top; break; } canvas.drawText(Integer.toString(getProgress()), textX, textY, textPaint); }
From source file:radialdemo.RadialMenuWidget.java
@Override protected void onDraw(Canvas c) { Paint paint = new Paint(); paint.setAntiAlias(true);/*from w w w .j av a2 s . c o m*/ paint.setStrokeWidth(3); // draws a dot at the source of the press if (showSource == true) { paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawCircle(xSource, ySource, cRadius / 10, paint); paint.setColor(selectedColor); paint.setAlpha(selectedAlpha); paint.setStyle(Paint.Style.FILL); c.drawCircle(xSource, ySource, cRadius / 10, paint); } //inner for (int i = 0; i < Wedges.length; i++) { RadialMenuWedge f = Wedges[i]; paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawPath(f, paint); if (f == enabled && Wedge2Shown == true) { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else if (f != enabled && Wedge2Shown == true) { paint.setColor(disabledColor); paint.setAlpha(disabledAlpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else if (f == enabled && Wedge2Shown == false) { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else if (f == selected) { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else { paint.setColor(defaultColor); paint.setAlpha(defaultAlpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } //button content Rect rf = iconRect[i]; if ((menuEntries.get(i).getIcon() != 0) && (menuEntries.get(i).getLabel() != null)) { // This will look for a "new line" and split into multiple lines String menuItemName = menuEntries.get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); paint.setColor(textColor); if (f != enabled && Wedge2Shown == true) { paint.setAlpha(disabledAlpha); } else { paint.setAlpha(textAlpha); } paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize); Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } Rect rf2 = new Rect(); rf2.set(rf.left, rf.top - ((int) textHeight / 2), rf.right, rf.bottom - ((int) textHeight / 2)); float textBottom = rf2.bottom; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } // Puts in the Icon Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon()); drawable.setBounds(rf2); if (f != enabled && Wedge2Shown == true) { drawable.setAlpha(disabledAlpha); } else { drawable.setAlpha(pictureAlpha); } drawable.draw(c); // Icon Only } else if (menuEntries.get(i).getIcon() != 0) { // Puts in the Icon Drawable drawable = getResources().getDrawable(menuEntries.get(i).getIcon()); drawable.setBounds(rf); if (f != enabled && Wedge2Shown == true) { drawable.setAlpha(disabledAlpha); } else { drawable.setAlpha(pictureAlpha); } drawable.draw(c); // Text Only } else { // Puts in the Text if no Icon paint.setColor(textColor); if (f != enabled && Wedge2Shown == true) { paint.setAlpha(disabledAlpha); } else { paint.setAlpha(textAlpha); } paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize + 10); // This will look for a "new line" and split into multiple lines String menuItemName = menuEntries.get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); // gets total height Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } float textBottom = rf.centerY() - (textHeight / 2); for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } } } // Animate the outer ring in/out if (animateOuterIn == true) { animateOuterWedges(ANIMATE_IN); } else if (animateOuterOut == true) { animateOuterWedges(ANIMATE_OUT); } //outer if (Wedge2Shown == true) { for (int i = 0; i < Wedges2.length; i++) { RadialMenuWedge f = Wedges2[i]; paint.setColor(outlineColor); paint.setAlpha(20); paint.setStyle(Paint.Style.STROKE); c.drawPath(f, paint); if (f == selected2) { paint.setColor(selectedColor); paint.setAlpha(selectedAlpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } else { paint.setColor(wedge2Color); paint.setAlpha(wedge2Alpha); paint.setStyle(Paint.Style.FILL); c.drawPath(f, paint); } Rect rf = iconRect2[i]; if ((wedge2Data.getChildren().get(i).getIcon() != 0) && (wedge2Data.getChildren().get(i).getLabel() != null)) { // This will look for a "new line" and split into multiple // lines String menuItemName = wedge2Data.getChildren().get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(animateTextSize); Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } Rect rf2 = new Rect(); rf2.set(rf.left, rf.top - ((int) textHeight / 2), rf.right, rf.bottom - ((int) textHeight / 2)); float textBottom = rf2.bottom; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } // Puts in the Icon Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon()); drawable.setBounds(rf2); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Icon Only } else if (wedge2Data.getChildren().get(i).getIcon() != 0) { // Puts in the Icon Drawable drawable = getResources().getDrawable(wedge2Data.getChildren().get(i).getIcon()); drawable.setBounds(rf); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Text Only } else { // Puts in the Text if no Icon paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(animateTextSize); // This will look for a "new line" and split into multiple // lines String menuItemName = wedge2Data.getChildren().get(i).getLabel(); String[] stringArray = menuItemName.split("\n"); // gets total height Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } float textBottom = rf.centerY() - (textHeight / 2); for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = rf.centerX() - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } } } } //Check if the user has given input for centre circle if (centerCircle != null) { // Draws the Middle Circle paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawCircle(xPosition, yPosition, cRadius, paint); if (inCircle == true) { paint.setColor(selectedColor); paint.setAlpha(selectedAlpha); paint.setStyle(Paint.Style.FILL); c.drawCircle(xPosition, yPosition, cRadius, paint); helper.onCloseAnimation(this, xPosition, yPosition, xSource, ySource); } else { paint.setColor(defaultColor); paint.setAlpha(defaultAlpha); paint.setStyle(Paint.Style.FILL); c.drawCircle(xPosition, yPosition, cRadius, paint); } // Draw the circle picture if ((centerCircle.getIcon() != 0) && (centerCircle.getLabel() != null)) { // This will look for a "new line" and split into multiple lines String menuItemName = centerCircle.getLabel(); String[] stringArray = menuItemName.split("\n"); paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize); Rect rectText = new Rect(); Rect rectIcon = new Rect(); Drawable drawable = getResources().getDrawable(centerCircle.getIcon()); int h = getIconSize(drawable.getIntrinsicHeight(), MinIconSize, MaxIconSize); int w = getIconSize(drawable.getIntrinsicWidth(), MinIconSize, MaxIconSize); rectIcon.set(xPosition - w / 2, yPosition - h / 2, xPosition + w / 2, yPosition + h / 2); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rectText); textHeight = textHeight + (rectText.height() + 3); } rectIcon.set(rectIcon.left, rectIcon.top - ((int) textHeight / 2), rectIcon.right, rectIcon.bottom - ((int) textHeight / 2)); float textBottom = rectIcon.bottom; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rectText); float textLeft = xPosition - rectText.width() / 2; textBottom = textBottom + (rectText.height() + 3); c.drawText(stringArray[j], textLeft - rectText.left, textBottom - rectText.bottom, paint); } // Puts in the Icon drawable.setBounds(rectIcon); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Icon Only } else if (centerCircle.getIcon() != 0) { Rect rect = new Rect(); Drawable drawable = getResources().getDrawable(centerCircle.getIcon()); int h = getIconSize(drawable.getIntrinsicHeight(), MinIconSize, MaxIconSize); int w = getIconSize(drawable.getIntrinsicWidth(), MinIconSize, MaxIconSize); rect.set(xPosition - w / 2, yPosition - h / 2, xPosition + w / 2, yPosition + h / 2); drawable.setBounds(rect); drawable.setAlpha(pictureAlpha); drawable.draw(c); // Text Only } else { // Puts in the Text if no Icon paint.setColor(textColor); paint.setAlpha(textAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(textSize); // This will look for a "new line" and split into multiple lines String menuItemName = centerCircle.getLabel(); String[] stringArray = menuItemName.split("\n"); // gets total height Rect rect = new Rect(); float textHeight = 0; for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); textHeight = textHeight + (rect.height() + 3); } float textBottom = yPosition - (textHeight / 2); for (int j = 0; j < stringArray.length; j++) { paint.getTextBounds(stringArray[j], 0, stringArray[j].length(), rect); float textLeft = xPosition - rect.width() / 2; textBottom = textBottom + (rect.height() + 3); c.drawText(stringArray[j], textLeft - rect.left, textBottom - rect.bottom, paint); } } } // Draws Text in TextBox if (headerString != null) { paint.setTextSize(headerTextSize); paint.getTextBounds(headerString, 0, headerString.length(), this.textRect); if (HeaderBoxBounded == false) { determineHeaderBox(); HeaderBoxBounded = true; } paint.setColor(outlineColor); paint.setAlpha(outlineAlpha); paint.setStyle(Paint.Style.STROKE); c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint); paint.setColor(headerBackgroundColor); paint.setAlpha(headerBackgroundAlpha); paint.setStyle(Paint.Style.FILL); c.drawRoundRect(this.textBoxRect, scalePX(5), scalePX(5), paint); paint.setColor(headerTextColor); paint.setAlpha(headerTextAlpha); paint.setStyle(Paint.Style.FILL); paint.setTextSize(headerTextSize); c.drawText(headerString, headerTextLeft, headerTextBottom, paint); } }
From source file:com.waz.zclient.pages.main.participants.dialog.ParticipantsDialogFragment.java
private void setDialogPosition(Rect rect, int posX, int posY, int dialogWidth, int dialogHeight) { if (getControllerFactory().getConversationScreenController() .getPopoverLaunchMode() == DialogLaunchMode.COMMON_USER) { dialogTranslationX = posX + (rect.width() - dialogWidth) / 2; marker.setVisibility(View.VISIBLE); } else if (getControllerFactory().getConversationScreenController() .getPopoverLaunchMode() == DialogLaunchMode.CONVERSATION_TOOLBAR) { int screenWidth = ViewUtils.getRealDisplayWidth(getActivity()); dialogTranslationX = screenWidth / 2 - dialogWidth / 2; marker.setVisibility(View.INVISIBLE); } else {//w w w .j a v a 2 s .c o m dialogTranslationX = getResources() .getDimensionPixelSize(R.dimen.framework__participants_dialog__pos_x); marker.setVisibility(View.VISIBLE); } int markerHeight = marker.getMeasuredHeight() / 2; // because we draw on our own int markerWidth = marker.getMeasuredWidth(); markerTranslationX = posX + (rect.width() - markerWidth) / 2; int displayHeight; int displayWidth; boolean forceRight = getControllerFactory().getConversationScreenController() .getPopoverLaunchMode() == DialogLaunchMode.SEARCH || getControllerFactory().getConversationScreenController() .getPopoverLaunchMode() == DialogLaunchMode.CONVERSATION_MENU; if (ViewUtils.isInPortrait(getActivity())) { displayHeight = ViewUtils.getOrientationIndependentDisplayHeight(getActivity()); displayWidth = ViewUtils.getOrientationIndependentDisplayWidth(getActivity()); } else { displayHeight = ViewUtils.getOrientationIndependentDisplayWidth(getActivity()); displayWidth = ViewUtils.getOrientationIndependentDisplayHeight(getActivity()); } final int screenBottom = displayHeight - participantDialogPadding - ViewUtils.getStatusBarHeight(getActivity()); final int screenRight = displayWidth - participantDialogPadding; final int screenLeft = participantDialogPadding; if ((posY - dialogHeight - markerHeight) >= participantDialogPadding && !forceRight) { // put above markerTranslationY = posY - markerHeight; dialogTranslationY = markerTranslationY - dialogHeight; marker.setRotation(0f); if (dialogTranslationX + dialogWidth > screenRight) { // too far right dialogTranslationX = screenRight - dialogWidth; } else if (dialogTranslationX < participantDialogPadding) { // too far left dialogTranslationX = participantDialogPadding; } selfGravity = Gravity.TOP; } else if (posY + rect.height() + dialogHeight + markerHeight < screenBottom && !forceRight) { // put below markerTranslationY = posY + rect.height() - markerHeight; dialogTranslationY = markerTranslationY + 2 * markerHeight; // 2 * because we draw on our own marker.setRotation(180f); if (dialogTranslationX + dialogWidth > screenRight) { // too far right dialogTranslationX = screenRight - dialogWidth; } else if (dialogTranslationX < participantDialogPadding) { // too far left dialogTranslationX = participantDialogPadding; } selfGravity = Gravity.BOTTOM; } else if (posX + rect.width() + markerWidth + dialogWidth <= displayWidth - participantDialogPadding || forceRight) { int tmp = markerHeight; //noinspection SuspiciousNameCombination markerHeight = markerWidth; markerWidth = tmp; // centered markerTranslationX = posX + rect.width() - markerWidth; dialogTranslationX = markerTranslationX + 2 * markerWidth; // 2 * because we draw on our own markerTranslationY = (posY + rect.centerY()) - (markerHeight / 2); dialogTranslationY = (posY + rect.centerY()) - (dialogHeight / 2); marker.setRotation(90f); if (dialogTranslationY < participantDialogPadding) { // too high dialogTranslationY = participantDialogPadding; } else if (posY + dialogHeight > screenBottom) { // too low dialogTranslationY = displayHeight - participantDialogPadding - dialogHeight - ViewUtils.getStatusBarHeight(getActivity()); } // too far right if (dialogTranslationX + dialogWidth > screenRight) { dialogTranslationX = screenRight - dialogWidth; markerTranslationX = dialogTranslationX - 2 * markerWidth; // 2 * because we draw on our own } selfGravity = Gravity.RIGHT; } else { int tmp = markerHeight; //noinspection SuspiciousNameCombination markerHeight = markerWidth; markerWidth = tmp; // centered markerTranslationX = posX - markerWidth; dialogTranslationX = markerTranslationX - dialogWidth; markerTranslationY = (posY + rect.centerY()) - (markerHeight / 2); dialogTranslationY = (posY + rect.centerY()) - (dialogHeight / 2); marker.setRotation(270f); if (dialogTranslationY < participantDialogPadding) { // too high dialogTranslationY = participantDialogPadding; } else if (posY + dialogHeight > screenBottom) { // too low dialogTranslationY = displayHeight - participantDialogPadding - dialogHeight - ViewUtils.getStatusBarHeight(getActivity()); } // too far left if (dialogTranslationX < screenLeft) { dialogTranslationX = screenLeft; markerTranslationX = dialogTranslationX + dialogWidth; } selfGravity = Gravity.LEFT; } dialogFrameLayout.setTranslationX(dialogTranslationX); dialogFrameLayout.setTranslationY(dialogTranslationY); marker.setTranslationX(markerTranslationX); marker.setTranslationY(markerTranslationY); }