Android 在一点和另一点之间画线
我有一个中央按钮和12个外部按钮之间的自定义线视图,见图。 代码块1是我用来获取每个按钮组合的中心点的。 代码块2是我的“自定义绘图”,它在每组中心点之间绘制一条线,总共12条线。Android 在一点和另一点之间画线,android,drawing,point,Android,Drawing,Point,我有一个中央按钮和12个外部按钮之间的自定义线视图,见图。 代码块1是我用来获取每个按钮组合的中心点的。 代码块2是我的“自定义绘图”,它在每组中心点之间绘制一条线,总共12条线。 12个自定义行都在一个xml文件中。 按钮和线条在两个单独的数组中传递到公共类Drawline中 我还创建了一个类,它使用路径绘制三角形。这是坐在左上角我的图形 我想要什么 按钮中心与第二个端点之间的一条线。 我希望点之间的线结束,就像你看到的“脆脆”和“平衡”按钮之间的线一样。我需要帮助找到如何“得到”这个点的x,
12个自定义行都在一个xml文件中。 按钮和线条在两个单独的数组中传递到公共类Drawline中 我还创建了一个类,它使用路径绘制三角形。这是坐在左上角我的图形 我想要什么 按钮中心与第二个端点之间的一条线。 我希望点之间的线结束,就像你看到的“脆脆”和“平衡”按钮之间的线一样。我需要帮助找到如何“得到”这个点的x,y。我还想知道如何将箭头放在线条的末端 我觉得应该有一个更简单的方法来做到这一点,但我还没有找到它。 谢谢你的帮助。 谢谢
Jim因为我现在知道了每个外部按钮的角度和到该点的半径,所以我能够使用极坐标获得位置。然后我使用下面的转换来获得相应的x和y坐标 val x=半径*数学cos(角度); val y=半径*数学sin(角度) 这给了我画线所需的另一个端点
public class Drawline {
public void drawLines(List<LineView> mlinesToDraw,ArrayList<Button> buttonsbalance, Context
context,Button btnbase) {
float centerXOnImage1;
double centerYOnImage1;
float centerXOfImageOnScreen1;
double centerYOfImageOnScreen1;
float centerXOnImage2;
float centerYOnImage2;
float centerXOfImageOnScreen2;
float centerYOfImageOnScreen2;
List<LineView> mLine = mlinesToDraw;
ArrayList<Button> btns = buttonsbalance;
PointF pointA;
PointF pointB;
for (int i = 0; i < mLine.size(); i++) {
Button button1 = btns.get(i + 1); //skip btnx0/btnbase
centerXOnImage1 = button1.getWidth() / 2;
centerYOnImage1 = (button1.getHeight()) / 2;//-actionBarHeight)/2;
centerXOfImageOnScreen1 = button1.getLeft()+ centerXOnImage1;
centerYOfImageOnScreen1 = button1.getTop() + (centerYOnImage1);
Button button2 = btnbase;
centerXOnImage2 = button2.getWidth() / 2;
centerYOnImage2 = (button2.getHeight()) / 2;//-actionBarHeight)/2;
centerXOfImageOnScreen2 = button2.getLeft() + centerXOnImage2;
centerYOfImageOnScreen2 = button2.getTop() + (centerYOnImage2);
pointA = new PointF(centerXOfImageOnScreen1, (float) centerYOfImageOnScreen1);
pointB = new PointF(centerXOfImageOnScreen2, (float) centerYOfImageOnScreen2);
mLine.get(i).setPointA(pointA);
mLine.get(i).setPointB(pointB);
mLine.get(i).draw();
}
}
public class LineView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private PointF pointA,pointB;
// private void init() {
// paint.setColor(Color.BLACK);
// }
public LineView(Context context) {
super(context);
// init();
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
// init();
}
public LineView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// init();
}
@SuppressLint("ResourceAsColor")
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int color = R.color.GradientStart;
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStrokeWidth(6);
canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, paint);
}
public void setPointA(PointF point){
pointA=point;}
public void setPointB(PointF point){
pointB=point;}
public void draw(){
invalidate();
requestLayout();
}}