x = canvas.getWidth/2 //Horizontal center of canvas view
y = canvas.getHeight/2 //Vertical center of canvas view
canvas.rotate(fStartAngle[i]+ fSweepAngle[i]/2, x ,y ); //Rotates canvas to a line in the middle
//of start and end of arc
canvas.translate(50f,0);//Moves the text a little out of the center of the circle (50f is arbitrary)
paintText.setStyle(Paint.Style.FILL);
canvas.drawText(rotatedtext, x, y, paintText);
//Undo the translations and rotations so that next arc can be drawn normally
canvas.translate(-50f,0);
canvas.rotate(-(temp+ value_degree[i]/2), x ,y );
//the path where your text/paint will be drawn across
Path path = new Path();
path.addArc(mEventsRect, fStartAngle, fSweepAngle);//add this if you want your path to be drawn across the arc of your sector
//if you are using a text get the width
float textWidth = mTextPaint.measureText("text");
//this is the y co-ordinate your text will start from
int hOffset = 100;
//this is the x co-ordinate your text will start from
int vOffset = 100;
//we will be using the matrix to rotate the bunds of our current path
Matrix matrix = new Matrix();
//we will use this to get the bounds of our current path
RectF bounds = new RectF();
path.computeBounds(bounds,true);
//we are using the matrix to rotate the bound (with is the bound of the path) by 90 degrees
matrix.setRotate(90,bounds.centerX(),bounds.centerY());
the we transform the points in the path using the matrix
path.transform(matrix);
//you can now draw the text on the path
canvas.drawTextOnPath("text", path, hOffset, vOffset , mBgPaints);
6条答案
按热度按时间rdrgkggo1#
您可以尝试以下代码片段:(来自:http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-2)
dm7nw8vv2#
有点晚了,但我必须弄清楚这一点,这是一个有点简单,比我发现周围。你已经有了你的文本的x和y,使用这些旋转画布
负号表示对第一次旋转取反。你可以把它换过来向相反的方向旋转。
您可以在循环中执行此操作,但每次坐标更改时都必须执行旋转循环。
jdgnovmf3#
也许这会帮助你,这里39.5是半径,这将完美地显示结果在mdpi屏幕上
zvms9eto4#
这里是我如何终于做到了这一点,经过两天的搜索与此库的帮助https://github.com/Ken-Yang/AndroidPieChart和方程,以中心文本的帮助下,我的朋友和大量的搜索
如果您正在使用片段,则位于主活动创建或创建视图上:
在布局中:
qfe3c7zg5#
这个问题很老了,但我想我会写一个一般性的答案,在这里我假设你想在画布的中间画一个饼图,并且你在一个数组中有你的开始角和渗流角。
tf7tbtn26#
现在是2023年,可能会有其他的答案,但这里有一个肯定会奏效