Canvas不是在自定义Android视图中绘制的

时间:2016-09-11 04:32:59

标签: android android-layout android-studio android-canvas android-custom-view

我在Android中创建了我的自定义View并试图在其中绘制一个圆圈,但没有任何东西正在绘制但没有错误。 这是我的代码

public class ColorOptionsView extends RelativeLayout {
    private View mValue;
    private ImageView mImage;
    private Paint paint;
    String cirlceText;
    int circleColor,circleTextColor;
    float circleTextSize;


    public ColorOptionsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ColorOptionsView, 0, 0);
        try {
            cirlceText = a.getString(R.styleable.ColorOptionsView_circleText);
            circleColor = a.getColor(R.styleable.ColorOptionsView_circleColor, 0);
            circleTextColor = a.getColor(R.styleable.ColorOptionsView_circleTextColor, 0);
            circleTextSize = a.getFloat(R.styleable.ColorOptionsView_circleTextSize,20);
        } finally {
            a.recycle();
        }

    }



    public void setCirlceText(String cirlceText) {
        this.cirlceText = cirlceText;
        invalidate();
        requestLayout();
    }



    public void setCircleColor(int circleColor) {
        this.circleColor = circleColor;
        invalidate();
        requestLayout();
    }



    public void setCircleTextColor(int circleTextColor) {
        this.circleTextColor = circleTextColor;
        invalidate();
        requestLayout();
    }



    public void setCircleTextSize(float circleTextSize) {
        this.circleTextSize = circleTextSize;
        invalidate();
        requestLayout();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setColor(circleColor);
        int centerX = this.getMeasuredWidth()/2;
        int centerY = this.getMeasuredHeight()/2;
        int raduis = 150;
        canvas.drawCircle(centerX,centerY,raduis,paint);

        paint.setColor(circleTextColor);
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(cirlceText,centerX,centerY,paint);
        canvas.drawLine(0,100,100,0,paint);
    }

我没有得到我错的地方。开发人员检查并帮助我。

0 个答案:

没有答案