2013-03-19 61 views
0

我正在Android平板應用程序,我有一個藍圖,我可以捏縮放。從側面菜單中,我可以將小符號拖入可縮放的圖像中,稍後我可以點擊並修改該圖像。把它想象成地圖上的標記。Android:捏縮放ImageView與附加的子視圖

對於縮放部分,我到目前爲止使用了一個WebView。這是我處理這個功能的代碼:

public void handlePinchZooming() { 
    WebView webView = (WebView) findViewById(R.id.blueprintWebView); 
    webView.setInitialScale(50); 
    webView.getSettings().setBuiltInZoomControls(true); 
    WebSettings settings = webView.getSettings(); 
    settings.setUseWideViewPort(true); 
    settings.setLoadWithOverviewMode(true); 
    webView.loadUrl("file:///android_asset/blueprint_example.jpg"); 
} 

對於拖動標記到圖像,並加入他們,因爲我用下面的代碼孩子:

private void handleDragDrop() { 
    findViewById(R.id.markerButton01).setOnTouchListener(new DragDropTouchListener()); 
    findViewById(R.id.markerButton02).setOnTouchListener(new DragDropTouchListener()); 
    findViewById(R.id.markerButton03).setOnTouchListener(new DragDropTouchListener()); 
    findViewById(R.id.blueprintWebView).setOnDragListener(new DragDropDragListener()); 
} 

private final class DragDropTouchListener implements View.OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     view.setAlpha(100); 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      view.getLayoutParams().width = 50; 
      view.getLayoutParams().height = 50; 
      ClipData data = ClipData.newPlainText("", ""); 
      View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      view.startDrag(data, shadowBuilder, view, 0); 
      view.setVisibility(View.INVISIBLE); 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

class DragDropDragListener implements View.OnDragListener { 
    Drawable enterShape = getResources().getDrawable(R.drawable.blueprint_example); 
    Drawable normalShape = getResources().getDrawable(R.drawable.blueprint_example); 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int action = event.getAction(); 
     switch (event.getAction()) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       // Do nothing 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 
       v.setBackgroundDrawable(enterShape); 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       v.setBackgroundDrawable(normalShape); 
       break; 
      case DragEvent.ACTION_DROP: 
       // Dropped, reassign View to ViewGroup 
       View view = (View) event.getLocalState(); 
       ViewGroup owner = (ViewGroup) view.getParent(); 
       owner.removeView(view); 
       WebView container = (WebView) v; 
       container.addView(view); 
       view.setX(event.getX()-25); 
       view.setY(event.getY()-25); 
       view.setVisibility(View.VISIBLE); 
       setMarkerOnClickListener(view); 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       v.setBackgroundDrawable(normalShape); 
      default: 
       break; 
     } 
     return true; 
    } 
} 

現在,當我周圍刷的藍圖圖像,標記隨之移動,但只要我放大或縮小,標記總是保持相同的絕對距離,這使得它們遠離了他們在藍圖上所需的位置。

我不知道,如果你需要這一點,但這裏是我的佈局XML文件的一部分:

<LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:gravity="center|right"> 

    <WebView 
      android:layout_width="800dp" 
      android:layout_height="600dp" 
      android:id="@+id/blueprintWebView" 
      android:layout_gravity="center" android:layout_margin="100dp"/> 
    <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="150dp" 
      android:layout_height="fill_parent" android:layout_gravity="right" android:layout_marginRight="80dp" 
      android:gravity="center_vertical|right" android:baselineAligned="false"> 
     <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="150dp"> 
      <ImageView 
        android:layout_width="fill_parent" 
        android:layout_height="150dp" 
        android:id="@+id/imageButton01" android:src="@drawable/icon_printer" 
        android:adjustViewBounds="false" 
        android:longClickable="false" 
        android:cropToPadding="false" android:scaleType="fitCenter" android:layout_marginTop="20dp" 
        android:layout_marginBottom="20dp"/> 
      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/markerButton01" android:src="@drawable/icon_marker_pink" 
        android:scaleType="fitCenter" android:cropToPadding="false" android:visibility="visible" 
        android:focusableInTouchMode="false" android:alpha="0" android:adjustViewBounds="false"/> 
     </RelativeLayout> 
     <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="150dp"> 
      <ImageView 
        android:layout_width="fill_parent" 
        android:layout_height="150dp" 
        android:id="@+id/imageButton02" android:src="@drawable/icon_printer" 
        android:scaleType="fitCenter" android:layout_marginTop="20dp" android:layout_marginBottom="20dp"/> 
      <ImageView 
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/markerButton02" android:src="@drawable/icon_marker_pink" 
        android:scaleType="fitCenter" android:cropToPadding="false" android:visibility="visible" 
        android:focusableInTouchMode="false" android:alpha="0" android:adjustViewBounds="false" 
        android:baselineAlignBottom="false"/> 
     </RelativeLayout> 
     <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="150dp"> 
      <ImageView 
        android:layout_width="fill_parent" 
        android:layout_height="150dp" 
        android:id="@+id/imageButton03" android:src="@drawable/icon_printer" android:cropToPadding="false" 
        android:scaleType="fitCenter" android:layout_marginBottom="20dp" android:layout_marginTop="20dp"/> 
      <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" 
         android:id="@+id/markerButton03" android:src="@drawable/icon_marker_pink" 
         android:scaleType="fitCenter" android:cropToPadding="false" android:visibility="visible" 
         android:focusableInTouchMode="false" android:alpha="0" android:adjustViewBounds="false" 
         android:baselineAlignBottom="false" android:focusable="false"/> 
     </RelativeLayout> 
    </LinearLayout> 
</LinearLayout> 

我會感激你的幫助來解決這個小問題。 :)

問候,

指關節

回答

0

您可能必須自己重新計算捏的位置。我不知道,如果web視圖使您可以獲取有關它的縮放狀態等 也許你會想切換到這個方法來代替,讓您在變焦過程中完全控制: Android Image View Pinch Zoom

另一種方式能在webview中顯示標記,而不是使用javascript。但是,這隻有在你進入網絡發展的時候。