我想在显示器上画一条线(相机预览)。我想在相机屏幕上显示相机时画一条线(就像油漆一样)。
答案 0 :(得分:0)
您可以使用类似下图的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.YourTextureViewForCameraDisplay
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.example.YourOverlayLayout - your overlay where your draw a line, or a regular layout (like a FrameLayout) where you put a drawable with a line
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
答案 1 :(得分:0)
以下代码段会帮助您。
public class SurfaceViewLineDemo extends Activity implements SurfaceHolder.Callback {
private static final String TAG = "TestSurface";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceView view = new SurfaceView(this);
setContentView(view);
view.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
tryDrawing(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h) {
tryDrawing(holder);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
private void tryDrawing(SurfaceHolder holder) {
Log.i(TAG, "Trying to draw...");
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
drawMyStuff(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void drawMyStuff(final Canvas canvas) {
Random random = new Random();
Log.i(TAG, "Drawing...");
canvas.drawRGB(255, 128, 128);
} }