簡體   English   中英

Android如何在圖像上設置按鈕

[英]android how to set a button on image

我想在圖像上設置一個按鈕( TouchImageView )。 我希望此按鈕固定在圖像(地圖)上。 因此,我可以滾動/縮放,此按鈕應固定在其位置上。 我試圖使用AbsoluteLayout(不推薦使用)和RelativeLayout。 沒有任何變相。

    //
    // 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM