如何使用drawArc()方法在onDraw中创建的弧移动到屏幕中心?

时间:2017-09-21 09:02:50

标签: android android-layout view paint

我使用drawArc()方法绘制了一个内半径和外半径的圆。我很难将它放到屏幕的中心。目前,使用以下代码,我将它靠近左角。

public Circle(Context context, AttributeSet attrs) {
    super(context, attrs);

    final int strokeWidth = 180;

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(strokeWidth);


    DisplayMetrics metrics = new DisplayMetrics();

   ((Activity)getContext())
    .getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    rect = new RectF(width/2-strokeWidth, height/2-strokeWidth, 
    width/2+strokeWidth, height/2+strokeWidth);

    Shader shader = new SweepGradient(rect.centerX(), rect.centerY(), 
    colours, null);[![enter image description here][1]][1]
    Matrix matrix = new Matrix();
    matrix.setRotate(225, rect.centerX(), rect.centerY());
    shader.setLocalMatrix(matrix);
    paint.setShader(shader);
    //Initial Angle (optional, it can be zero)
    angle = 0;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);

}

0 个答案:

没有答案