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

时间:2015-05-09 09:10:06

标签: android canvas background android-custom-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); } ... }

CustomView

在XML中,我想使用此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" />

<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>

circle.xml:

Canvas

android:background="@drawable/circly"影子位于前景,但应该在后台,这意味着在 Connecté 2015-05-09 11:08:38.541 MYAPPNAME[39279:1718140] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSLayoutConstraint:0x7f919d91c4a0 H:[UIImageView:0x7f919d91bef0(359)]>", "<NSLayoutConstraint:0x7f919d922850 H:[UIImageView:0x7f919d91bef0]-(0)-| (Names: '|':UIView:0x7f919d920da0 )>", "<NSLayoutConstraint:0x7f919d9228a0 H:|-(0)-[UIImageView:0x7f919d91bef0] (Names: '|':UIView:0x7f919d920da0 )>", "<NSLayoutConstraint:0x7f919d82aa50 UIView:0x7f919d920da0.trailing == UITableViewCellContentView:0x7f919d90ca50.trailingMargin>", "<NSLayoutConstraint:0x7f919d829970 UIView:0x7f919d920da0.leading == UITableViewCellContentView:0x7f919d90ca50.leadingMargin>", "<NSLayoutConstraint:0x7f919d834d60 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7f919d90ca50(412)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f919d91c4a0 H:[UIImageView:0x7f919d91bef0(359)]> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-05-09 11:08:38.544 MYAPPNAME[39279:1718140] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSLayoutConstraint:0x7f919d827c60 H:[UIImageView:0x7f919d832410(359)]>", "<NSLayoutConstraint:0x7f919d83f180 H:[UIImageView:0x7f919d832410]-(0)-| (Names: '|':UIView:0x7f919d835ba0 )>", "<NSLayoutConstraint:0x7f919d842b60 H:|-(0)-[UIImageView:0x7f919d832410] (Names: '|':UIView:0x7f919d835ba0 )>", "<NSLayoutConstraint:0x7f919d835b30 UIView:0x7f919d835ba0.trailing == UITableViewCellContentView:0x7f919d8422d0.trailingMargin>", "<NSLayoutConstraint:0x7f919d843bb0 UIView:0x7f919d835ba0.leading == UITableViewCellContentView:0x7f919d8422d0.leadingMargin>", "<NSLayoutConstraint:0x7f919d83afa0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7f919d8422d0(412)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f919d827c60 H:[UIImageView:0x7f919d832410(359)]> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 2015-05-09 11:08:38.548 MYAPPNAME[39279:1718140] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSLayoutConstraint:0x7f919d82bee0 H:[UIImageView:0x7f919d82e780(359)]>", "<NSLayoutConstraint:0x7f919d83e1b0 H:[UIImageView:0x7f919d82e780]-(0)-| (Names: '|':UIView:0x7f919d839410 )>", "<NSLayoutConstraint:0x7f919d83e200 H:|-(0)-[UIImageView:0x7f919d82e780] (Names: '|':UIView:0x7f919d839410 )>", "<NSLayoutConstraint:0x7f919d810470 UIView:0x7f919d839410.trailing == UITableViewCellContentView:0x7f919d8300d0.trailingMargin>", "<NSLayoutConstraint:0x7f919d8104c0 UIView:0x7f919d839410.leading == UITableViewCellContentView:0x7f919d8300d0.leadingMargin>", "<NSLayoutConstraint:0x7f919d84cfd0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7f919d8300d0(412)]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7f919d82bee0 H:[UIImageView:0x7f919d82e780(359)]> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 和文字后面。

目前的结果:

wrong

希望的结果:

correct

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

1 个答案:

答案 0 :(得分:5)

解决方案非常简单。 XML定义的背景设置&#39; android_background&#39;是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);

    }

    ...

}

感谢您的关注。