Android 自定义 View onDraw 不工作

标签 android android-layout

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

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

我想做的是绘制 View ,以便它:

  • 中间有一个黑条
  • 位于它膨胀的 Activity 的中间点
  • 具有在其膨胀的 Activity 中计算的高度

为此,我尝试了以下方法:

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() 从 Activity 中调用:

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(),但这不是我主要关心的问题。也许这有助于解释为什么事情似乎没有正确绘制。有什么想法吗?

最佳答案

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

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

高度是 107 但 startY = 539 和 stopY = 559

关于Android 自定义 View onDraw 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32469794/

相关文章:

android - 如何在不使用java的情况下在android上的TextView中制作可点击的url链接

java - AsyncTask新手问题

android - android 软键盘开启时调整布局

android - 当键盘打开时,布局应该在 android 中移动到上方

java - 在 Android 上获取当前 GPS 位置

android - 如果另一个 TextView 太长,如何在新行中移动 TextView

android - 未决 Intent 未收到不同 notificationId 的 putExtraBudle

java - 另一种颜色是 Cardview 中的绑定(bind)颜色?

Android 布局和图像按钮和灰色边框