2

I am overriding the SurfaceHolder like this and placing an image inside it.

public class TestCameraActivity extends Activity implements SurfaceHolder.Callback

I want to get the x and y position of the this image. How to get it? Please help.

This is the code:

public class TestCameraActivity extends Activity implements SurfaceHolder.Callback {

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
ImageView image;
Button b;
View viewControl;
RelativeLayout rel;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    getWindow().setFormat(PixelFormat.UNKNOWN);  
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    viewControl = controlInflater.inflate(R.layout.control, null);

    image =(ImageView) viewControl.findViewById(R.id.img);

    LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, 
    LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);  

sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); boolean accelSupported = sensorMgr.registerListener(this, SENSOR_ACCELEROMETER, SENSOR_DELAY_UI); }

@Override
    public void onSensorChanged(int sensor, float[] values) {
        float x = values[0];
        float y = values[1];
        float z = values[2];    
        System.out.println("x = " + x + ", y = " + y + ",z = " + z);

        Rect r = image.getDrawable().getBounds();

        int drawLeft = r.left;
        int drawTop = r.top;
        int drawRight = r.right;
        int drawBottom = r.bottom;

        r.left = (int) x;
        r.right =(int) y;


    }
James
  • 13,891
  • 26
  • 68
  • 93

1 Answers1

0

As per I understand your question,

You should be able to get the (x, y) coordinates of the drawable inside, if that's what you're looking for. Try this:

ImageView img = (ImageView)findViewById(R.id.img);
Rect r = img.getDrawable().getBounds();

int drawLeft = r.left;
int drawTop = r.top;
int drawRight = r.right;
int drawBottom = r.bottom;

drawLeft is your X value.

drawTop is your Y value.

EDIT: Look at this question Moving an image using Accelerometer of android and also this tutorial Moving Images on the Screen with Android.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I tried this @Override public void onSensorChanged(int sensor, float[] values) { float x = values[0]; float y = values[1]; float z = values[2]; System.out.println("x = " + x + ", y = " + y + ",z = " + z); Rect r = image.getDrawable().getBounds(); int drawLeft = r.left; int drawTop = r.top; int drawRight = r.right; int drawBottom = r.bottom; r.left = (int) x; r.right =(int) y; } but image is not moving. please help. please check my edit – James Dec 05 '11 at 08:45