【发布时间】: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