有没有更好的方法来制作可拖动的背景?

发布于 2024-11-29 20:40:48 字数 1156 浏览 1 评论 0原文

我正在开始一个新项目,到目前为止,我已经制作了一个简单的应用程序,其中有一个绘制在 Canvas 上的图像,您可以用手指在屏幕上拖动该图像。这个想法是在游戏的 Canvas 上绘制大量内容。然而,拖动速度非常缓慢,用手指拖动时,屏幕可能每秒更新图像 2-3 次。它的速度非常慢,所以我假设我做错了(我使用的是 Nexus S)。

我只有应用程序的基本框架,只有一个 SurfaceView,其中包含一个线程,用于使用这一张图像更新 Canvas。这是代码:

// In GameThread

private void doDraw(Canvas c) {
    if(canvasInit){
        Bitmap background = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bg);
        c.drawBitmap(background, offsetX, offsetY, null);           
    }
}


public boolean doTouchDown(MotionEvent event) {
    if(drag == false){
        //initialize drag
        startX = event.getX();
        startY = event.getY();
        drag = true;
    }else{
        //caluclate new position
        offsetX = Math.max(Math.min(0, offsetX + (event.getX() - startX)), mCanvasWidth - BACKGROUND_WIDTH); 
        offsetY = Math.max(Math.min(0, offsetY + (event.getY() - startY)), mCanvasHeight- BACKGROUND_HEIGHT);
        startX = event.getX();
        startY = event.getY();
    }
    return true;
}

public boolean doTouchUp(MotionEvent event){
    drag = false;
    return true;
}

I'm starting out a new project, and thus far I've made a simple app that has an image which is drawn onto a Canvas, which you can drag around the screen with your finger. The idea is to draw lots of stuff onto this Canvas for a game. However the dragging is extremely laggy, while dragging with my finger the screen updates the image maybe 2-3 times a second. It's very noticeably slow, so I'm assuming I'm doing it very wrong (I'm on a Nexus S).

I only have the very barebones of the app, just a SurfaceView containing a thread to update the Canvas with this one image. Here's the code:

// In GameThread

private void doDraw(Canvas c) {
    if(canvasInit){
        Bitmap background = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bg);
        c.drawBitmap(background, offsetX, offsetY, null);           
    }
}


public boolean doTouchDown(MotionEvent event) {
    if(drag == false){
        //initialize drag
        startX = event.getX();
        startY = event.getY();
        drag = true;
    }else{
        //caluclate new position
        offsetX = Math.max(Math.min(0, offsetX + (event.getX() - startX)), mCanvasWidth - BACKGROUND_WIDTH); 
        offsetY = Math.max(Math.min(0, offsetY + (event.getY() - startY)), mCanvasHeight- BACKGROUND_HEIGHT);
        startX = event.getX();
        startY = event.getY();
    }
    return true;
}

public boolean doTouchUp(MotionEvent event){
    drag = false;
    return true;
}

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

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

发布评论

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

评论(2

月棠 2024-12-06 20:40:48

您似乎每次绘制时都会加载位图。您可以尝试将资源加载移动到某种初始化方法/ SurfaceView 构造函数并比较性能?

Bitmap background;
...

private void init() {
  ...
  background = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bg);
  ...
}

private void doDraw(Canvas c) {
    if(canvasInit){
        c.drawBitmap(background, offsetX, offsetY, null);           
    }
}

You seem to be loading the Bitmap every time you draw. You can try moving the resource loading to some kind of initialisation method/ SurfaceView constructor and compare the performance?

Bitmap background;
...

private void init() {
  ...
  background = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bg);
  ...
}

private void doDraw(Canvas c) {
    if(canvasInit){
        c.drawBitmap(background, offsetX, offsetY, null);           
    }
}
紫竹語嫣☆ 2024-12-06 20:40:48

我认为问题出在doDraw方法中,您每次Draw创建一个新的位图,尝试在构造函数中加载此Bitmap code> 然后在 doDraw 方法中获取它的引用,并使用您的 x 和 y 绘制它。

I Think the problem is in doDraw method, your creating a new Bitmap every Draw, try to load this Bitmap in Constructor then get it reference in doDraw method, and draw it using your x and y.

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