在Android上集中缩放

时间:2010-02-02 15:41:26

标签: android view zooming

我设法创建了一个缩放(扩展视图),以便将PNG加载到Drawable对象中,并将其放在具有缩放和平移功能的屏幕上。请参阅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;
}
}

问题是我如何获得以屏幕为中心的缩放?目前,缩放的中心始终是PNG文件的中心...我不知道如何修改setBounds以获得此行为...任何想法? 谢谢;)

0 个答案:

没有答案