我如何放置一个按钮,使它的位置与图像上的位置匹配,图像被调整大小以适合屏幕的宽度。
这就是
我已将其添加到MainActivity的ConstraintLayout中
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/test"
tools:context=".MainActivity">
<ImageView
android:layout_width="0dp"
android:layout_margin="0dp"
android:padding="0dp"
android:layout_height="0dp" app:srcCompat="@drawable/untitled" android:id="@+id/imageView"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"
android:scaleType="fitStart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
左上角位于像素(100,100)上,图像的大小为500 × 500像素,黑盒的大小为50 × 50像素。
我试着通过使用屏幕上图像的宽度来计算位置,并使用实际宽度创建一个乘数。但是按钮关闭了很多,我不知道如何将它与图像匹配。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout viewById = findViewById(R.id.test);
Button button = new Button(this);
button.setId(View.generateViewId());
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(200, 200);
layoutParams.setMargins(0, 0, 0, 0);
button.setLayoutParams(layoutParams);
button.setPadding(0, 0, 0, 0);
button.setElevation(100f);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(viewById);
constraintSet.connect(button.getId(), ConstraintSet.START, viewById.getId(), ConstraintSet.START);
constraintSet.connect(button.getId(), ConstraintSet.TOP, viewById.getId(), ConstraintSet.TOP);
viewById.addView(button);
constraintSet.applyTo(viewById);
ImageView image = findViewById(R.id.imageView);
final be.robben.test.MainActivity mainActivity = this;
ViewTreeObserver vto = image.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
image.getViewTreeObserver().removeOnPreDrawListener(this);
int finalHeight, finalWidth;
finalHeight = image.getHeight();
finalWidth = image.getWidth();
init(image.getDrawable().getIntrinsicWidth(), finalHeight, mainActivity);
return true;
}
});
}
public void init(final int x, final int y, final AppCompatActivity context) {
float mult = ((float) x) / ((float) 500);
ConstraintLayout viewById = context.findViewById(R.id.test);
Button button = new Button(context);
button.setId(View.generateViewId());
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams((int) (50 * mult), (int) (50 * mult));
float a = ((float) 100 / (float) 500) * x;
layoutParams.setMargins(0, 0, 0, 0);
layoutParams.setMarginStart(0);
layoutParams.setMarginEnd(0);
layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
button.setLayoutParams(layoutParams);
button.setX(a);
button.setY(a);
button.setPadding(0, 0, 0, 0);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.connect(button.getId(), ConstraintSet.START, viewById.getId(), ConstraintSet.START, 0);
constraintSet.connect(button.getId(), ConstraintSet.TOP, viewById.getId(), ConstraintSet.TOP, 0);
viewById.addView(button);
}
}
1条答案
按热度按时间hgc7kmma1#
仅仅为了获得图像中矩形的屏幕位置就需要你获得图像。你不能在OnCreate中完成它,因为图像还没有附加到屏幕上。你需要做如下的事情:
获得屏幕上的位置后,可以添加按钮。无需使用约束集创建按钮并填充其layoutParams来定位按钮:
这似乎是一件奇怪的事情,但这应该会给你指明正确的方向。
这个管用