在android中获取实际像素

时间:2011-12-05 05:34:07

标签: android

我正在覆盖SurfaceHolder并在其中放置一个图像。

public class TestCameraActivity extends Activity implements SurfaceHolder.Callback

我希望获得此图片的 x y 位置。怎么弄? 请帮忙。

这是代码:

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;


    }

1 个答案:

答案 0 :(得分:0)

据我了解你的问题,

你应该能够获取可绘制的(x,y)坐标,如果这就是你要找的东西。试试这个:

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 是您的X值。

drawTop 是您的Y值。

编辑:请查看此问题Moving an image using Accelerometer of android以及本教程Moving Images on the Screen with Android