Android: Scrollable (horizontal and vertical) Imag

2019-06-28 02:10发布

问题:

I want to implement an activity where the only thing you see is a big image, which can be scrolled horizontally and vertically.On Top of that image I want to display buttons, that can be clicked and trigger certain actions (like creating an intent to start a new activity).

First I was thinking about a ScrollView, that has a FrameLayout as a child. The FrameLayout could have the image as a background and can have the buttons as childs. Because I know the position of my buttons exactly I could place them with absolute coordinates. Here is my first code:

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

The Problem is, that you can only scroll a ScrollView vertically. HorizontalScrollView doesn't solve the Problem, cause it only scrolls in one direction either. Can I mix them somehow? Is there another solution?

I found some similar threads on stackoverflow, where people put the image into a WebView and get horizonzal/vertical scrolling for free (here). Or someone put the image in an imageview and gave the imageview an onTouchListener to handle scrolling (here). The Problem with both ways is, that I either way I dont think you can put Buttons on top of the image, which is what I need to do.

I would very appreciate if someone help me out.

回答1:

Using the (deprecated!!!) AbsoluteLayout and giving it and onTouchListener solved my problem:

<?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;
            }
        });

    }