请看核心源码:
代码如下:
<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 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); //消除锯齿
this.paint.setStyle(Style.STROKE); //绘制空心圆或 空心矩形
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
int center = getWidth()/2;
int innerCircle = dip2px(context, 83); //内圆半径
int ringWidth = dip2px(context, 10); //圆环宽度
// 第一种方法绘制圆环
//绘制内圆
this.paint.setARGB(255, 138, 43, 226);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center, center, innerCircle, this.paint);
//绘制圆环
this.paint.setARGB(255, 138, 43, 226);
this.paint.setStrokeWidth(ringWidth);
canvas.drawCircle(center, center, innerCircle + 1 +ringWidth/2, this.paint);
//绘制外圆
this.paint.setARGB(255, 138, 43, 226);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center, center, innerCircle + ringWidth, this.paint);
super.onDraw(canvas);
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
</SPAN>
总结:
1,这种分三次来绘制的方法,可以将圆环的内圆 圆环 和外圆的颜色设成不一样的,对paint进行三次设置。还可以将绘制圆环的paint透明度设成10左右就会有圆环透明的效果。
2,三次绘制时的canvas.drawCircle圆心都是(center,center),但三次半径确实不一样的。尤其是第二次绘制圆环的时候,半径是innerCircle + 1 +ringWidth/2。这里的加1是第一次外圆paint.setStrokeWidth(2);宽度设成2,也就是说单条线的宽度1。后面的ringWidth/2也是同理。
示例如下(底色是预览摄像头的视频):
您可能感兴趣的文章:android自定义View实现圆环颜色选择器利用Android模仿微信摄像圆环进度效果实例Android自定义带动画的半圆环型进度效果Android自定义view绘制圆环占比动画Android实现动态圆环的图片头像控件Android自定义View实现圆环交替效果Android中自定义View实现圆环等待及相关的音量调节效果Android自定义View之酷炫圆环(二)Android自定义View之酷炫数字圆环Android实现光点模糊渐变的自旋转圆环特效