2013-11-26 153 views
0

我有LinearLayout作爲其主佈局的活動。在這種佈局中,有一個按鈕,增加了一個視圖(R.layout.motor_block)的主要佈局(R.id.layout):捏縮放和平移

LayoutInflater inflater = 
    (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View iv = inflater.inflate(R.layout.motor_block, null); 
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout); 
    rl.addView(iv); 

這工作得很好,但是當我添加更多的意見,屏幕會被他們填滿,所以我需要一種通過添加平底鍋來「擴展」尺寸的方式,這樣我就可以瀏覽所有的視圖。 我也想添加縮放。

我試圖用的WebView:

 RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout); 
    WebView wv = (WebView) findViewById(R.id.webView1); 
    wv.getSettings().setSupportZoom(true); 
    wv.getSettings().setBuiltInZoomControls(true); 
    wv.getSettings().setUseWideViewPort(true); 
    wv.getSettings().setLoadWithOverviewMode(true); 
    wv.getSettings().setDisplayZoomControls(false); 
    wv.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR); 
    wv.addView(iv); 

的意見被添加到它,和平移工程。問題是,當我縮放時,webView會縮放背景,並且視圖不會更改大小。

我也嘗試了一些自定義視圖,但他們不支持帶子視圖的視圖。

完成此操作的最佳方法是什麼?

回答

0

最簡單的方法是在ScrollView中添加所有這些視圖。然後,您可以將ScrollView添加到Horizo​​ntalScrollView以在兩個方向滾動。有一個手勢捏和縮放手勢,併爲他們調用監聽器。對於縮放,您可以通過運行循環或可能使用ScaleGestureDetector來增加每個視圖的大小。
這種方法有點波濤洶涌,但有效。檢查下面的答案更多信息 - Implementing Pan and Zoom

更新: -

我這裏還有一些更使答案 -
How to implement zoom effect for image view in android?
android pinch zoom

+0

當視圖重新調整大小時,他們是否保持相同的距離? – Sochimickox

+0

如果您手動縮放它們,是的,它們會保持相同的邊距,但我不確定ScaleGestureDetector。 – noob

+0

這個答案可以應用於平移和縮放gridview嗎? –

1

只是一個例子。在主容器視圖中使用setTranslationX/Y和setScaleX/Y。它的孩子將會隨着它進行縮放和翻譯。

public class MainActivity extends Activity { 

    private RelativeLayout mainLayout; 
    private ScaleGestureDetector scaleGestureDetector; 
    private float scale = 1; 
    private PointF touchPoint; 
    private PointF pan; 

    private boolean isScaling; 
    private boolean endScalingNextUp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mainLayout = (RelativeLayout) findViewById(id.main_layout); 
     scaleGestureDetector = new ScaleGestureDetector(this, new ExampleScaleGestureListener()); 
     Button b = ((Button) findViewById(id.btn_reset)); 
     b.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       reset(); 
      } 
     }); 
     reset(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     scaleGestureDetector.onTouchEvent(event); 

     if (scale != 1 && !isScaling) { 
      float x = event.getRawX(); 
      float y = event.getRawY(); 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       touchPoint = new PointF(x, y); 
      } else if (event.getAction() == MotionEvent.ACTION_MOVE && touchPoint != null) { 
       pan.x = x - touchPoint.x; 
       pan.y = y - touchPoint.y; 
       panView(); 
      } 
     } 
     if (isScaling && endScalingNextUp) { 
      if (event.getAction() == MotionEvent.ACTION_POINTER_UP) { 
       endScalingNextUp = false; 
       isScaling = false; 
      } 
     } 

     return true; 
    } 

    private void setPivot(float focusX, float focusY) { 
     mainLayout.setPivotX(focusX); 
     mainLayout.setPivotY(focusY); 
    } 

    private void scaleView() { 
     mainLayout.setScaleX(scale); 
     mainLayout.setScaleY(scale); 
    } 

    private void panView() { 
     mainLayout.setTranslationX(pan.x); 
     mainLayout.setTranslationY(pan.y); 
    } 

    private void reset() { 
     setPivot(0, 0); 
     scale = 1; 
     scaleView(); 
     pan = new PointF(0, 0); 
     panView(); 
     isScaling = false; 
    } 

    private class ExampleScaleGestureListener implements OnScaleGestureListener { 

     private static final float SCALE_SPEED = .02f; 

     @Override 
     public void onScaleEnd(ScaleGestureDetector detector) { 
      endScalingNextUp = true; 
     } 

     @Override 
     public boolean onScaleBegin(ScaleGestureDetector detector) { 
      float focusX = detector.getFocusX(); 
      float focusY = detector.getFocusY(); 
      setPivot(focusX, focusY); 
      isScaling = true; 
      endScalingNextUp = false; 
      return true; 
     } 

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 
      isScaling = true; 
      endScalingNextUp = false; 
      if (detector.getScaleFactor() < 1) { 
       scale -= SCALE_SPEED; 
      } else if (detector.getScaleFactor() > 1) { 
       scale += SCALE_SPEED; 
      } 
      scaleView(); 
      return true; 
     } 
    } 
} 


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:id="@+id/main_layout" 
     tools:context=".MainActivity" > 

     <TextView 
      android:id="@+id/tv" 
      android:layout_centerInParent="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello_world" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/tv" 
      android:layout_marginBottom="69dp" 
      android:layout_marginRight="29dp" 
      android:layout_toLeftOf="@+id/tv" 
      android:src="@drawable/ic_launcher" /> 

     <Button 
      android:id="@+id/btn_reset" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tv" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="22dp" 
      android:text="Reset" /> 

    </RelativeLayout>