Android 在按钮、can和x27的中心点之间绘制线;找不到中心
在约束布局中,我有一列6个按钮。它们链接在一起,顶部按钮约束到操作栏,底部按钮约束到导航栏正上方的导向。在该列的左侧,我有另一个按钮。我已经在两组按钮之间画了线-参见图 这些行来自自定义视图,并已添加到我的xml中。下面是我的自定义视图中绘制线条的一些代码Android 在按钮、can和x27的中心点之间绘制线;找不到中心,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,在约束布局中,我有一列6个按钮。它们链接在一起,顶部按钮约束到操作栏,底部按钮约束到导航栏正上方的导向。在该列的左侧,我有另一个按钮。我已经在两组按钮之间画了线-参见图 这些行来自自定义视图,并已添加到我的xml中。下面是我的自定义视图中绘制线条的一些代码 public void onDraw(Canvas canvas) { super.onDraw(canvas); int color = ContextCompat.getColor(getContext(), R.col
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int color = ContextCompat.getColor(getContext(), R.color.GradientStart);
paint.setColor(color);
paint.setAntiAlias(true);
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;}
我的问题是我似乎无法在每个按钮的中心找到开始点和结束点。要么按钮上移,要么线路下移
这是我的按钮xml。我只添加了顶部和底部以及一些中间按钮。所有中间按钮都具有相同的xml
<com.example.android.cop1803.LayoutCustom xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mylayoutcustom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context="com.example.android.cop1803.MainActivity"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout_editor_absoluteY="73dp">
<!---->
<com.example.android.cop1803.MyViewbutton
android:id="@+id/MainCOPbtn"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="@id/guideline2"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/max"
app:layout_constraintHeight_max="@dimen/max"
<com.example.android.cop1803.MyViewbutton>
android:id="@+id/x21"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/copbutton"
app:layout_constraintBottom_toTopOf="@+id/x22"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall"
app:layout_constraintHeight_max="@dimen/maxsmall"/>
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x22"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/copbutton"
app:layout_constraintBottom_toTopOf="@+id/x23"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x21"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall"/>
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x23"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/copbutton"
app:layout_constraintBottom_toTopOf="@id/x24"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x22"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall"/>
<com.example.android.cop1803.MyViewbutton
android:id="@+id/x27"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/copbutton"
app:layout_constraintBottom_toBottomOf="@id/guideline2"
android:layout_marginBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x26"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall"/>
这是我找到中心点的代码
public void drawLines(List<LineView> mlinesToDraw) {
int[] viewALocationInWindow = new int[2];
int[] viewBLocationInWindow = new int[2];
vBtn1.getLocationInWindow(viewALocationInWindow);
vBtn2.getLocationInWindow(viewBLocationInWindow);
int[] viewALocationOnScreen = new int[2];
int[] viewBLocationOnScreen = new int[2];
vBtn1.getLocationOnScreen(viewALocationOnScreen);
vBtn2.getLocationOnScreen(viewBLocationOnScreen);
float Ax = vBtn1.getX();
float Ay = vBtn1.getY();
float Bx = vBtn2.getX();;
float By = vBtn2.getY();
final int lineStartX = ((ConstraintLayout.LayoutParams) vBtn1.getLayoutParams()).leftMargin + (vBtn1.getMeasuredWidth() / 2)+vBtn1.getLeft();
final int lineStartY = ((ConstraintLayout.LayoutParams) vBtn1.getLayoutParams()).topMargin + (vBtn1.getMeasuredHeight() / 2)+vBtn1.getTop();
final int lineEndX = ((ConstraintLayout.LayoutParams) vBtn2.getLayoutParams()).leftMargin + (vBtn2.getMeasuredWidth() / 2)+vBtn2.getLeft();
final int lineEndY = ((ConstraintLayout.LayoutParams) vBtn2.getLayoutParams()).topMargin + (vBtn2.getMeasuredHeight() / 2)+vBtn2.getTop();
pointA=new PointF(lineStartX, lineStartY);
pointB=new PointF(lineEndX,lineEndY);
mLine.get(i).setPointA(pointA);
mLine.get(i).setPointB(pointB);
} }
public void绘图线(列表mlinesToDraw){
int[]viewALocationInWindow=新int[2];
int[]viewBLocationInWindow=新int[2];
vBtn1.getLocationInWindow(ViewalLocationInWindow);
vBtn2.getLocationInWindow(viewBLocationInWindow);
int[]viewalocationnscreen=新int[2];
int[]viewBlocationnScreen=新int[2];
vBtn1.getLocationOnScreen(视图定位屏幕);
vBtn2.getLocationOnScreen(viewBLocationOnScreen);
float Ax=vBtn1.getX();
float Ay=vBtn1.getY();
float Bx=vBtn2.getX();;
float By=vBtn2.getY();
final int lineStartX=((ConstraintLayout.LayoutParams)vBtn1.getLayoutParams()).leftMargin+(vBtn1.getMeasuredWidth()/2)+vBtn1.getLeft();
final int lineStartY=((ConstraintLayout.LayoutParams)vBtn1.getLayoutParams()).topMargin+(vBtn1.getMeasuredHeight()/2)+vBtn1.getTop();
final int lineEndX=((ConstraintLayout.LayoutParams)vBtn2.getLayoutParams()).leftMargin+(vBtn2.getMeasuredWidth()/2)+vBtn2.getLeft();
final int lineEndY=((ConstraintLayout.LayoutParams)vBtn2.getLayoutParams()).topMargin+(vBtn2.getMeasuredHeight()/2)+vBtn2.getTop();
pointA=新的PointF(lineStartX、lineStartY);
点B=新点F(lineEndX,lineEndY);
mLine.get(i).setPointA(pointA);
mLine.get(i).setPointB(pointB);
} }
我已将“getViewTreeObserver().addOnGlobalLayoutListener”附加到我的主布局,因此在我绘制线条之前,按钮已经呈现
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int color = ContextCompat.getColor(getContext(), R.color.GradientStart);
paint.setColor(color);
paint.setAntiAlias(true);
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;}
非常感谢您在查找罪犯方面提供的任何帮助。不清楚您使用的是哪一组坐标。从外观上看,看起来x值是正确的,但y值是关闭的。(您是否正在计算屏幕坐标并使用它们进行绘制?这将为您提供正确的
x
值,但不正确的y
值。)检查以确保drawLine()
的坐标设置正确:x
的计算将转到x
坐标,而y
的计算也是如此
目前还不清楚在LayoutCustom
中发生了什么,因此可能发生了一些事情。坐标的任何平移都可能会让你发疯
这里有一个快速的例子,可以帮助您根据您的布局。我使用View.set前台()
(API 23+)来写入画布,但概念保持不变
活动\u main.xml
<android.support.constraint.ConstraintLayout
android:id="@+id/mylayoutcustom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="true">
<Button
android:id="@+id/MainCOPbtn"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="@id/guideline2"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="@dimen/max"
app:layout_constraintHorizontal_bias="0.14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/max" />
<Button
android:id="@+id/x21"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_light"
app:layout_constraintBottom_toTopOf="@+id/x22"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="@dimen/maxsmall"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline"
app:layout_constraintVertical_chainStyle="spread"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall" />
<Button
android:id="@+id/x22"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_light"
app:layout_constraintBottom_toTopOf="@+id/x23"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x21"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall" />
<Button
android:id="@+id/x23"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_light"
app:layout_constraintBottom_toTopOf="@id/x24"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x22"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall" />
<Button
android:id="@+id/x24"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:background="@android:color/holo_blue_light"
app:layout_constraintBottom_toBottomOf="@id/guideline2"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/x23"
app:layout_constraintWidth_default="wrap"
app:layout_constraintWidth_max="@dimen/maxsmall" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="16dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="382dp" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener {
private ConstraintLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(R.id.mylayoutcustom);
findViewById(R.id.mylayoutcustom).getViewTreeObserver()
.addOnGlobalLayoutListener(this);
}
@Override
public void onGlobalLayout() {
Button btns[] = new Button[]{
findViewById(R.id.MainCOPbtn),
findViewById(R.id.x21),
findViewById(R.id.x22),
findViewById(R.id.x23),
findViewById(R.id.x24)};
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
mLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Bitmap bmp = Bitmap.createBitmap(mLayout.getWidth(), mLayout.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
drawCircle(canvas, btns[0], paint);
for (int i = 1; i < btns.length; i++) {
drawLine(canvas, btns[0], btns[i], paint);
}
BitmapDrawable d = new BitmapDrawable(getResources(), bmp);
mLayout.setForeground(d);
}
private void drawLine(Canvas canvas, View fromView, View toView, Paint paint) {
Point from = getCenter(fromView);
Point to = getCenter(toView);
canvas.drawCircle(to.x, to.y, 20, paint);
canvas.drawLine(from.x, from.y, to.x, to.y, paint);
}
private void drawCircle(Canvas canvas, View view, Paint paint) {
Point center = getCenter(view);
canvas.drawCircle(center.x, center.y, 20, paint);
}
Point getCenter(View view) {
Point pt = new Point();
pt.x = (int) (view.getX() + view.getWidth() / 2);
pt.y = (int) (view.getY() + view.getHeight() / 2);
return pt;
}
}
公共类MainActivity扩展AppCompatActivity实现ViewTreeObserver.OnGlobalLayoutListener{
私人限制性布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout=findViewById(R.id.mylayoutcustom);
findViewById(R.id.mylayoutcustom).getViewTreeObserver()
.addOnGlobalLayoutListener(本);
}
@凌驾
公共图书馆{
按钮基站[]=新按钮[]{
findViewById(R.id.MainCOPbtn),
findViewById(R.id.x21),
findViewById(R.id.x22),
findViewById(R.id.x23),
findviewbyd(R.id.x24)};
油漆=新油漆();
油漆。设置颜色(颜色。红色);
油漆。设置行程宽度(10);
mLayout.getViewTreeObserver().removeOnGlobalLayoutListener(此);
位图bmp=Bitmap.createBitmap(mLayout.getWidth(),mLayout.getHeight(),
位图.Config.ARGB_8888);
画布=新画布(bmp);
drawCircle(帆布,BTN[0],油漆);
对于(int i=1;i
@Cheticamp的答案看起来不错,但如果你继续遇到麻烦,你可能想看看。我喜欢你的答案,尽管我终于找到了我的答案。问题在于y轴。正在创建的间隙是由于状态栏的高度造成的。不知道为什么,但出于某种原因,它没有被考虑