2012-07-13 290 views
0

我試圖在SurfaceView上繪製文本,但沒有任何東西可見。在SurfaceView上繪製文本

當我試圖獲得View的寬度和高度時,雖然它應該填充父元素,但返回0。

camerapreview.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
    <SurfaceView 
      android:id="@+id/svCamera" 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent"/> 

    <FrameLayout 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent"> 
     <View 
       android:id="@+id/vOverlays" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent"/> 
    </FrameLayout> 
</FrameLayout> 

CameraActivity.java

public class CameraActivity extends Activity { 
    private Camera camera; 
    private CameraPreview cameraPreview; 
    private View vOverlays; 
    private SurfaceHolder svHolder; 
    private SurfaceView svCamera; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.camerapreview); 

     svCamera = (SurfaceView) findViewById(R.id.svCamera); 
     vOverlays = findViewById(R.id.vOverlays); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     camera = Camera.open(); 

     svHolder = svCamera.getHolder(); 
     cameraPreview = new CameraPreview(svHolder, camera); 
     svHolder.addCallback(cameraPreview); 
     svHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     Canvas canvas = new Canvas(); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(10); 

     canvas.drawText("Testext", vOverlays.getWidth()/2, vOverlays.getHeight()/2, paint); 
     vOverlays.draw(canvas); 
    } 
} 

回答

-2

您可以在父視圖它的自我添加文本視圖,而且創建一個動態文本視圖和添加它你的父母佈局。在這裏你需要得到框架佈局的參考,你可以添加你的文本視圖...

frame_layout.addView(text_view); 
text_view.bringToFront(); 
+0

這與繪製文本到surfaceview上沒有任何關係。 – RichieHH 2014-08-05 13:20:57