将外部视图内容绘制到内部画布中,同时调暗它?

时间:2015-03-26 10:00:13

标签: android canvas view

我正在自定义视图中绘制另一个视图的内容,如下所示:

@Override
public void draw(final Canvas canvas){
    // Draw lower filler
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.bottom - mStartBounds.bottom);
    canvas.clipRect(0, mStartBounds.bottom, canvas.getWidth(), canvas.getHeight());
    mOuterView.draw(canvas);
    canvas.restore();

    // Draw upper filler
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.top - mStartBounds.top);
    canvas.clipRect(0, 0, canvas.getWidth(), mStartBounds.top);
    mOuterView.draw(canvas);
    canvas.restore();

    // Draw content
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.top);
    canvas.clipRect(0, 0, mCurrentExpansionRect.right, mCurrentExpansionRect.height());
    super.draw(canvas);
    canvas.restore();
}

mOuterView是提供背景并在我的自定义视图的顶部和底部绘制的视图(这些背景称为上部和下部“填充”)

我想调暗上下填充物,但我不能修改它的颜色。

我可以在修改输出时在自定义视图中绘制另一个视图的内容,比如调暗它吗?

我想避免在已经绘制的上下填充物上方画一个调光层,以避免过度拉伸。

编辑: 最终结果的视频:https://www.youtube.com/watch?v=72g7LQ0vdug 展开视图上方和下方的行是上部和下部填充,并且在行展开时应逐渐变暗。

1 个答案:

答案 0 :(得分:0)

一种方法是在填充物上绘制渐变,这会产生我想要避免的额外透支,但似乎是唯一的方法

@Override
public void draw(final Canvas canvas){
    // Draw lower filler
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.bottom - mStartBounds.bottom);
    canvas.clipRect(0, mStartBounds.bottom, canvas.getWidth(), canvas.getHeight());
    mOuterView.draw(canvas);
    canvas.restore();

    // Draw lower filler shadow
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.bottom);
    mFillerShadowPaint.setShader(mLowerFillerShadow);
    mFillerShadowPaint.setAlpha((int) (mAnimatedFraction * 255));
    canvas.drawPaint(mFillerShadowPaint);
    canvas.restore();

    // Draw upper filler
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.top - mStartBounds.top);
    canvas.clipRect(0, 0, canvas.getWidth(), mStartBounds.top);
    mOuterView.draw(canvas);
    canvas.restore();

    // Draw upper filler shadow
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.top - mTopDistance);
    mFillerShadowPaint.setShader(mUpperFillerShadow);
    mFillerShadowPaint.setAlpha((int) (mAnimatedFraction * 255));
    canvas.drawPaint(mFillerShadowPaint);
    canvas.restore();

    // Draw content
    canvas.save();
    canvas.translate(0, mCurrentExpansionRect.top);
    canvas.clipRect(0, 0, mCurrentExpansionRect.right, mCurrentExpansionRect.height());
    super.draw(canvas);
    canvas.restore();
}