Passing x,y coordinates of drawCircle() to drawLine()


I am trying to draw line between two circles. I have drawn two circles on canvas, These two circles have their cx and cy points. <strong>I want to pass these cx and cy of both circles to drawLine()'s startx,starty and stopx,stopy. So that a line should be drawn between these two circles.</strong> These two circles can be parallel to each other or horizontal to each other. What I have planned so far is that I should apply action-listener to circles in order to get their x and y coordinates. The problem I am facing here is that <strong>drawCircle()</strong> method call is in <strong>onDraw()</strong> and <strong>lineDraw()</strong> method call is in <strong>onTouch()</strong>. I have shared my code below please suggest solution so that I can <strong>pass circle's coordinates to drawLine().</strong>

package com.example.circleline; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class MainActivity extends Activity implements OnTouchListener { ImageView imageView; Bitmap bitmap; Canvas canvas; Paint paint; Paint pDot = new Paint(); float downx=0,downy=0,upx=30,upy=50; int cols = 5; int rows = 6; @SuppressWarnings("deprecation") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView=(ImageView)this.findViewById(R.id.imageView1); Display currentDisplay= getWindowManager().getDefaultDisplay(); float dw=currentDisplay.getWidth(); float dh=currentDisplay.getHeight(); bitmap=Bitmap.createBitmap((int) dw, (int)dh, Bitmap.Config.ARGB_8888); canvas=new Canvas(bitmap); paint=new Paint(); paint.setColor(Color.BLUE); imageView.setImageBitmap(bitmap); dw=canvas.getWidth()/(cols+1); dh=canvas.getDensity()/(rows+1); for (int y=0;y<rows;y++) { for (int x=0;x<cols;x++) { canvas.drawCircle((x + 1) * dw, (y + 1) *(3* dh), 20, pDot); } } imageView.setOnTouchListener(this); //canvas.drawCircle(upx, upy, 20, pDot); } public boolean onTouch(View v, MotionEvent e) { int action=e.getAction(); switch(action) { case MotionEvent.ACTION_DOWN: downx=e.getX(); downy=e.getY(); Log.d("Umar", String.valueOf(downx)); Log.d("Farooq", String.valueOf(downy)); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: upx=e.getX(); upy=e.getY(); canvas.drawLine(downx, downy, upx, upy, paint); imageView.invalidate(); break; case MotionEvent.ACTION_CANCEL: break; default: break; } return true; } }


Put lineDraw() in onDraw() as well. Put all your drawing operations in onDraw(), they don't belong anywhere else. In onTouch just save the x/y, and in onDraw() draw at x/y.


  • Update data in d3.js group
  • how to get the drawing graphic on picture box in c#
  • Javascript syntax null : ?{}
  • angularjs ng-bind-html html input part missing
  • How do I properly code a javascript property and method using the 'prototype' function?
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • draw a B+ tree in latex
  • Circle movement upon rectangle Collision
  • Load image without autoscaling in Android
  • Differences between drawing an Ellipse in Android and Java
  • Exception in the iconization of JInternalFrame with DefaultDesktopManager
  • Winforms: Add a close “x” button in a UserControl
  • write text on image and show it to a imageview
  • c# winform DrawToBitmap offscreen
  • How to discover Font Type?
  • EditText is covered by Keyboard
  • How to draw a line dynamically in android [duplicate]
  • URLConnection doesn't work since API 10 and higher?
  • Using a canvas object in a thread to do simple animations - Java
  • Chart.js Multiple dataset
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • req.body is undefined - nodejs
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • How reduce the height of an mschart by breaking up the y-axis
  • Excel - Autoshape get it's name from cell (value)
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Check if a string to interpolate provides expected placeholders
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Symfony2: How to get request parameter
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • RestKit - RKRequestDelegate does not exist
  • Is there a mandatory requirement to switch app.yaml?
  • Traverse Array and Display in markup
  • Cannot Parse HTML Data Using Android / JSOUP
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • Android Google Maps API OnLocationChanged only called once
  • unknown Exception android
  • Android Heatmap on canvas or ImageView