title: Android plotting points over an image
body: <p>I am trying to create a simple app. The steps of the app are </p>
  
  <ol>
  <li>an image should be displayed on  the screen</li>
  <li>When a user selects a point on the image the xy coordinates of the selected point must be saved.</li>
  <li>Then a red dot should be placed over that xy coordinates on the image.</li>
  <li>It should also support pinch zoom in and zoom out. The points should be placed relatively</li>
  <li>Save the image with the dot.
  Thanks a lot in advance for your help.</li>
  </ol>
  
  <p>This is my MAIN ACTIVITY</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      Matrix matrix = new Matrix();
      Matrix savedMatrix = new Matrix();
  
  
      PointF start = new PointF();
       PointF mid = new PointF();
         float oldDist = 1f;
      static final int NONE = 0;
      static final int DRAG = 1;
      static final int ZOOM = 2;
      int mode = NONE;
      private static final String TAG = "Touch" ;
      private RelativeLayout rl_Main;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          rl_Main = (RelativeLayout) findViewById(R.id.rl_main);
          rl_Main.addView(new MyView(this));
      }
  
      class MyView extends View implements OnTouchListener{
  
  
          Paint paint = new Paint();
          Point point = new Point();
          protected ImageView imageView;
          public MyView(Context context) {
              super(context);
              paint.setColor(Color.RED);
              paint.setStrokeWidth(15);
              paint.setStyle(Style.STROKE);
              imageView = (ImageView)findViewById(R.id.imageView1);
              imageView.buildDrawingCache(); 
              imageView.setOnTouchListener(this);
          }
  
          @Override
          protected void onDraw(Canvas canvas) {
                Bitmap b=imageView.getDrawingCache();//BitmapFactory.decodeResource(getResources(), R.drawable.josepg);
                canvas.drawBitmap(b, 0, 0, paint);
                canvas.drawCircle(point.x, point.y, 1, paint);
          }
  
          @Override
          public boolean onTouch(View v,MotionEvent event) {
              ImageView view = (ImageView) v;
              dumpEvent(event);
              switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  point.x = event.getX();
                  point.y = event.getY();
                  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_UP:
                  mode = NONE;
                  Log.d(TAG, "mode=NONE" );
                  break;
                  case MotionEvent.ACTION_MOVE:
                  if (mode == DRAG) {
                  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 &gt; 10f) {
                      matrix.set(savedMatrix);
                      float scale = newDist / oldDist;
                      matrix.postScale(scale, scale, mid.x, mid.y);
                      }
                      }
                  break;
  
                  case MotionEvent.ACTION_POINTER_DOWN:
                      oldDist = spacing(event);
                      Log.d(TAG, "oldDist=" + oldDist);
                      if (oldDist &gt; 10f) {
                      savedMatrix.set(matrix);
                      midPoint(mid, event);
                      mode = ZOOM;
                      Log.d(TAG, "mode=ZOOM" );
                      }
                      break;
  
  
  
  
  
  
  
              }
              view.setImageMatrix(matrix);
                invalidate();
              return true;
  
          }
  
      }
       class Point {
              float x, y;
          }
  
       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);
          }
  
  }
  </code></pre>
  
  <p>This is my XML</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/rl_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.imagemerge.MainActivity$PlaceholderFragment" &gt;
  
      &lt;ImageView android:id="@+id/imageView1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:src="@drawable/josepg"
      android:scaleType="matrix" &gt;
      &lt;/ImageView&gt;
  
      &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>But I keep getting this error</p>
  
  <pre><code>07-15 10:36:20.808: W/dalvikvm(17289): threadid=1: thread exiting with uncaught exception (group=0x415ebba8)
  07-15 10:36:20.808: E/AndroidRuntime(17289): FATAL EXCEPTION: main
  07-15 10:36:20.808: E/AndroidRuntime(17289): Process: com.example.imagenew, PID: 17289
  07-15 10:36:20.808: E/AndroidRuntime(17289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagenew/com.example.imagenew.MainActivity}: java.lang.NullPointerException
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.os.Handler.dispatchMessage(Handler.java:102)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.os.Looper.loop(Looper.java:136)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.ActivityThread.main(ActivityThread.java:5001)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at java.lang.reflect.Method.invokeNative(Native Method)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at java.lang.reflect.Method.invoke(Method.java:515)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at dalvik.system.NativeStart.main(Native Method)
  07-15 10:36:20.808: E/AndroidRuntime(17289): Caused by: java.lang.NullPointerException
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at com.example.imagenew.MainActivity$MyView.&lt;init&gt;(MainActivity.java:63)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at com.example.imagenew.MainActivity.onCreate(MainActivity.java:48)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.Activity.performCreate(Activity.java:5231)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
  07-15 10:36:20.808: E/AndroidRuntime(17289):    ... 11 more
  </code></pre>