SoFunction
Updated on 2025-04-10

Android development notes: Implementation method of drawing circles on ImageView


<SPAN xmlns="http:///1999/xhtml">package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class DrawImageView extends ImageView {
 private final Paint paint;
 private final Context context;&nbsp;
 public DrawImageView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
   = context;
   = new Paint();
(true); //Extinguish the aliasing
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (); //Draw hollow circles or hollow rectangles
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  int center = getWidth()/2;
int innerCircle = dip2px(context, 83); //The radius of the inner circle
int ringWidth = dip2px(context, 10);   //Ring width

// The first method to draw a circle
//Draw the inner circle
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (255, 138, 43, 226);
  (2);
  (center, center, innerCircle, );&nbsp;  

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Draw the circle
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (255, 138, 43, 226);
  (ringWidth);
  (center, center, innerCircle + 1 +ringWidth/2, );&nbsp;  

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Draw the outer circle&nbsp;
  (255, 138, 43, 226);
  (2);
  (center, center, innerCircle + ringWidth, );&nbsp;&nbsp;  

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (canvas);

 }
/* Convert from dp to px (pixel) based on the resolution of the phone */
 public static int dip2px(Context context, float dpValue) { 
  final float scale = ().getDisplayMetrics().density; 
  return (int) (dpValue * scale + 0.5f); 
 }&nbsp;
}
</SPAN>