2 jz726 jz726 于 2015.06.06 13:42 提问

自定义的View铺满了整个屏幕,本来让它只显示内容大小,在xml中wrap_content无效

protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub

    Paint mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(Color.parseColor("#26ffffff"));
    mPaint.setStrokeWidth(3);
    canvas.drawCircle(80, 80, 80, mPaint);

    Paint arcPaint = new Paint();
    arcPaint.setAntiAlias(true);
    arcPaint.setStyle(Paint.Style.STROKE);
    arcPaint.setColor(Color.parseColor("#d08cc9"));
    arcPaint.setStrokeWidth(3);
    RectF mRectF = new RectF(0, 0, 160, 160);
    canvas.drawArc(mRectF, 0f, 90f, false, arcPaint);

    super.onDraw(canvas);

}   

3个回答

bdmh
bdmh   Ds   Rxr 2015.06.06 15:26
已采纳

这是你画上去的内容,view当然不知道内容多大了,如果这样,你就要设置view的长和宽才行

q3733353520
q3733353520   2015.06.06 14:44

是用view设置Bitmap(Canvas)么,那么它的大小应该和Bitmap一样

jz726
jz726   2015.06.06 15:27

问题已找到,谢谢回答,自定义View,必须要实现onMeasure()方法,在onMeasure()中调用setMeasuredDimension(width,height),给出实际的宽高

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!