我想在一个圆形的动态图片周围创建动态线条。线条的大小取决于总的回合数,如果总回合数为10,则10个线条适合该整体。
我尝试了下面的for循环中的代码,动态创建了线,但它们不是圆形的。请帮助我解决此问题。谢谢。
这是您可以在其中看到红色的图像。月球周围的黄绿线:
代码如下:
int Stages = 10;
int circleCircumfarence = 785;
int spaceBetweenLine = 10;
int allspacesSize = 785 - (spaceBetweenLine * Stages);
int onelinesize = allspacesSize / 10; //250 is the width of circle
int x1 = (int) imgCatfill.getWidth() / 2;//image of globe
int y1 = 10;
int x2 = (int) x1 + onelinesize;
int y2 = x2 / 10;
int curveRadius = 5;
int midX = x1 + ((x2 - x1) / 2);
int midY = y1 + ((y2 - y1) / 2);
float xDiff = midX - x1;
float yDiff = midY - y1;
double angle = (Math.atan2(yDiff, xDiff) * (180 / Math.PI)) - 45;
double angleRadians = Math.toRadians(angle);
float pointX = (float) (midX + curveRadius * Math.cos(angleRadians));
float pointY = (float) (midY + curveRadius * Math.sin(angleRadians));
path.moveTo(x1, y1);
path.cubicTo(x1, y1, pointX, pointY, x2, y2);
canvas.drawPath(path, paint);
答案 0 :(得分:1)
这可以通过使用方法canvas.drawTextOnPath
并根据文本宽度更改x和y位置来完成
参考代码
private String QUOTE = "123456789123456789";
private Path circle;
private Paint mCirlcePaint;
private Paint tPaint;
private Rect textBounds;
private int mTextWidth, mTextHeight, centerX, centerY;
private float radius;
public GraphicsView(Context context) {
super(context);
circle = new Path();
tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
tPaint.setColor(Color.RED);
tPaint.setTextSize(80f);
textBounds = new Rect();
tPaint.getTextBounds(QUOTE, 0, QUOTE.length(), textBounds);
mTextWidth = Math.round(tPaint.measureText(QUOTE.toString())); // Use measureText to calculate width
mTextHeight = textBounds.height(); // Use height from getTextBounds()
mCirlcePaint = new Paint();
mCirlcePaint.setStyle(Paint.Style.FILL);
mCirlcePaint.setColor(Color.BLUE);
radius = (float) ((mTextWidth) / (Math.PI));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2;
centerY = h / 2;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.rotate(180, getWidth() / 2, getHeight() / 2);
canvas.drawCircle(centerX, centerY, radius, mCirlcePaint);
circle.addCircle(centerX, centerY, radius, Path.Direction.CW);
canvas.drawTextOnPath(QUOTE, circle, 0, 0, tPaint);
}
}
如果文本长度大于画布,则需要减小文本大小或使用完整的圆圈。
replace radius = (float) ((mTextWidth) / (Math.PI))
与radius = (float) ((mTextWidth) / (2*(Math.PI)))