Android:可滚动(水平和垂直)的带有按钮覆盖的 ImageView

标签 android image button scroll

我想实现一个 Activity ,其中您唯一看到的是一个可以水平和垂直滚动的大图像。在该图像的顶部我想显示按钮,可以单击它们并触发某些操作(例如创建开始一项新 Activity 的 Intent )。

首先,我考虑的是一个 ScrollView,它有一个 FrameLayout 作为 subview 。 FrameLayout 可以将图像作为背景,并且可以将按钮作为子元素。因为我确切地知道我的按钮的位置,所以我可以用绝对坐标放置它们。这是我的第一个代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:layout_width="1298px"
        android:layout_height="945px"
        android:background="@drawable/myimage">

        <Button  
            android:id="@+id/mybutton"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_x="115px"
            android:layout_y="128px"/>

    </FrameLayout>
</ScrollView>

问题是,您只能垂直滚动 ScrollView。 Horizo​​ntalScrollView 不能解决问题,因为它也只能在一个方向上滚动。我能以某种方式混合它们吗?还有其他解决方案吗?

我在 stackoverflow 上发现了一些类似的线程,人们将图像放入 WebView 并免费获得水平/垂直滚动(here)。或者有人将图像放在 ImageView 中,并为 ImageView 提供一个 onTouchListener 来处理滚动(here)。这两种方式的问题是,无论哪种方式,我都不认为你可以将按钮放在图像之上,而这正是我需要做的。

如果有人帮助我,我将不胜感激。

最佳答案

使用(已弃用!!!)AbsoluteLayout 并提供它和 onTouchListener 解决了我的问题:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:src="@drawable/myImage"
        android:layout_width="1298px"
            android:layout_height="945px"
            android:layout_x="0px"
        android:layout_y="0px" />
    <Button  
        android:id="@+id/myButton"
        android:text="1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_x="50px"
        android:layout_y="300px"
        android:tag="1"/>

</AbsoluteLayout>




private float mx;
    private float my;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout);



        final Button button = (Button)findViewById(R.id.myButton);
        button.setOnClickListener (new View.OnClickListener(){
             // OnClickAction
});


        final AbsoluteLayout switcherView = (AbsoluteLayout) this.findViewById(R.id.myLayout);

        switcherView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View arg0, MotionEvent event) {

                float curX, curY;

                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        mx = event.getX();
                        my = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        curX = event.getX();
                        curY = event.getY();
                        switcherView.scrollBy((int) (mx - curX), (int) (my - curY));
                        mx = curX;
                        my = curY;
                        break;
                    case MotionEvent.ACTION_UP:
                        curX = event.getX();
                        curY = event.getY();
                        switcherView.scrollBy((int) (mx - curX), (int) (my - curY));
                        break;
                }

                return true;
            }
        });

    }

关于Android:可滚动(水平和垂直)的带有按钮覆盖的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3438656/

相关文章:

node.js - ExpressJs、Node Js、Passport - 注销后退按钮

html - CSS 更改特定 href 的按钮颜色

Android 相机视频为空白,带有白线

java - 如何生成.class文件?

css - 是否可以将页面上的所有图像调整为特定大小?

python - 在 openCV 和 python 中计算二值图像中的曲线、角度和直线

html - 用户头像的自然尺寸是显示尺寸的两倍

javascript - 我的“完成”按钮不知何故转到了空白页面,除非刷新 -ajax,否则删除将不起作用

android - 如何在 kivy 中创建一个在 android 上运行的项目?

java - 如何以编程方式使用 ImageButtons 填充网格?