tags:

views:

32

answers:

1

I want to make the view move around the screen. Is that possible?

in other words, I want panning to be possible and I think that has something to do with the view.

How do you do Panning a video preview?

+1  A: 

If you want to move your view all over the screen, its possible. Presuming this is indeed your requirement, here's what you could do. Make the view a child of Relative Layout. Everytime you want to move the view, get the RelativeLayout.LayoutParams of the child view, change relevent margins and set this as the child view's LayoutParam.

If you are doing this to a SurfaceView (needed to play the video), you get surfaceChanged callback everytime you change the margin.

Here's a sample code of the tweak I did for API Demos' CameraPreview activity which does the same. The SurfaceView is moved from left to right. Hope this helps.

Regards, Anirudh.

public class CameraPreview extends Activity {
protected static final String TAG = "CameraPreview";
private Preview mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Hide the window title.
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Create our Preview view and set it as the content of our activity.
    mPreview = new Preview(this);
    mPreview.setId(100);
    RelativeLayout mainLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams mainLp = new RelativeLayout.LayoutParams(640, 480);
    mainLp.leftMargin = 20;

    mainLayout.addView(mPreview, mainLp);

    Button btn = new Button(this);
    btn.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            RelativeLayout.LayoutParams nLp = (LayoutParams) mPreview.getLayoutParams();
            nLp.leftMargin += 10;
            Log.v(TAG,"nLp.leftMargin: " + nLp.leftMargin);
            mPreview.setLayoutParams(nLp);
        }

    });
    btn.setText("Click me!");

    RelativeLayout.LayoutParams btnLp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    btnLp.addRule(RelativeLayout.BELOW, mPreview.getId());

    mainLayout.addView(btn ,btnLp);
    setContentView(mainLayout);
}

}

aniait
I'm unable to see the button on the screen. I did exactly as above and I can't test the movement without the button because that code is in onClick()
Namratha
Does the surface view shift to the right of the screen maintaining the captured picture or does the moving of the surface view cut the picture? I want the latter
Namratha
You could reduce the preview size. I checked this in relatively large screen device. You can simulate the actions of the button in other ways (onTouch event, timer etc). As the view moves out of the purview of the screen of the device, you would see a cropped view.
aniait
Yeah I'm trying Handler and sending a message. So by changing the margin of SurfaceView, we're actually making the user see less than what the camera captures, right?
Namratha
Yes, it is just like any other view going out of visible screen display area.
aniait
Say you have a tree and a house in front of you(tree on your left and house on your right). If we move the SurfaceView to the right, the tree should go out of focus but that is not happening. Instead we see a constricted image of both the tree and the house. I want to make the tree go out of focus as the SurfaceView moves right. How do you do that?
Namratha
I want to use the above method to have some buttons below the SurfaceView. But I tried this example and can't see the buttons. Any suggestions?
Namratha