问题 - 缩放

时间:2011-12-13 06:06:03

标签: android android-layout pinch

我试着捏缩放sample.but itz不工作。我认为问题是活动类无法识别xml文件中的项目。

错误说明mapview无法解析或不是字段。

我的代码如下

package pinchZoom.com;
import android.R;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.nikkoaiello.mobile.android.PinchMapView;

public class PinchZoomActivity extends MapActivity
{

PinchMapView view;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapview);
    view = (PinchMapView) findViewById(R.id.Map);
}

protected boolean isRouteDisplayed() {
    return false;
}

////////////////////////// xml file /////////////////// ////////////////////////////

  <?xml version="1.0" encoding="utf-8"?>

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:isScrollContainer="true"

    >

   <com.nikkoaiello.mobile.android.PinchMapView

        android:id="@+id/Map"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:clickable="true"

        android:apiKey="***MY API KEY***"

   />


   </RelativeLayout>

有人可以告诉我解决这个问题的方法吗?如果您有任何实例,那将是一个真正的帮助!

1 个答案:

答案 0 :(得分:3)

// 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;

    ImageView sampleImageView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample);

     sampleImageView = (ImageView) findViewById(R.id.sampleImageView);

    sampleImageView.setOnTouchListener(new OnTouchListener() {

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

        // Handle touch events here...
        switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            mode = DRAG;

            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);

            if (oldDist > 10f) {
            savedMatrix.set(matrix);
            midPoint(mid, event);
            mode = ZOOM;
            }

            break;

        case MotionEvent.ACTION_UP:

        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;

            break;

        case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
            // ...
            matrix.set(savedMatrix);

            float postTranslateX = event.getX() - start.x;
            float postTranslateY = event.getY() - start.y;

            matrix.postTranslate(postTranslateX, postTranslateY);

            } else if (mode == ZOOM) {
            float newDist = spacing(event);

            if (newDist > 10f) {
                matrix.set(savedMatrix);
                float scale = newDist / oldDist;
                matrix.postScale(scale, scale, mid.x, mid.y);
            }
            }

            break;
        }

        view.setImageMatrix(matrix);

        return true; // indicate event was handled

        }
    });
    }

    /**
     * Checks the spacing between the two fingers on touch.
     * 
     * @param event
     *            the MotionEvent
     * @return float
     */
    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);
    }

    /**
     * Calculates the midpoint between the two fingers.
     * 
     * @param point
     *            the PointF object,
     * @param event
     *            the MotionEvent
     */
    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);
    }

请试一试。