SurfaceView透明度无法正常工作

时间:2015-07-23 22:30:54

标签: android background transparency surfaceview transparent

我已经阅读了谷歌上的每个网页,阅读了关于这个主题的10多个stackoverflow帖子,但我仍然无法正常工作。我99%在那里。我必须有一些非常简单的东西,但到目前为止,我已经花了4个小时没有运气。

我正在为我正在写作的游戏的主菜单上工作。我想在中心透明的SurfaceView中绘制一些动画,但我总是得到一个黑色的屏幕,而不是看到背景。由于SurfaceView位于LinearLayout之上,我已经使用了背景颜色和alpha设置的每种组合 - 没有任何效果。

就像stackoverflow提及的10多个主题一样,我补充说:

    mAnimView = (SurfaceView) findViewById (R.id.animationSurfaceView);
    mAnimView.setZOrderOnTop(true);    // necessary
    mAnimView.getHolder ().setFormat(PixelFormat.TRANSPARENT);
    SurfaceHolder sfhTrackHolder = mAnimView.getHolder ();
    sfhTrackHolder.addCallback (this);

在回调中我这样做:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    holder.setFormat(PixelFormat.TRANSPARENT);
}

public void surfaceCreated (SurfaceHolder holder) {
    holder.setFormat(PixelFormat.TRANSPARENT);
}

public void surfaceDestroyed (SurfaceHolder holder) {

}

在我的绘画程序中,我这样做:

Paint p = new Paint ();
p.setColor (0);
p.setStyle(Paint.Style.FILL);
c.drawColor(0,android.graphics.PorterDuff.Mode.CLEAR);

(我知道这是多余的,我变得绝望了)

嗯,奇怪的是我的SurfaceView是透明的,但仅限于某些情况。

如果我将包含SurfaceView的LinearView设置为在其下面有背景纹理(我在创建它之后在SurfaceView画布上绘制小人物),这就是我的屏幕的样子:

Texture background

如果我将Surface视图下的LinearView背景设置为红色,那么这就是我的屏幕:

Color background

新数据点!如果我将根LinearLayout的背景设置为单一颜色,那么它也可以。我开始认为它与该背景有关吗?

red root background

如果我将SurfaceView下的LinearView背景设置为透明,那么这就是我的屏幕。除了SurfaceView之外,为什么我在窗口的每个部分都看到根LinearView的背景?完全被这个混淆了。

enter image description here

这是我的XML文件:

<LinearLayout
    android:background="@drawable/main_menu_background"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="top"
    android:id="@+id/mainSectionMenu"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="LUNA PUMA"
        android:id="@+id/titleText"
        android:layout_gravity="center"
        android:textColor="#ff6495ed"
        android:autoText="false"
        android:textSize="50dp"
        android:layout_weight=".3"
        android:gravity="center"
        android:background="@android:color/transparent"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight=".5"
        android:background="@android:color/transparent"
        >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight=".25"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="0px"
                android:text="Easy"
                android:id="@+id/easyButton"
                android:layout_gravity="center"
                android:layout_weight=".5"
                android:background="@android:color/transparent"
                android:textColor="#ff6495ed"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="0px"
                android:text="Medium"
                android:id="@+id/mediumButton"
                android:layout_gravity="center"
                android:layout_weight=".5"
                android:textColor="#ff6495ed"
                android:background="@android:color/transparent"/>
        </LinearLayout>

        <SurfaceView
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:id="@+id/animationSurfaceView"
            android:layout_weight=".5"
            />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight=".25">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="0px"
                android:text="Hard"
                android:id="@+id/hardButton"
                android:layout_gravity="center"
                android:layout_weight=".5"
                android:textColor="#ff6495ed"
                android:background="@android:color/transparent"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="0px"
                android:text="More ..."
                android:id="@+id/moreButton"
                android:layout_gravity="center"
                android:layout_weight=".5"
                android:background="@android:color/transparent"
                android:textColor="#ff6495ed"
                android:alpha="1"
                android:clickable="false"/>
        </LinearLayout>
    </LinearLayout>



    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight=".2">
    </FrameLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

事实证明这非常简单!

我做的一切都正确。它得到了适当的实施。

我忘记了为了早期测试宇航员的间距和尺寸,我从背景纹理中剪下了一个黑色正方形,恰好是表面视图的大小相同!!!!

我一直在寻找正确的纹理。

感叹!

答案 1 :(得分:0)

我不知道在xml中直接使用它的方法,但我相信除了将透明度应用于包含的LinearLayout之外,您还需要将透明度应用于SurfaceView。在您的活动中执行此操作:

SurfaceView surface = (SurfaceView)findViewById(R.id.your_surface_view);
surface.setZOrderOnTop(true);
SurfaceHolder surfaceHolder = surface.getHolder();
surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
相关问题