title: android - how to get the image edge x/y position inside imageview
body: <p>If i have an ImageView that fills the screen.<br>
  The ImageView background is set to green color.<br>
  I place a bitmap in the ImageView, keeping bitmap proportions.    </p>
  
  <p>A portrait photo in this layout will show green on both left and right side<br>
  (Phone orientation= portrait).     </p>
  
  <p>Now, How do i get the left side x/y position of the edge when the green ends and bitmap begins. </p>
  
  <p>The background for this endeavor project is that i want to write text on the image and save   the image back to a new image with the text. The problem is..</p>
  
  <p>Since I scale the image inSampleSize = 4; and the ImageView shrinking it even more, saving this new picture will give a small small approx 250x350 image. </p>
  
  <p>What i want is to use the x/y positions and transfer the written text to the original inSampleSize = 4 image or to the sdcard 1500x3000 image</p>
  
  <p>I know and read other questions about this that i have to "Do the math calculations" myself
  I just need this small answer.</p>
  
  <p>I forgot i can take a screenshot to clarify.</p>
  
  <p>this is what it look like: (I get a new pen on pressing the button "pen" each pen hold its own unique text and position on the screen</p>
  
  <p><img src="https://i.stack.imgur.com/mnDko.png" alt="enter image description here"></p>
  
  <p>Here is the imageview</p>
  
  <pre><code>    import java.util.HashMap;
      import java.util.UUID;
      import android.app.Activity;
      import android.content.Context;
      import android.graphics.Bitmap;
      import android.graphics.BitmapFactory;
      import android.graphics.Canvas;
      import android.graphics.Color;
      import android.graphics.Paint;
      import android.graphics.Point;
      import android.graphics.Rect;
      import android.graphics.Typeface;
      import android.graphics.drawable.BitmapDrawable;
      import android.graphics.drawable.Drawable;
      import android.os.Environment;
      import android.util.AttributeSet;
      import android.view.Display;
      import android.view.MotionEvent;
      import android.widget.EditText;
      import android.widget.ImageView;
  
      public class DrawView2 extends ImageView {
  
          private HashMap&lt;String, ColorBall2&gt; HashBall ;
          private String balID = ""; // variable to know what ball is being dragged
          public final String PTPPSERVICE_DERECTORY = "/PTPPservice/";    
          private Bitmap bitmap;
          private EditText ed;
          private Paint paint = new Paint();
          private Paint paint2 = new Paint();
          private Paint pTouch = new Paint();
          private EditText addtext;
          private Context ctx;
          private String imagePath;
          private boolean removeBall = false;
        int viewWidth = 0;
          int viewHeight = 0;
          double bitmapHight =0;
          double bitmapWidth =0;  
          double ratio =0;
  
          double startX = 0;
          double endX= 0;
          double startY= 0;
          double endY = 0;
  
          public DrawView2(Context context, AttributeSet atts,String image1) {
  
              super(context, atts);
              this.ctx = context;
              this.imagePath = image1;
              setFocusable(true);
  
              paint.setStyle(Paint.Style.FILL_AND_STROKE);
              paint.setColor(Color.BLACK);
              paint.setTypeface(Typeface.DEFAULT_BOLD); 
  
              paint2.setStyle(Paint.Style.FILL_AND_STROKE);
              paint2.setColor(Color.RED);
  
            addtext = (EditText) ((Activity) ctx).findViewById(R.id.edittextaddtext); 
  
              String filePath = Environment.getExternalStorageDirectory().toString() + imagePath;
              BitmapFactory.Options options = new BitmapFactory.Options();
              options.inSampleSize = 4;
  
              bitmap = BitmapFactory.decodeFile(filePath,options);
              // SAVE RATIO
              int x = bitmap.getWidth();
              int y = bitmap.getHeight();
              if(y&gt;x)
                  ratio = ((double)y)/x;
              if(x&gt;y)
                  ratio = ((double)x)/y;  
              if(y==x)
                  ratio = 1;
              Drawable bit = new BitmapDrawable(bitmap);
              setImageDrawable(bit);
  
          }
          public double getRatio() {
              return ratio;
          }
          public HashMap&lt;String, ColorBall2&gt; getHashBall() {
              return HashBall;
          }
          // RETURN THE ON SCREEN RESIZED BITMAP
          public double getOnScreenBitmapHight(){
  
              return bitmapHight;
          }
          public double getOnScreenBitmapWidth(){
  
              return  bitmapWidth;
          }
          // BITMAP SIZE
          public int getBitmapHight(){
  
              return bitmap.getHeight();
          }
          public int getBitmapWidth(){
  
              return  bitmap.getWidth();
          }
          // GET IMAGEVIEW HIGHT WIDTH
          public int getViewWidth() {
              return viewWidth;
          }
          public int getViewHeight() {
              return viewHeight;
          }
          // START END X Y
          public double getStartX() {
              return startX;
          }
          public double getEndX() {
              return endX;
          }
          public double getStartY() {
              return startY;
          }
          public double getEndY() {
              return endY;
          }
          // SET BALL TEXT
          public void addTextToBall(String text) {
              if(balID != "")
              HashBall.get(balID).setText(text);
          }
          // PATH
          public String getImagePath() {
              return imagePath;
          }
          // THE ORIGINAL INSAMPELSIZE=4 BITMAP
          public Bitmap getBitmap() {
              return bitmap;
          }
          // STOP DRAWAING THE BALL
          public void removeBall(boolean value) {
             removeBall = value;      
          }   
          // THE RECT THAT RETURN WRONG VALUE
          public Rect getRect(){
  
              Rect r = getDrawable().copyBounds();
  
              int drawLeft = r.left;
              int drawTop = r.top;
              int drawRight = r.right;
              int drawBottom = r.bottom;
              return r;
          }
  
          @Override
          protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
              super.onSizeChanged(xNew, yNew, xOld, yOld);
              viewWidth = xNew;
              viewHeight = yNew;
          }
  
          public void addBall(){
  
  // HERE I TRY TO CALCULATE THE BOUNDS LEFT,RIGHT,TOP AND BOTTOM EDGE OF THE BITMAP
  //NOT GOING THAT GOOD
              if(HashBall == null)
                  HashBall = new HashMap&lt;String,ColorBall2&gt;();
  
              //X
              double drawAbleWidth = viewWidth/ratio;
              startX = (viewWidth-drawAbleWidth)/2;
  
              double drawAbleHight = viewHeight/ratio;
              startY = drawAbleHight/2;
  
              int ballY = (viewHeight/2); 
              int ballX = (viewWidth/2);
  
              Point point1 = new Point();
              point1.x = (int) ballX;
              point1.y = (int) ballY;
              String uuId = UUID.randomUUID().toString();
              HashBall.put(uuId,(new ColorBall2(ctx,R.drawable.pen1, point1,uuId)));  
  
  
          }
  
          @Override 
          protected void onDraw(Canvas canvas) {
              super.onDraw(canvas);
  
  
              //canvas.drawCircle(10,10,10,null);
              if(!removeBall &amp;&amp; HashBall != null){
                  for (String key : HashBall.keySet()) {
                      //System.out.println("Key: " + key + ", Value: " + map.get(key));
                      if(addtext!=null)
                          //canvas.drawCircle(HashBall.get(key).getX(),      HashBall.get(key).getY(), 10, paint2);
                          canvas.drawBitmap(HashBall.get(key).getBitmap(), HashBall.get(key).getX()-10, HashBall.get(key).getY()-80, null);
                        canvas.drawText  (HashBall.get(key).getText() + "  X="+HashBall.get(key).getX() + "  Y="+HashBall.get(key).getY()
                                  , HashBall.get(key).getX(), HashBall.get(key).getY(), paint);
                  }
  
              }
  
          }
  
  
          // events when touching the screen
          @Override
          public boolean onTouchEvent(MotionEvent event) {
              int eventaction = event.getAction(); 
  
  
              int X = (int)event.getX(); 
              int Y = (int)event.getY(); 
  
              switch (eventaction ) 
              { 
  
              case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
                  balID = "";
                  for (String key : HashBall.keySet()) {
  
                      // check if inside the bounds of the ball (circle)
                      // get the center for the ball
                      int centerX = HashBall.get(key).getX() + 15;
                      int centerY = HashBall.get(key).getY() + 15;
  
                      // calculate the radius from the touch to the center of the ball
                      double radCircle  = Math.sqrt( (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
  
                      // if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
                      if (radCircle &lt; 33){
                          balID = HashBall.get(key).getID();
                          addtext.setText(HashBall.get(key).getText());
                          break;
                      }
                  }
  
                  break; 
  
  
              case MotionEvent.ACTION_MOVE:   // touch drag with the ball
                  // move the balls the same as the finger
                  if (balID != "") {
                      HashBall.get(balID).setX(X-25);
                      HashBall.get(balID).setY(Y-25);
  
                  }
                  break; 
  
              case MotionEvent.ACTION_UP: 
                  // touch drop - just do things here after dropping
  
                  break; 
              } 
              // redraw the canvas
              invalidate(); 
              return true; 
          }
      }
  </code></pre>