2013-03-19 58 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。但是,这只有在你进入网络发展的时候。