0

在我的 Android 应用程序中有一个显示相机预览的 Activity,使用此处的文档实现为 SurfaceView:http: //developer.android.com/guide/topics/media/camera.html#custom-camera

我想从一开始就在这个 SurfaceView 上显示一个 ImageView,但我还想在用户拍摄第一张照片时更新它的布局(宽度、高度和源图像)。我的问题是代码在第一步中工作,当 Activity 启动时,图像显示在 SurfaceView 上,但它的布局在我想要的时候不会改变。

这是 XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_capture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CaptureActivity" >

    <RelativeLayout
        android:id="@+id/camera_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
    >
            <!-- Here I will programmatically add the SurfaceView -->

        <ImageView
            android:id="@+id/image_picture"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_centerInParent="true"
            android:contentDescription="picture preview"
            android:src="@drawable/image1" />

    </RelativeLayout>

    <Button
      android:id="@+id/button_capture"
      android:text="Capture"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|center"
    />


</FrameLayout>

SurfaceView 与此代码段一起添加:

mPreview = new CameraPreview(this, mCamera);
RelativeLayout preview = (RelativeLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview, 0); //

这是应该更新 ImageView 的代码(它在onPictureTaken()方法内部)......但它不起作用:

LayoutInflater linf = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout frame = (FrameLayout) linf.inflate(R.layout.activity_capture, null);
ImageView imgw = (ImageView) frame.findViewById(R.id.image_left_picture); // get the ImageView

imgw.setImageResource(R.drawable.image2); // change its source image        
imgw.setLayoutParams(new RelativeLayout.LayoutParams(w, h)); //change its size

invalidate(); // also tried to add this method call... nothing changes.
4

2 回答 2

0

我认为问题是:

FrameLayout frame = (FrameLayout) linf.inflate(R.layout.activity_capture, null);

您是否已将此布局添加到您的视图组?我没有看到你的代码。您只是将其膨胀并在此布局内设置图像视图。

于 2013-11-01T08:46:53.560 回答
0

试试这个……它可能对你有帮助……

public class CameraActivity extends Activity implements PictureCallback{

protected static final String EXTRA_IMAGE_PATH = "com.blundell.tut.cameraoverlay.ui.CameraActivity.EXTRA_IMAGE_PATH";
 ImageView img;
private Camera camera;
private CameraPreview cameraPreview;
int windowwidth;
int windowheight;

@SuppressWarnings("unused")
private LayoutParams layoutParams;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);


     windowwidth = getWindowManager().getDefaultDisplay().getWidth();
     windowheight = getWindowManager().getDefaultDisplay().getHeight();
    img=(ImageView) findViewById(R.id.img);
    img.setImageResource(R.drawable.img1);


    setResult(RESULT_CANCELED);
    // Camera may be in use by another activity or the system or not available at all
    camera = getCameraInstance();
    if(cameraAvailable(camera)){
        initCameraPreview();
    } else {
        finish();
    }
}

// Show the camera view on the activity
private void initCameraPreview() {
    cameraPreview = (CameraPreview) findViewById(R.id.camera_preview);
    cameraPreview.init(camera);
    cameraPreview.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            LayoutParams layoutParams = (LayoutParams) img
                    .getLayoutParams();
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
             break;
      case MotionEvent.ACTION_MOVE:
             int x_cord = (int) event.getRawX();
             int y_cord = (int) event.getRawY();

             if (x_cord > windowwidth) {
                    x_cord = windowwidth;
             }
             if (y_cord > windowheight) {
                    y_cord = windowheight;
             }

             layoutParams.leftMargin = x_cord - 25;
             layoutParams.topMargin = y_cord - 75;

             img.setLayoutParams(layoutParams);
             break;
      default:
             break;
      }
      return true;
        }
    });
}

@FromXML
public void onCaptureClick(View button){
    // Take a picture with a callback when the photo has been created
    // Here you can add callbacks if you want to give feedback when the picture is being taken
    camera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Log.d("Picture taken");
    String path = savePictureToFileSystem(data);
    setResult(path);
    finish();
}

private static String savePictureToFileSystem(byte[] data) {
    File file = getOutputMediaFile();
    saveToFile(data, file);
    return file.getAbsolutePath();
}

private void setResult(String path) {
    Intent intent = new Intent();
    intent.putExtra(EXTRA_IMAGE_PATH, path);
    setResult(RESULT_OK, intent);
}

// ALWAYS remember to release the camera when you are finished
@Override
protected void onPause() {
    super.onPause();
    releaseCamera();
}

private void releaseCamera() {
    if(camera != null){
        camera.release();
        camera = null;
    }
}

}

和相机预览...

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private Camera camera;

private SurfaceHolder holder;

public CameraPreview(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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

public CameraPreview(Context context) {
    super(context);
}

public void init(Camera camera) {
    this.camera = camera;
    initSurfaceHolder();
}

@SuppressWarnings("deprecation") // needed for < 3.0
private void initSurfaceHolder() {
    holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    initCamera(holder);

}

private void initCamera(SurfaceHolder holder) {
    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
    } catch (Exception e) {
        Log.d("Error setting camera preview", e);
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}

}

于 2013-10-31T12:24:46.390 回答