Anroid自定義View-繪製圓環

yayun0516發表於2017-06-11

自定義View程式碼如下:

public class CircleProgress extends View {

    private Paint mPaint;
    private int mWidth;
    private int mHeight;
    private static final int RING_WIDTH =80;

    public CircleProgress(Context context) {
        super(context);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.YELLOW);
    }

    public CircleProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight = getHeight();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(RING_WIDTH);
        canvas.drawCircle(mWidth / 2, mHeight / 2, 100, mPaint);
    }
}

 

MainActivity.java程式碼如下:

 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new CircleProgress(this));
    }
}

 

執行例項如下:

 

相關文章