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

问题描述:

我想实现一个活动,您唯一看到的是一个大图像,可以水平和垂直滚动。在该图像的顶部,我想显示按钮,可以点击并触发某些操作(如创建开始新活动的意图)。Android:可滚动(水平和垂直)ImageView,带按钮覆盖

首先我想到一个ScrollView,它有一个FrameLayout作为一个孩子。 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不能解决问题,导致它只向一个方向滚动。我可以混合他们吗?还有其他解决方案吗?

我在*上发现了一些类似的线程,其中人们将图像放入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; 
      } 
     }); 

    } 
+0

没有你设法把一个按钮在屏幕的实例的中心,也是创建按钮出来的屏幕呢?我的意思是,当你在两侧滚动图像时,你可以看到新的按钮出现(这可能不是你需要的,但我对你的问题感兴趣) – Sephy 2010-08-09 13:11:34