Android Development notes: How to draw a ring on a imageview _android

Source: Internet
Author: User
Tags stub
Drawing the ring is actually very simple, there are probably the following three kinds of ideas. Here, first of all, a method mentioned online. The idea is to draw the inner circle first, then draw the circle (the width of the ring is the width of the paint.setstrokewidth set by paint), and draw the outer circle at the end.
Please see Core Source:
Copy Code code as follows:

<span xmlns= "http://www.w3.org/1999/xhtml" >package Yan.guoqi.rectphoto;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Paint.Style;
Import Android.graphics.RectF;
Import Android.util.AttributeSet;
Import Android.widget.ImageView;
public class Drawimageview extends ImageView {
Private final Paint Paint;
Private final context;
Public Drawimageview (context context, AttributeSet Attrs) {
Super (context, attrs);
TODO auto-generated Constructor stub
This.context = context;
This.paint = new paint ();
This.paint.setAntiAlias (TRUE); Anti-aliasing
This.paint.setStyle (Style.stroke); Draw a hollow circle or a hollow rectangle
}
@Override
protected void OnDraw (Canvas Canvas) {
TODO auto-generated Method Stub
int center = getwidth ()/2;
int innercircle = dip2px (context, 83); Inner Circle Radius
int ringwidth = dip2px (context, 10); Ring width

The first method draws a ring
Draw Inner Circle
This.paint.setARGB (255, 138, 43, 226);
This.paint.setStrokeWidth (2);
Canvas.drawcircle (center, center, Innercircle, This.paint);

Draw a Circle
This.paint.setARGB (255, 138, 43, 226);
This.paint.setStrokeWidth (Ringwidth);
Canvas.drawcircle (center, center, innercircle + 1 +ringwidth/2, this.paint);

Draw an outer circle
This.paint.setARGB (255, 138, 43, 226);
This.paint.setStrokeWidth (2);
Canvas.drawcircle (center, center, Innercircle + ringwidth, this.paint);

Super.ondraw (canvas);

}
/* According to the resolution of the mobile phone from the DP unit into PX (pixel) * *
public static int dip2px (context context, float Dpvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Dpvalue * scale + 0.5f);
}
}
</SPAN>

Summary:
1, this method is divided three times to draw the circle of the Circle and the outer circle of the color can be different, to the paint three times set. You can also set the paint transparency of the drawing ring to about 10 to have the effect of the ring being transparent.
2, three times when drawing the Canvas.drawcircle Center is (center,center), but the three radius is really different. In particular, the second time the circle is drawn, the radius is innercircle + 1 +ringwidth/2. Here the plus 1 is the first outer circle Paint.setstrokewidth (2), the width is set to 2, that is, the width of a single line 1. The same is true for the ringwidth/2 behind.
Examples are as follows (background is the video of the preview camera):



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.