java intersects_Java RectF.intersects方法代码示例

此代码段展示了如何在Java中利用RectF.intersects方法避免图表标签之间的重叠。它计算了标签的位置,并检查是否与其他已渲染的标签相交。如果相交,它会调整标签的位置,确保它们正确显示。该方法适用于带有圆弧的图表,如饼图或雷达图。
摘要由CSDN通过智能技术生成

import android.graphics.RectF; //导入方法依赖的package包/类

/**

* Draws a text label.

*

* @param canvas the canvas

* @param labelText the label text

* @param renderer the renderer

* @param prevLabelsBounds the previous rendered label bounds

* @param centerX the round chart center on X axis

* @param centerY the round chart center on Y axis

* @param shortRadius the short radius for the round chart

* @param longRadius the long radius for the round chart

* @param currentAngle the current angle

* @param angle the label extra angle

* @param left the left side

* @param right the right side

* @param color the label color

* @param paint the paint

* @param line if a line to the label should be drawn

* @param display display the label anyway

*/

protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer,

List prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius,

float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line,

boolean display) {

if (renderer.isShowLabels() || display) {

paint.setColor(color);

double rAngle = Math.toRadians(90 - (currentAngle + angle / 2));

double sinValue = Math.sin(rAngle);

double cosValue = Math.cos(rAngle);

int x1 = Math.round(centerX + (float) (shortRadius * sinValue));

int y1 = Math.round(centerY + (float) (shortRadius * cosValue));

int x2 = Math.round(centerX + (float) (longRadius * sinValue));

int y2 = Math.round(centerY + (float) (longRadius * cosValue));

float size = renderer.getLabelsTextSize();

float extra = Math.max(size / 2, 10);

paint.setTextAlign(Align.LEFT);

if (x1 > x2) {

extra = -extra;

paint.setTextAlign(Align.RIGHT);

}

float xLabel = x2 + extra;

float yLabel = y2;

float width = right - xLabel;

if (x1 > x2) {

width = xLabel - left;

}

labelText = getFitText(labelText, width, paint);

float widthLabel = paint.measureText(labelText);

boolean okBounds = false;

while (!okBounds && line) {

boolean intersects = false;

int length = prevLabelsBounds.size();

for (int j = 0; j < length && !intersects; j++) {

RectF prevLabelBounds = prevLabelsBounds.get(j);

if (prevLabelBounds.intersects(xLabel, yLabel, xLabel + widthLabel, yLabel + size)) {

intersects = true;

yLabel = Math.max(yLabel, prevLabelBounds.bottom);

}

}

okBounds = !intersects;

}

if (line) {

y2 = (int) (yLabel - size / 2);

canvas.drawLine(x1, y1, x2, y2, paint);

canvas.drawLine(x2, y2, x2 + extra, y2, paint);

} else {

paint.setTextAlign(Align.CENTER);

}

canvas.drawText(labelText, xLabel, yLabel, paint);

if (line) {

prevLabelsBounds.add(new RectF(xLabel, yLabel, xLabel + widthLabel, yLabel + size));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值