Hi all,

I have followed the example of using multi touch which can be found
here..http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-
android-2/1747?tag=mantle_skin;content

I am pretty sure I have included, in my code, all of the required
elements but, although the dumpEvent log shows that my touch gestures
are being picked up, and accurately, I can't move my picture around or
zoom in.

The only difference I can see is that my picture is loaded from the SD
card whereas in the example the picture is a drawable...

Here is my code


package com.pengilleys.fishingsnapz;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class ImagePreview extends Activity implements OnTouchListener{
        static final int PROGRESS_DIALOG = 0;
        ProgressDialog progressDialog;
        private Bundle extras;
        protected static Bitmap bm;
        protected DBAdapter db = new DBAdapter(ImagePreview.this);
        Long id;
        GetHandler handler = new GetHandler();

        //Touch gesture related variables
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();

        // Remember some things for zooming
        PointF start = new PointF();
        PointF mid = new PointF();

        float oldDist = 0;

        // We can be in one of these 3 states
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        int mode = NONE;

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

        try{
                getImage();

                //start a progressdialog
                showDialog(PROGRESS_DIALOG);
                extras = getIntent().getExtras();
                id = (Long) extras.get(DBAdapter.KEY_ROWID);

                db.open();

                bm = getImage();


                //Log.e("ONCREATE","setImageBitmap="+bm.toString());
            ImageView imageV =
(ImageView)findViewById(R.id.image_preview);
                imageV.setOnTouchListener(this);
                imageV.setImageBitmap(bm);
                dismissDialog(PROGRESS_DIALOG);


        }catch(Exception e){
                Log.e("ONCREATE",e.toString());
        }finally{
                db.close();
        }

        }

        //setup the progressdialog
        protected Dialog onCreateDialog(int id) {
                switch(id) {
        case PROGRESS_DIALOG:
                try{
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("Loading snap...");
            progressDialog.setOwnerActivity(this);
            return progressDialog;
        }catch(Exception e){
                        Log.e("ONCREATEDIALOG",e.toString());
                }
        default:
            return null;
        }

    }

        private class GetHandler extends Handler{
                public void handleMessage(Message message){
                        ImagePreview.this.getImage();
                }
        }

        public Bitmap getImage(){
                try {
                        Uri uri=null;

                        Cursor c = db.fetchUri(id);
                        uri =
Uri.parse(c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_URI)));

                        ContentResolver cr = getContentResolver();

                        Bitmap bitmap = 
MediaStore.Images.Media.getBitmap(cr,uri);

                        return bitmap;
                                } catch (FileNotFoundException e) {
                                        // TODO Auto-generated catch block
                                        Log.e("GETIMAGE",e.toString());
                                        return null;
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        Log.e("GETIMAGE",e.toString());
                                        return null;
                                }catch(Exception e){
                                        Log.e("GETIMAGE",e.toString());
                                        return null;
                                }

        };

        @Override
        public boolean onTouch(View v, MotionEvent event) {
                final String TAG = "Touch";
                // TODO Auto-generated method stub
                ImageView view = (ImageView) v;

                dumpEvent(event);

                switch(event.getAction() & MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_DOWN:
                        savedMatrix.set(matrix);
                        start.set(event.getX(), event.getY());
                        Log.d(TAG,"mode=DRAG");
                        mode=DRAG;
                        break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_DOWN:
                        oldDist=spacing(event);
                        Log.d(TAG, "oldDist=" + oldDist);
                        if(oldDist>10f){
                                savedMatrix.set(matrix);
                                midPoint(mid,event);
                                mode=ZOOM;
                                Log.d(TAG,"mode=ZOOM");
                        }

                        break;
                case MotionEvent.ACTION_MOVE:

                        if(mode==DRAG){
                                Log.d(TAG,"mode=MOVE");
                                matrix.set(savedMatrix);
                                matrix.postTranslate(event.getX() - 
start.x,event.getY()-start.y);
                        }else if(mode==ZOOM){
                                float newDist= spacing(event);
                                Log.d(TAG, "newDist=" + newDist);
                                if(newDist>10f){
                                        matrix.set(savedMatrix);
                                        float scale = newDist / oldDist;
                                        matrix.postScale(scale, scale, mid.x, 
mid.y);
                                }
                        }
                        break;
                }
                        //perform the transformation
                        view.setImageMatrix(matrix);

                return true;
        }

        private static 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 static 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);
        }

        /** Show an event in the logcat view for debugging **/
        private void dumpEvent(MotionEvent event){
                String names[] =
{"DOWN","UP","MOVE","CANCEL","OUTSIDE","POINTER_DOWN","POINTER_UP","7?","8?","9?"};
                StringBuilder sb = new StringBuilder();
                int action = event.getAction();
                int actionCode = action & MotionEvent.ACTION_MASK;
                sb.append("event ACTION_").append(names[actionCode]);
                if(actionCode==MotionEvent.ACTION_POINTER_DOWN
                                || actionCode==MotionEvent.ACTION_POINTER_UP){
                        sb.append("pid ").append(
                        action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
                        sb.append(")");
                };
                sb.append(" [");
                for(int i=0;i < event.getPointerCount();i++){
                        sb.append("#").append(i);
                        sb.append("(pid ").append(event.getPointerId(i));
                        sb.append(")=").append((int) event.getX(i));
                        sb.append(",").append((int) event.getY(i));
                        if(i+1 < event.getPointerCount())
                                sb.append(";");
                }
                sb.append("]");
                Log.d("DUMP", sb.toString());
        }

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to