Android自定义视图onDraw无效

时间:2015-09-09 01:58:22

标签: android android-layout

我有一个通过xml添加的自定义视图:

<com.my.app.views.CustomView
    android:id="@+id/custom_view"
    android:layout_width="match_parent"
    android:background="@color/black"
    android:layout_height="0dp"
    />

我想要做的是绘制视图以便:

  • 在其中间包含一个黑条
  • 位于其充气的活动的中间点
  • 的高度是在活动中计算的高度

为此,我已经尝试过:

public class CustomView extends View {
    public Context context;
    public int barViewWidth;
    public int barViewHeight;
    public int deviceHeight;

    public final static double LINE_STROKE = 20.0;

    public CustomView(Context context) {
        super(context, null, 0);

        this.context = context;
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Timber.i("onDraw %s", "WHOOPS");

        setBackgroundColor(getResources().getColor(R.color.transparent));

        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.black));
        paint.setStrokeWidth((int)LINE_STROKE);

        float startX = 0.0f;

        Timber.i("deviceHeight %d", deviceHeight);
        Timber.i("barViewHeight %d", barViewHeight);
        Timber.i("barViewWidth %d", barViewWidth);

        double yOrigin = (deviceHeight / 2) - (barViewHeight / 2);
        float startY = (float)yOrigin;

        float stopX = barViewWidth;
        float stopY = (float)yOrigin + (float)LINE_STROKE;

        Timber.i("startX %s", Float.toString(startX));
        Timber.i("startY %s", Float.toString(startY));
        Timber.i("stopX %s", Float.toString(stopX));
        Timber.i("stopY %s", Float.toString(stopY));

        canvas.drawLine(startX, startY, stopX, stopY, paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width;
        int height;

        int widthSize = View.MeasureSpec.getSize(widthMeasureSpec);
        barViewWidth = widthSize;

        Timber.i("onMeasure barViewHeight %d", barViewHeight);

        width = widthSize;
        height = barViewHeight;

        setMeasuredDimension(width, height);
    }

    public void setHeight(int activityHeight, int segmentHeight) {
        this.deviceHeight = activityHeight;
        this.barViewHeight = segmentHeight;
    }
}
从活动中调用

setHeight()

customView.setHeight(height(), viewHeight());

我知道值似乎正在设置,因为这是我的Timber输出显示的内容:

onMeasure barViewHeight 107
onDraw WHOOPS
deviceHeight 1184
barViewHeight 107
barViewWidth 720
startX 0.0
startY 539.0
stopX 720.0
stopY 559.0
onMeasure barViewHeight 107
onDraw WHOOPS
deviceHeight 1184
barViewHeight 107
barViewWidth 720
startX 0.0
startY 539.0
stopX 720.0
stopY 559.0

然而,尽管数字看起来还不错,但View并未实际展示。我似乎也在调用onMeasure(),因此onDraw()两次,但这不是我的主要关注点。也许这有助于解释为什么事情似乎并没有正确地绘制。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

barViewHeight 107
barViewWidth 720
startX 0.0
startY 539.0
stopX 720.0
stopY 559.0

似乎你在可见范围之外画线。

高度为107但startY = 539且stopY = 559