如何用两根手指滚动位图?

发布于 2024-11-30 16:01:44 字数 77 浏览 0 评论 0原文

谁能告诉我如何用两根手指滚动位图,因为在我的应用程序中我用一根手指来绘画。由于我想绘制整个图像(它可能比屏幕大),用两根手指滚动会很方便吗?

Can anybody tell me how to scroll a bitmap with two fingers, because in my app I am using one finger to paint. Since I want to paint an entire image (it may be larger than the screen) scrolling with two fingers would be convenient?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-12-07 16:01:44
private class Img extends View {
    private Bitmap bmp;
    private Rect imgRect, scrollRect;
    private float prevX, prevY;
    private int newX, newY;

    public Img(Context context) {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.screen);
        imgRect = new Rect(0, 0, width, height);
        scrollRect = new Rect(0, 0, width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE: {
            int numPointers = event.getPointerCount();
            if (numPointers > 1) {
                float currX = event.getRawX();
                float deltaX = -(currX - prevX);
                newX += deltaX;
                float currY = event.getRawY();
                float deltaY = -(currY - prevY);
                newY += deltaY;
                if (newX < 0)
                    newX = 0;
                if (newY < 0)
                    newY = 0;
                if (newX > bmp.getWidth() - width)
                    newX = bmp.getWidth() - width;
                if (newY > bmp.getHeight() - height)
                    newY = bmp.getHeight() - height;
                scrollRect.set(newX, newY, newX + width, newY + height);
                invalidate();
                prevX = currX;
                prevY = currY;
            }
            break;
        }
        case MotionEvent.ACTION_DOWN: {
            prevX = event.getRawX();
            prevY = event.getRawY();
            break;
        }
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bmp, scrollRect, imgRect, new Paint());
    }
}

这里Img类代表带有大位图的View,它显示其当前可见的矩形。 int numPointers = event.getPointerCount();
if (numPointers > 1) { - 在这里你可以得到触摸屏幕的手指数量。因此,如果它大于 1,则大图像将滚动,否则,您可以实现您的绘制逻辑。

private class Img extends View {
    private Bitmap bmp;
    private Rect imgRect, scrollRect;
    private float prevX, prevY;
    private int newX, newY;

    public Img(Context context) {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.screen);
        imgRect = new Rect(0, 0, width, height);
        scrollRect = new Rect(0, 0, width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE: {
            int numPointers = event.getPointerCount();
            if (numPointers > 1) {
                float currX = event.getRawX();
                float deltaX = -(currX - prevX);
                newX += deltaX;
                float currY = event.getRawY();
                float deltaY = -(currY - prevY);
                newY += deltaY;
                if (newX < 0)
                    newX = 0;
                if (newY < 0)
                    newY = 0;
                if (newX > bmp.getWidth() - width)
                    newX = bmp.getWidth() - width;
                if (newY > bmp.getHeight() - height)
                    newY = bmp.getHeight() - height;
                scrollRect.set(newX, newY, newX + width, newY + height);
                invalidate();
                prevX = currX;
                prevY = currY;
            }
            break;
        }
        case MotionEvent.ACTION_DOWN: {
            prevX = event.getRawX();
            prevY = event.getRawY();
            break;
        }
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bmp, scrollRect, imgRect, new Paint());
    }
}

Here Img class represents the View with a large bitmap, which displays its currently visible rectangle. int numPointers = event.getPointerCount();
if (numPointers > 1) {
- and here you get number of fingers, that touch the screen. So if it is greater than 1, large image will be scrolled, otherwise, you can implement your paint logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文