15

내 안드로이드 애플리케이션에서 액센트 확대 기능을 생성해야합니다. 선형 레이아웃 확대를위한 유용한 코드를 발견했습니다 here. 하지만 내 응용 프로그램에서 두 가지 활동을 scrollview 시작하고이 코드는 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://stackoverflow.com/questions/18572014/enabling-zoom-on-scroll-view Adir.el @ 회신 –

+0

감사하지만 난 다른 해결책을 발견했다. – IndieBoy

답변

22

확인. 그물을 갈고 마침내 내 대답을 찾았습니다. onTouchEvent() 대신 dispatchTouchEvent()을 사용해야하므로 scrollview에 대한 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; 
    } 
} 

고마워요. 내있는 ScrollView가 단편이 아니라 활동 안에 있기 때문에 솔루션에 대한

+5

또 다른 생명을 구했습니다! Brazi에서 여기에서 보증되는 또 다른 맥주! –

+2

핀치 줌 만 표시하고 구현할 때 스크롤하지 않습니다. –

+0

내가 이것을 사용해야 할 곳을 설명해 주시겠습니까? Scrollview를 확장하는 커스텀 클래스를 생성하고 그 클래스에 코드를 추가합니다. 하지만 초기화 예외를주고있다 –

1

덕분에, 나는 그것이 조금 다른 구현, 나도 같은 사용자 정의 스크롤 뷰에 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; 
     } 
    } 
} 
관련 문제