3

当图像放置在表面视图上时,下面的代码正在工作。但是当我将图像从线性布局拖到表面视图时,它不起作用。请给我解决方案,将图像从线性布局拖到实时馈送相机中。

public class PreviewDemo extends Activity 
{
 private View selected_item = null;
 private int offset_x = 0;
 private int offset_y = 0;
 private SurfaceView preview=null;
 private SurfaceHolder previewHolder=null;
 private Camera camera=null;
 private boolean inPreview=false;
 private boolean cameraConfigured=false;
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 ViewGroup vg = (ViewGroup)findViewById(R.id.overlay);
  vg.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
                    case MotionEvent.ACTION_MOVE:
                            int x = (int)event.getX() - offset_x;
                            int y = (int)event.getY() - offset_y;

    int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
    int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
    if(x > w)
        x = w;
    if(y > h)
        y = h;
                     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    new ViewGroup.MarginLayoutParams(
                                    LinearLayout.LayoutParams.WRAP_CONTENT,
                                    LinearLayout.LayoutParams.WRAP_CONTENT));
                     lp.setMargins(x, y, 0, 0);

                            selected_item.setLayoutParams(lp);
                            break;
                    default:
                            break;
            }
            return true;
            }
     });
       ImageView image= (ImageView)findViewById(R.id.icon);
    image.setOnTouchListener(new View.OnTouchListener() {

      public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
                    case MotionEvent.ACTION_DOWN:
                            offset_x = (int)event.getX();
                            offset_y = (int)event.getY();
                            selected_item = v;
                            break;
                    default:
                            break;
            }

            return false;
          }
           });
            preview=(SurfaceView)findViewById(R.id.preview);
           previewHolder=preview.getHolder();
             previewHolder.setFormat(PixelFormat.TRANSPARENT);
               previewHolder.addCallback(surfaceCallback);
           previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                }

              public void onResume()
      {
     super.onResume();
      camera=Camera.open();
        startPreview();
        }

      public void onPause() 
          {
         if (inPreview) 
          {
        camera.stopPreview();
        }
        camera.release();
         camera=null;
          inPreview=false;
        super.onPause();
        }
        private Camera.Size getBestPreviewSize(int width, int height,Camera.Parameters                               parameters)
     {
         Camera.Size result=null;
         for (Camera.Size size : parameters.getSupportedPreviewSizes()) 
         {
  if (size.width<=width && size.height<=height)
  {
    if (result==null) 
    {
      result=size;
    }
    else 
    {
      int resultArea=result.width*result.height;
      int newArea=size.width*size.height;
      if (newArea>resultArea) 
      {
        result=size;
      }
    }
  }
}
return(result);
      }
    private void initPreview(int width, int height) 
      {
      if (camera!=null && previewHolder.getSurface()!=null) 
      {
  try 
  {
    camera.setPreviewDisplay(previewHolder);
  }
  catch (Throwable t)
  {
    Log.e("PreviewDemo-surfaceCallback",
          "Exception in setPreviewDisplay()", t);
    Toast
      .makeText(PreviewDemo.this, t.getMessage(), Toast.LENGTH_LONG)
      .show();
  }

  if (!cameraConfigured) {
    Camera.Parameters parameters=camera.getParameters();
    Camera.Size size=getBestPreviewSize(width, height,
                                        parameters);

    if (size!=null) {
      parameters.setPreviewSize(size.width, size.height);
      camera.setParameters(parameters);
      cameraConfigured=true;
       }
     }
   }
  }

      private void startPreview() {
      if (cameraConfigured && camera!=null) {
       camera.startPreview();
        inPreview=true;
         }
        }

        SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
         // no-op -- wait until surfaceChanged()
         }

            public void surfaceChanged(SurfaceHolder holder,
                           int format, int width,
                           int height) {
              initPreview(width, height);
                  startPreview();
            }

             public void surfaceDestroyed(SurfaceHolder holder) {
           // no-op
           }
           };
      }

布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.view.SurfaceView
    android:id="@+id/preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >
</android.view.SurfaceView>

<LinearLayout
    android:id="@+id/overlay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#80000000"
        android:src="@drawable/cw" >
    </ImageView>
</LinearLayout>
</FrameLayout>
4

0 回答 0