如何制作可缩放滚动视图?

问题描述:

在我的android应用程序中,我需要创建活动缩放能力。我发现用于缩放线性布局的有用代码here。但在我的应用程序中,一些活动以scrollview开头,并且此代码无法识别滚动视图。我怎样才能使可缩放活动缩放? 这是我的一个布局。如何制作可缩放滚动视图?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/scrollViewZoom" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" > 

<LinearLayout 
    android:id="@+id/wd_content" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:orientation="vertical" > 

    <!-- Start Circle --> 

    <TableRow 
     android:id="@+id/row_circl1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_marginTop="30dp" 
     android:background="@color/purple_color" > 

     <RelativeLayout 
      android:id="@+id/circle_layout1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@color/white_color" > 

      <ImageView 
       android:id="@+id/img_engin_circle1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:background="@drawable/circle_engin_bg" 
       android:contentDescription="TODO" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/circle_layout2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@color/white_color" > 

      <ImageView 
       android:id="@+id/img_engin_circle2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:background="@drawable/circle_engin_bg" 
       android:contentDescription="TODO" /> 
     </RelativeLayout> 
    </TableRow> 

    <TableRow 
     android:id="@+id/row_name_circle" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_marginTop="30dp" > 

     <RelativeLayout 
      android:id="@+id/circle_name_layout1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="-15dp" 
      android:background="@color/white_color" > 

      <ImageView 
       android:id="@+id/img_name_circle1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:background="@drawable/circle_gauge_name" 
       android:contentDescription="TODO" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/circle_name_layout2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="-15dp" 
      android:background="@color/white_color" > 

      <ImageView 
       android:id="@+id/img_name_circle2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:background="@drawable/circle_gauge_name" 
       android:contentDescription="TODO" /> 
     </RelativeLayout> 
    </TableRow> 

    <!-- End Circle --> 

</LinearLayout> 

</ScrollView> 

任何想法都会帮助我。 谢谢。

+0

请检查此链接: [启用变焦上滚动视图] [1] 好运。 [1]:http://*.com/questions/18572014/enabling-zoom-on-scroll-view –

+0

感谢回复@ Adir.el但我发现另一种解决方案。 – IndieBoy

好的。在耕网之后,终于找到了我的答案。 我发现onTouchEvent()为scrollview不起作用,所以我必须使用dispatchTouchEvent()而不是onTouchEvent()。 在顶部你可以看到我的xml代码(在我的问题中),这里是我的活动代码,当然有评论。

// step 1: add some instance 
private float mScale = 1f; 
private ScaleGestureDetector mScaleDetector; 
GestureDetector gestureDetector; 

//step 2: create instance from GestureDetector(this step sholude be place into onCreate()) 
gestureDetector = new GestureDetector(this, new GestureListener()); 

// animation for scalling 
mScaleDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener() 
    {         
     @Override 
     public boolean onScale(ScaleGestureDetector detector) 
     { 
      float scale = 1 - detector.getScaleFactor(); 

      float prevScale = mScale; 
      mScale += scale; 

      if (mScale < 0.1f) // Minimum scale condition: 
       mScale = 0.1f; 

      if (mScale > 10f) // Maximum scale condition: 
       mScale = 10f; 
      ScaleAnimation scaleAnimation = new ScaleAnimation(1f/prevScale, 1f/mScale, 1f/prevScale, 1f/mScale, detector.getFocusX(), detector.getFocusY()); 
      scaleAnimation.setDuration(0); 
      scaleAnimation.setFillAfter(true); 
      ScrollView layout =(ScrollView) findViewById(R.id.scrollViewZoom); 
      layout.startAnimation(scaleAnimation); 
      return true; 
     } 
    }); 


// step 3: override dispatchTouchEvent() 
@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    super.dispatchTouchEvent(event); 
    mScaleDetector.onTouchEvent(event); 
    gestureDetector.onTouchEvent(event); 
    return gestureDetector.onTouchEvent(event); 
} 

//step 4: add private class GestureListener 

private class GestureListener extends GestureDetector.SimpleOnGestureListener { 
    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 
    // event when double tap occurs 
    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     // double tap fired. 
     return true; 
    } 
} 

非常感谢。

+5

拯救了另一个生命!另一款啤酒在这里保证在Brazi! –

+2

它只显示缩放,当我实现时不滚动。 –

+0

你能解释一下我需要使用它吗?我创建了一个扩展Scrollview的自定义类,并在该类中添加了上面的代码。但是它会给init初始化异常 –

感谢您的解决方案,我实现它有点不同,因为我的滚动型是一个片段,而不是一个活动里面,委托所有的逻辑视图,我会建议增加dispatchTouchEvent在相同的自定义滚动视图:

package com.your.package; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.ScaleGestureDetector; 
import android.view.animation.ScaleAnimation; 
import android.widget.ScrollView; 

import com.your.package.R; 

public class CustomZoomScrollView extends ScrollView { 


    // step 1: add some instance 
    private float mScale = 1f; 
    private ScaleGestureDetector mScaleDetector; 
    GestureDetector gestureDetector; 


    public CustomZoomScrollView(Context context) { 
     super(context); 
    } 

    public CustomZoomScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     //step 2: create instance from GestureDetector(this step should be place into onCreate()) 
     gestureDetector = new GestureDetector(getContext(), new GestureListener()); 

     mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener() 
     { 
      @Override 
      public boolean onScale(ScaleGestureDetector detector) 
      { 
       float scale = 1 - detector.getScaleFactor(); 

       float prevScale = mScale; 
       mScale += scale; 

       if (mScale < 0.1f) // Minimum scale condition: 
        mScale = 0.1f; 

       if (mScale > 10f) // Maximum scale condition: 
        mScale = 10f; 
       ScaleAnimation scaleAnimation = new ScaleAnimation(1f/prevScale, 1f/mScale, 1f/prevScale, 1f/mScale, detector.getFocusX(), detector.getFocusY()); 
       scaleAnimation.setDuration(0); 
       scaleAnimation.setFillAfter(true); 
       getRootView().findViewById(R.id.svSeats).startAnimation(scaleAnimation); 
       return true; 
      } 
     }); 
    } 

    // step 3: override dispatchTouchEvent() 
    @Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
     super.dispatchTouchEvent(event); 
     mScaleDetector.onTouchEvent(event); 
     gestureDetector.onTouchEvent(event); 
     return gestureDetector.onTouchEvent(event); 
    } 

//step 4: add private class GestureListener 

    private class GestureListener extends GestureDetector.SimpleOnGestureListener { 
     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 
     // event when double tap occurs 
     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      // double tap fired. 
      return true; 
     } 
    } 
}