Android 上的居中缩放

发布于 2024-08-19 20:22:37 字数 1858 浏览 3 评论 0原文

我成功地创建了一个缩放(扩展视图),以便将 PNG 加载到可绘制对象中,并通过缩放和平移功能将其显示在屏幕上。请参阅视图的代码(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 以获得此行为...知道吗? 谢谢 ;)

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 ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文