【问题标题】:Can't update an ImageView on top of a (camera) SurfaceView无法在(相机)SurfaceView 上更新 ImageView
【发布时间】:2013-10-31 12:02:50
【问题描述】:

在我的 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 是用这个 sn-p 添加的:

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.

【问题讨论】:

    标签: android imageview android-camera surfaceview


    【解决方案1】:

    我认为问题是:

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

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

    【讨论】:

    • 已解决,问题与此有关!事实上,应该改变 ImageView 布局的方法是在 SurfaceView 内部,所以我需要用那个可能是错误的技巧来检索 ImageView。现在我让父 Activity 修改 ImageView,而不使用充气器,它可以工作。谢谢
    【解决方案2】:

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

    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) {
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多