如何使用Canvas设置图像

时间:2018-04-07 18:08:17

标签: android android-canvas

我想在我的活动中获取位图,但不知道如何从我的自定义视图中获取它。这是我的尝试

这是我的绘制方法:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        centerX = getMeasuredWidth()/ 2;
        centerY = getMeasuredHeight()/ 2;
        int radius = Math.min(centerX,centerY);
        canvas.drawCircle(centerX, centerY, radius, outerCircularPaint);
        canvas.drawCircle(centerX, centerY, radius-1, innerCircularPaint);

    }

这是我的完整代码:

public class CircleIcon extends View {
    private int centerX,centerY;
    private Paint outerCircularPaint,innerCircularPaint;
    private static final int STROKE_WIDTH = 5;
    Bitmap bitmap;
    public CircleIcon(Context context) {
        super(context);
        init();
    }

    private void init()
    {

        bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
        outerCircularPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        outerCircularPaint.setStyle(Paint.Style.STROKE);
        outerCircularPaint.setStrokeWidth(STROKE_WIDTH);

        outerCircularPaint.setAntiAlias(true);
        outerCircularPaint.setStrokeCap(Paint.Cap.BUTT);
        outerCircularPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_black));

        innerCircularPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        innerCircularPaint.setStyle(Paint.Style.FILL);
        innerCircularPaint.setStrokeWidth(STROKE_WIDTH);
        innerCircularPaint.setColor(ContextCompat.getColor(getContext(), R.color.white));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        centerX = getMeasuredWidth()/ 2;
        centerY = getMeasuredHeight()/ 2;
        int radius = Math.min(centerX,centerY);
        canvas.drawCircle(centerX, centerY, radius, outerCircularPaint);
        canvas.drawCircle(centerX, centerY, radius-1, innerCircularPaint);

    }
}

0 个答案:

没有答案