views:

574

answers:

1

Hi, I've managed to create a Zoom (extends View) in order to load a PNG into a Drawable object and have it on the screen with zooming and panning function. See the code of the View (Zoom.java) :

public class Zoom extends View {

private Drawable image;
private int zoomControler = 200;
private int shiftleft = 0;
private int shiftup = 0 ;
public float oldX = 0;
public float oldY = 0;

public Zoom(Context context) {
    super(context);
    image = context.getResources().getDrawable(R.drawable.androidmarker);
    setFocusable(true);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    image.setBounds(getWidth() / 2 - zoomControler - shiftleft,
             getHeight() / 2 - zoomControler - shiftup,
            getWidth()/ 2 + zoomControler - shiftleft,
            getHeight()/ 2 + zoomControler - shiftup);
    image.draw(canvas);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
        zoomControler += 10;
    if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
        zoomControler -= 10;
    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) 
    {

        shiftleft +=10;

    }
    if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
    {
        shiftleft-=10;
    }

    if (zoomControler < 10)
        zoomControler = 10;

    invalidate();
    return true;
}


@Override
public boolean onTouchEvent(MotionEvent e) {

    int action = e.getAction();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        oldX = e.getX();
        oldY = e.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        shiftleft+=(int) (-(e.getX() - oldX) ) / 2;
        shiftup+=(int) (-(e.getY() - oldY) / 2);
        break;
    case MotionEvent.ACTION_UP:
        break;
    case MotionEvent.ACTION_CANCEL:
        break;
    }
    invalidate();
    return true;
}
}

The problem is how I get a screen centered zoom ? For the moment, the center of the zoom is always the center of the PNG file...I don't know how to modified setBounds in order to get this behaviour... Any idea ? Thanks ;)

A: 

I think you can find a solution to your problem in the tutorial I wrote, here: http://blogs.sonyericsson.com/developerworld/2010/05/18/android-one-finger-zoom-tutorial-part-1/

Andreas Agvard