2016-01-20 79 views
1

我有3次:我不能让我的自定义SurfaceView之上Android中

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

    • 的GLSurfaceView是在底部
    • 自定义SurfaceView应位于顶部并位于顶部
    • 观是上方和底部

这是我的代码:

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“大文本”。 SurfaceView不显示。 当我取消setContentView(gameGlSurfaceView)的注释时,会显示SurfaceView。

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

非常感谢

+1

自定义视图也可以给我们,您所提供的布局XML文件的文件名?此外代码的评论'setContentView(...)' – helleye

+0

抱歉我的错误。我在我的代码示例中添加了setContentView。 – worf01

+0

语言混淆 - setZOrderOnTop()使用“顶部”,就像你使用“上面”一样。我认为。无论如何,尝试在表面上的'setZOrderOnTop()'应用于所有事物之前,因为您在相同的Z-顺序下使用两个重叠的曲面得到未定义的行为。尝试使用'canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR)'清除它(注意混合模式 - 要设置像素,而不是将它们混合到以前的内容中)。 – fadden

回答

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); 
    } 
} 
+0

非常感谢,这就是解决方案 – worf01

相关问题