我用Canvas和Paint制作了一个小装载圈。 这是我第一次尝试使用这些类,所以它可能是我错误使用的。
我的问题是,绘画的分辨率太低了。我可以清楚地看到像素。
我怎么能改进这个?
这是我的班级:
public class LoadingCircle extends LinearLayout {
public LoadingCircle(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
}
// time
int countDownTime = 180;
Paint paint = new Paint();
RectF oval = new RectF();
Path path = new Path();
// value
int value = 360 / countDownTime;
// starting progress
int progress = -360 - value;
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = (float) getWidth();
float height = (float) getHeight();
float center_x = width / 2, center_y = height / 2;
float loadingRadius = (float) ((width / 2)*0.85);
float whiteRadius = (float) (loadingRadius * 1.06);
float greenRadius = (float) (loadingRadius * 1.14);
// **background green circle**/
oval.set(center_x - greenRadius, center_y - greenRadius, center_x + greenRadius, center_y + greenRadius);
paint.setColor(Color.parseColor("#a3d47b"));
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawArc(oval, 270, 360, true, paint);
// ****//
// **background green circle**/
oval.set(center_x - whiteRadius, center_y - whiteRadius, center_x + whiteRadius, center_y + whiteRadius);
paint.setColor(Color.parseColor("#ffffff"));
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawArc(oval, 270, 360, true, paint);
// **Loading circle**//
path.addCircle(center_x, center_y, loadingRadius, Path.Direction.CW);
paint.setColor(Color.parseColor("#71b23c"));
paint.setStyle(Paint.Style.FILL_AND_STROKE);
oval.set(center_x - loadingRadius, center_y - loadingRadius, center_x + loadingRadius, center_y + loadingRadius);
progress = progress + value;
Log.i("proges: ", progress + "");
canvas.drawArc(oval, 270, progress, true, paint);
// /**//
}
public void setCountDownTime(int time) {
this.countDownTime = time;
this.value = 360 / countDownTime;
this.progress = -360 - value;
}
// reseting loading circle
public void reset() {
this.progress = -360 - value;
this.invalidate();
}
}
答案 0 :(得分:13)
声明这样的颜料:
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
这将启用抗锯齿功能,你就可以摆脱那种丑陋的像素化。
或者,您可以在您的功能中执行此操作:
paint.setAntiAlias(true);