我无法在Android

时间:2016-01-20 19:36:35

标签: android android-layout surfaceview

我有3个观点:

  1. GLSurfaceView
  2. 自定义SurfaceView
  3. 查看

    • GLSurfaceView位于底部
    • 自定义SurfaceView应位于顶部
    • 上方
    • 视图位于上方和底部
  4. 这是我的代码:

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    
    public class MainActivity extends AppCompatActivity {
    
        GameGlSurfaceView gameGlSurfaceView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            gameGlSurfaceView = new GameGlSurfaceView(this);
            setContentView(gameGlSurfaceView);
    
            View hudView = getLayoutInflater().inflate(R.layout.activity_main, null);
            addContentView(hudView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
    
            View inpudPadView = getLayoutInflater().inflate(R.layout.input_pad, null);
            LinearLayout.LayoutParams inpudPadViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            inpudPadViewParams.gravity = Gravity.TOP;
            addContentView(inpudPadView, inpudPadViewParams);
        }
    }
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.util.AttributeSet;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class SurfacePanel extends SurfaceView implements SurfaceHolder.Callback {
    
        Context context;
        Bitmap bitmapTest;
        MyThread mythread;
    
        public SurfacePanel(Context ctx, AttributeSet attrSet   ) {
            super(ctx, attrSet);
    
            context = ctx;
    
            bitmapTest = Bitmap.createBitmap(200, 100, Bitmap.Config.ARGB_8888);
            bitmapTest.eraseColor(Color.TRANSPARENT);
    
            SurfaceHolder holder = getHolder();
            holder.addCallback(this);
        }
    
        void doDraw(Canvas canvas) {
            canvas.drawColor(Color.GREEN);
            canvas.drawBitmap(bitmapTest, 50, 10, null);
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mythread = new MyThread(holder, context,this);
            mythread.setRunning(true);
            mythread.start();
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            mythread.setRunning(false);
            boolean retry = true;
    
            while(retry) {
    
                try {
                    mythread.join();
                    retry = false;
                }
    
                catch(Exception e) {
                }
            }
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="de.batechniks.surfaceviewtest.MainActivity">
    
        <de.batechniks.surfaceviewtest.SurfacePanel
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView"/>
    
    </RelativeLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:baselineAligned="false"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:gravity="left">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bt_down"
                android:src="@mipmap/ic_launcher"
                android:layout_gravity="bottom"/>
    
    </LinearLayout>
    

    结果是:我可以看到GLSurfaceView,我可以在底部看到上面的视图。 在顶部,我只看到textview&#34; Large Text&#34;。 SurfaceView未显示。 当我取消注释setContentView(gameGlSurfaceView)时,会显示SurfaceView。

    主要问题是显示SurfaceView,第二个问题是将SurfaceView透明化。

    非常感谢

1 个答案:

答案 0 :(得分:1)

您需要调用方法

this.setZOrderOnTop(true);

在你的构造函数中。

建议在像这样的自定义视图中执行构造函数

public SurfacePanel (Context context) {
    super(context);
    init();
}

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

public SurfacePanel (Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

@TargetApi(21)
public SurfacePanel (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

private void init() {
    if(!isInEditMode()) {
        this.setZOrderOnTop(true);
    }
}