在SurfaceView上绘制文本

时间:2012-07-13 13:11:53

标签: android

我正在尝试在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);
    }
}

1 个答案:

答案 0 :(得分:-2)

您可以在其自身的父视图上添加文本视图,以创建动态文本视图并将其添加到父版式上。在这里你需要获得框架布局的参考,你可以添加文本视图......

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