android 如何在图片上设置按钮
android how to set a button on image
我想在图像上设置一个按钮(TouchImageView)。我希望这个按钮固定在图像(地图)上。所以我可以 scroll/zoom 并且这个按钮应该固定在它的位置上。我确实使用了 AbsoluteLayout(已弃用)和 RelativeLayout。 None 的帮助。
//
// DecimalFormat rounds to 2 decimal places.
//
df = new DecimalFormat("#.##");
scrollPositionTextView = (TextView) findViewById(R.id.scroll_position);
zoomedRectTextView = (TextView) findViewById(R.id.zoomed_rect);
currentZoomTextView = (TextView) findViewById(R.id.current_zoom);
image = (TouchImageView) findViewById(R.id.image);
//
// Set the OnTouchImageViewListener which updates edit texts
// with zoom and scroll diagnostics.
//
image.setOnTouchImageViewListener(new OnTouchImageViewListener() {
@Override
public void onMove() {
PointF point = image.getScrollPosition();
RectF rect = image.getZoomedRect();
float currentZoom = image.getCurrentZoom();
boolean isZoomed = image.isZoomed();
scrollPositionTextView.setText("x: " + df.format(point.x) + " y: " + df.format(point.y));
zoomedRectTextView.setText("left: " + df.format(rect.left) + " top: " + df.format(rect.top)
+ "\nright: " + df.format(rect.right) + " bottom: " + df.format(rect.bottom));
currentZoomTextView.setText("getCurrentZoom(): " + currentZoom + " isZoomed(): " + isZoomed);
}
});
你能给我一个建议吗?或者至少是阅读参考。我想做一些类似辐射避难所或部落冲突的东西(你可以在其中滚动并查看你的基地)。
您可以使用 Framelayout 在图像顶部显示按钮,并使用 view.offsetLeftAndRight 方法更改按钮位置。
例如
int lastX;
int lastY;
void setButtonPosition(int x, int y) {
button.offsetLeftAndRight(x - lastX);
button.offsetTopAndBottom(y - lastY);
lastX = x;
lastY = y;
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:text="Button" />
</FrameLayout>
我想在图像上设置一个按钮(TouchImageView)。我希望这个按钮固定在图像(地图)上。所以我可以 scroll/zoom 并且这个按钮应该固定在它的位置上。我确实使用了 AbsoluteLayout(已弃用)和 RelativeLayout。 None 的帮助。
//
// DecimalFormat rounds to 2 decimal places.
//
df = new DecimalFormat("#.##");
scrollPositionTextView = (TextView) findViewById(R.id.scroll_position);
zoomedRectTextView = (TextView) findViewById(R.id.zoomed_rect);
currentZoomTextView = (TextView) findViewById(R.id.current_zoom);
image = (TouchImageView) findViewById(R.id.image);
//
// Set the OnTouchImageViewListener which updates edit texts
// with zoom and scroll diagnostics.
//
image.setOnTouchImageViewListener(new OnTouchImageViewListener() {
@Override
public void onMove() {
PointF point = image.getScrollPosition();
RectF rect = image.getZoomedRect();
float currentZoom = image.getCurrentZoom();
boolean isZoomed = image.isZoomed();
scrollPositionTextView.setText("x: " + df.format(point.x) + " y: " + df.format(point.y));
zoomedRectTextView.setText("left: " + df.format(rect.left) + " top: " + df.format(rect.top)
+ "\nright: " + df.format(rect.right) + " bottom: " + df.format(rect.bottom));
currentZoomTextView.setText("getCurrentZoom(): " + currentZoom + " isZoomed(): " + isZoomed);
}
});
你能给我一个建议吗?或者至少是阅读参考。我想做一些类似辐射避难所或部落冲突的东西(你可以在其中滚动并查看你的基地)。
您可以使用 Framelayout 在图像顶部显示按钮,并使用 view.offsetLeftAndRight 方法更改按钮位置。
例如
int lastX;
int lastY;
void setButtonPosition(int x, int y) {
button.offsetLeftAndRight(x - lastX);
button.offsetTopAndBottom(y - lastY);
lastX = x;
lastY = y;
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:text="Button" />
</FrameLayout>