Android自定义视图画布清除失败

huangapple go评论81阅读模式
英文:

Android custom view canvas clear fails

问题

我正在编写一个简单的自定义“View”,应该绘制一个RSSI信号形状(两个重叠的三角形,一个根据信号级别比例填充)。组件的工作原理已经实现,除了清除画布部分。

我认为我需要做类似这样的事情:

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

    final int mWidth = getMeasuredWidth();
    final int mHeight = getMeasuredHeight();
    final float _fillw = mWidth * (mLevel) / 100f;
    final float _fillh = mHeight * (100f - mLevel) / 100f;

    //canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    canvas.drawColor(Color.BLACK);

    if (mLevel < mWarnLevel)
        mFillPaint.setColor(mWarnFillColor);
    else if ((mLevel >= mWarnLevel) && (mLevel < mSafeLevel))
        mFillPaint.setColor(mFillColor);
    else if (mLevel >= mSafeLevel)
        mFillPaint.setColor(mSafeFillColor);

    mFramePath.moveTo(0, mHeight);
    mFramePath.lineTo(mWidth, mHeight);
    mFramePath.lineTo(mWidth, 0);
    mFramePath.close();

    mFillPath.moveTo(0, mHeight);
    mFillPath.lineTo(_fillw, mHeight);
    mFillPath.lineTo(_fillw, _fillh);
    mFillPath.close();

    canvas.drawPath(mFramePath, mFramePaint);
    canvas.drawPath(mFillPath, mFillPaint);
}

public void setLevel(int level) {
    if (this.mLevel != level) {
        this.mLevel = level;
        invalidate();
    }
}

级别被正确绘制,直到它增长,三角形区域也变大。当它开始减小时,区域不再更新,可能是因为背景没有被清除。
我还尝试了使用被注释掉的 .drawColor() 行,但没有成功。

附:我需要在此“View”上有透明的背景。

英文:

I'm writing a simple custom View that should draw the an RSSI signal shape (two overlapping triangles one filled proportionally with the level of the signal). The components works except for the clearing of canvas.

I tought I need to do something like that:

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

    final int mWidth = getMeasuredWidth();
    final int mHeight = getMeasuredHeight();
    final float _fillw = mWidth * (mLevel) / 100f;
    final float _fillh = mHeight * (100f - mLevel) / 100f;

    //canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    canvas.drawColor(Color.BLACK);

    if (mLevel &lt; mWarnLevel)
        mFillPaint.setColor(mWarnFillColor);
    else if ((mLevel &gt;= mWarnLevel) &amp;&amp; (mLevel &lt; mSafeLevel))
        mFillPaint.setColor(mFillColor);
    else if (mLevel &gt;= mSafeLevel)
        mFillPaint.setColor(mSafeFillColor);

    mFramePath.moveTo(0, mHeight);
    mFramePath.lineTo(mWidth, mHeight);
    mFramePath.lineTo(mWidth, 0);
    mFramePath.close();

    mFillPath.moveTo(0, mHeight);
    mFillPath.lineTo(_fillw, mHeight);
    mFillPath.lineTo(_fillw, _fillh);
    mFillPath.close();

    canvas.drawPath(mFramePath, mFramePaint);
    canvas.drawPath(mFillPath, mFillPaint);
}

public void setLevel(int level) {
    if (this.mLevel != level) {
        this.mLevel = level;
        invalidate();
    }
}

The level is drawn correctly until it grows and the triangle area also get larger. When it start to decrease the area is not updated anymore probably beacuse the background is not cleared.
I tried also to use the commented .drawColor() line without luck.

P.S. I need to have a transparent background on this View.

答案1

得分: 1

你只需要清除路径

public void clear()
{
    mFramePath.reset();
    mFillPath.reset();
    invalidate();
}
英文:

You just have to clear the path

public void clear()
{
    mFramePath.reset();
    mFillPath.reset();
    invalidate();
}

huangapple
  • 本文由 发表于 2020年7月29日 00:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定