如何在Canvas后面设置CustomView的背景?

Bar*_*ock 1 android background canvas android-custom-view

我有这样的习惯View:

public class ShadowTextView extends TextView {

...

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

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        final int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        final int minSize = Math.min(parentWidth, parentHeight);

        mShadow = new Paint(Paint.ANTI_ALIAS_FLAG);

        RadialGradient gradient = new RadialGradient(
                parentWidth * mCenterX,
                parentHeight * mCenterY,
                minSize * mGradientRadiusWidthPercent,
                new int[]{mStartColor, mCenterColor, mEndColor},
                null,
                android.graphics.Shader.TileMode.CLAMP);

        mShadow.setDither(true);
        mShadow.setShader(gradient);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvas.drawRect(0, 0, getWidth(), getHeight(), mShadow);

    }

...

}
Run Code Online (Sandbox Code Playgroud)

在XML格式中,我希望将它CustomView与背景一起使用Canvas.

<com.ShadowTextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="@drawable/circle"
    android:paddingBottom="3dp"

    android:gravity="center"
    android:text="+"
    android:textColor="@android:color/white"
    android:textSize="32dp"

    app:startColor="@android:color/black"
    app:centerColor="@android:color/black"
    app:endColor="@android:color/transparent"
    app:gradientRadiusWidthPercent=".5"
/>
Run Code Online (Sandbox Code Playgroud)

circle.xml:

<layer-list>

    <item xmlns:android="http://schemas.android.com/apk/res/android"
        android:bottom="6dp"
        android:left="3dp"
        android:right="3dp">

        <shape android:shape="oval">

            <!--
                accentColor might be material red #F44336
            -->

            <solid android:color="#F44336" />

        </shape>

    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

Canvas阴影是在前台,但应该是在后台,这意味着背后android:background="@drawable/circly"和文字.

目前的结果:

错误

希望的结果:

正确

最后一个重要的注意事项:
我知道有很多开放库可以获得浮动操作按钮.请不要推荐我.我想找到我自己的"自己的"解决方案,所以设置textView样式.

Bar*_*ock 6

解决方案非常简单.XML定义' android_background' 的背景设置是在draw(...)- 而不是onDraw(...)- 方法中绘制的.

所以,我所要做的就是在draw(...)方法中绘制阴影,然后调用super.draw(...)绘制背景的方法(在我的阴影上).

此外,在该super.draw(...)方法中,onDraw(...)调用该方法来绘制文本TextView.

与上面相同的代码稍作修改:

public class ShadowTextView extends TextView {

    ...

    // overriding of the draw() method instead of the onDraw(...) method

    @Override
    public void draw(Canvas canvas) {

        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mShadow);

        /*
            Draw the background setting by XML definition android:background
         */
        super.draw(canvas);

    }

    ...

}
Run Code Online (Sandbox Code Playgroud)

感谢你的关心.