Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在TextView上使用alpha掩码也会删除父视图alpha?_Android - Fatal编程技术网

Android 在TextView上使用alpha掩码也会删除父视图alpha?

Android 在TextView上使用alpha掩码也会删除父视图alpha?,android,Android,我正在尝试使用alpha线性渐变的TextView,如下所示: 我有一个几乎可以工作的实现,下面是代码: public class MyTextView extends TextView { Paint paint = new Paint(); void init() { paint.setAlpha(0xffffffff); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.O

我正在尝试使用alpha线性渐变的TextView,如下所示:

我有一个几乎可以工作的实现,下面是代码:

public class MyTextView extends TextView {

    Paint paint = new Paint();

    void init() {
        paint.setAlpha(0xffffffff);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.OVERLAY)); 
        paint.setShader(
            new LinearGradient(
                0, 0, getWidth(), getHeight(), 
                0xffffffff, 0x00000000, 
                Shader.TileMode.CLAMP));
    }

    void draw(Canvas canvas) {
        super.draw(canvas);

        canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
    }
}
它位于此布局中,具有自己的半透明背景:

<View background="#33000000">
    <MyTextView />
</View>

“绘制”模式似乎删除了其父视图的半透明背景色。我不知道如何阻止textview类中的绘图代码影响其父视图的背景色

谢谢

试试这个:

public class AlphaMaskLayout extends RelativeLayout {

    private Paint paint;

    public AlphaMaskLayout(Context context) {
        super(context);
        init(context, null, 0);
    }

    public AlphaMaskLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public AlphaMaskLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAlpha(0xffffffff);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

        setLayerType(LAYER_TYPE_SOFTWARE, null);
        setWillNotDraw(false);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        paint.setShader(
                new LinearGradient(
                        0, 0, w, h,
                        new int[]{ 0xffffffff, 0xfaffffff, 0x00000000, 0x00000000, 0xfaffffff, 0xffffffff},
                        new float[]{0, 0.15f, 0.3f, 0.7f, 0.85f, 1f},
                        Shader.TileMode.CLAMP));
    }
}
在xml中:

<your.package.AlphaMaskLayout
    android:layout_centerInParent="true"
    android:layout_width="160dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/moving_textview"
        android:layout_centerVertical="true"
        android:padding="16dp"
        android:textSize="16sp"
        android:background="@null"
        android:textColor="@android:color/white"
        android:shadowColor="@android:color/black"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="1"
        android:text="Hello World!!!"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</saulmm.coordinatorexamples.AlphaMaskLayout>