【问题标题】:How to set a Background of a CustomView behind the Canvas?如何在 Canvas 后面设置 CustomView 的背景?
【发布时间】:2015-05-09 09:10:05
【问题描述】:

我有一个这样的自定义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);

    }

...

}

在 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"
/>

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>

Canvas 阴影在前景中,但应该在背景中,这意味着在 android:background="@drawable/circly" 和文本后面。

当前结果:

希望的结果:

最后一个重要提示:
我知道有很多开放的库可以获得浮动操作按钮。请不要转介我。我想找到我的“自己的”解决方案,以便为 textView 设置样式。

【问题讨论】:

  • "android:background" 是(顾名思义)放在背景上的,但是你可以通过调用 getBackground() 来获取背景 Drawable

标签: android canvas background android-custom-view


【解决方案1】:

解决方案非常简单。 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);

    }

    ...

}

感谢您的关心。

【讨论】:

  • 笨拙但可能的陷阱:不要在构造函数中调用“setBackground”:/
猜你喜欢
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 2020-12-14
  • 2023-03-25
  • 1970-01-01
  • 2015-07-22
  • 2018-09-08
  • 2014-04-08
相关资源
最近更新 更多