1

我有一张巴士路线图作为图像。使用缩放控制器图像正在缩小但不放大请查看我的代码并让我知道要完成的更改使其工作..我正在 Gingerbread 即 API 10 上开发我的应用程序

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;

 public class Busmaps extends Activity {

ImageView img;
ZoomControls zoom;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.busmaps);

    img = (ImageView) findViewById(R.id.imageViewmaps1);
    zoom = (ZoomControls) findViewById(R.id.zoomControls1);

    zoom.setOnZoomInClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        int w = img.getWidth();
        int h = img.getHeight();

        RelativeLayout.LayoutParams params = 
            new RelativeLayout.LayoutParams(w + 50, h + 50);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);

        img.setLayoutParams(params);
    }
  });

      zoom.setOnZoomOutClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        int w = img.getWidth();
        int h = img.getHeight();

        RelativeLayout.LayoutParams params = 
            new RelativeLayout.LayoutParams(w - 50, h - 50);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);

        img.setLayoutParams(params);
    }
  });
  }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.bus_map_zoom, menu);
    return true;
  }
  }

我的 xml 表示公交路线图像:-

<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" >

  <ImageView
    android:id="@+id/imageViewmaps1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/map" />

  <ZoomControls
    android:id="@+id/zoomControls1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
     />

  </RelativeLayout>

我应该如何让我的缩放控制同时适用于放大和缩小。

4

2 回答 2

0
public class Busmap extends Activity implements OnTouchListener 
{
private static final String TAG = "Touch";
@SuppressWarnings("unused")
private static final float MIN_ZOOM = 1f,MAX_ZOOM = 1f;

// These matrices will be used to scale points of the image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

// The 3 states (events) which the user is trying to perform
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

// these PointF objects are used to record the point(s) the user is touching
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.busmaps);
    ImageView view = (ImageView) findViewById(R.id.imageViewmaps1);
    view.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) 
{
    ImageView view = (ImageView) v;
    view.setScaleType(ImageView.ScaleType.MATRIX);
    float scale;

    dumpEvent(event);
    // Handle touch events here...

    switch (event.getAction() & MotionEvent.ACTION_MASK) 
    {
        case MotionEvent.ACTION_DOWN:   // first finger down only
                                            savedMatrix.set(matrix);
                                            start.set(event.getX(), event.getY());
                                            Log.d(TAG, "mode=DRAG"); // write to LogCat
                                            mode = DRAG;
                                            break;

        case MotionEvent.ACTION_UP: // first finger lifted

        case MotionEvent.ACTION_POINTER_UP: // second finger lifted

                                            mode = NONE;
                                            Log.d(TAG, "mode=NONE");
                                            break;

        case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down

                                            oldDist = spacing(event);
                                            Log.d(TAG, "oldDist=" + oldDist);
                                            if (oldDist > 5f) {
                                                savedMatrix.set(matrix);
                                                midPoint(mid, event);
                                                mode = ZOOM;
                                                Log.d(TAG, "mode=ZOOM");
                                            }
                                            break;

        case MotionEvent.ACTION_MOVE:

                                            if (mode == DRAG) 
                                            { 
                                                matrix.set(savedMatrix);
                                                matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix  of points
                                            } 
                                            else if (mode == ZOOM) 
                                            { 
                                                // pinch zooming
                                                float newDist = spacing(event);
                                                Log.d(TAG, "newDist=" + newDist);
                                                if (newDist > 5f) 
                                                {
                                                    matrix.set(savedMatrix);
                                                    scale = newDist / oldDist; // setting the scaling of the
                                                                                // matrix...if scale > 1 means
                                                                                // zoom in...if scale < 1 means
                                                                                // zoom out
                                                    matrix.postScale(scale, scale, mid.x, mid.y);
                                                }
                                            }
                                            break;
    }

    view.setImageMatrix(matrix); // display the transformation on screen

    return true; // indicate event was handled
}



private float spacing(MotionEvent event) 
{
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
}



private void midPoint(PointF point, MotionEvent event) 
{
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);
}
于 2013-11-20T11:17:44.100 回答
0

请将您的 Zoom in 和 ZoomOut 侦听器替换为以下内容:

zoom.setOnZoomInClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        float x = img.getScaleX();
        float y = img.getScaleY();

        img.setScaleX((float) (x+1));
        img.setScaleY((float) (y+1));
    }
});

    zoom.setOnZoomOutClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        float x = img.getScaleX();
        float y = img.getScaleY();

        img.setScaleX((float) (x-1));
        img.setScaleY((float) (y-1));
    }
});
}

另一种选择是在内置缩放控制器的 Web 视图中加载图像。

如下:

 String page = "<html><body><center><img src=\""+path to your image+"\"/></center></body></html>";
    webView.loadDataWithBaseURL("fake",page, "text/html", "UTF-8","");

如果您不想使用内置的 web 视图缩放控制器,那么您只需放置自己的按钮并在 web 视图上应用放大和缩小,如下所示:

webView.setInitialScale(ZOOM_LEVEL);
于 2013-03-05T03:47:52.703 回答