Android - 实现 AsyncTask 或 Thread 的正确设计

发布于 2024-11-24 02:06:15 字数 1363 浏览 0 评论 0原文

我有一个 getViewBitmap() 方法,它使用第三方库(专有的)创建位图,如下所示:

public Bitmap getViewBitmap(int index) {
    Bitmap retBitmap = null;

    int width = 400;
    int height = 200;

    try {
        retBitmap = lib.createBitmap(width, height, index);
    } catch(BMException e) {
        e.printStacktrace();
    }

    return retBitmap;
}

此方法用于在另一种方法中创建两个页面视图位图:

public Bitmap getTwoPageBitmap(int firstPageIndex, intSecondPageIndex) {
    Bitmap first = getViewBitmap(firstPageIndex);
    Bitmap second = getViewBitmap(secondPageIndex);

    Bitmap retBitmap = Bitmap.create(800, 400, first.getConfig());

    Canvas helperCanvas = new Canvas(splitViewBm);
    helperCanvas.drawBitmap(leftPageBitmap, 0, 0, null);
    helperCanvas.drawBitmap(rightPageBitmap, leftPageBitmap.getWidth(), 0, null);

    return retBitmap;
}

最后在启动方法中,我有这个:

public View createView() {
    MyView v = new MyView();
    if(pagePortratit) {
        v.setPageView(getViewBitmap(0));
    } else {
        // if page is landscape
        v.setPageView(getTwoPageBitmap(0, 1));
    }

    return v;
}

现在 - 我想让 getViewBitmap(int) 方法异步。由于“lib.createBitmap(int, int, int)”非常慢并且会阻塞 UI,因此我希望在另一个线程中创建位图 (getViewBitmap(int))中断其工作的可能性。

这种设计的正确设计是什么,使得实际上很重的方法变成异步的?

I have a getViewBitmap() method that creates a Bitmap with a 3rd party library (which is proprietary) and goes like this:

public Bitmap getViewBitmap(int index) {
    Bitmap retBitmap = null;

    int width = 400;
    int height = 200;

    try {
        retBitmap = lib.createBitmap(width, height, index);
    } catch(BMException e) {
        e.printStacktrace();
    }

    return retBitmap;
}

This method is used for creating two page view bitmap in another method:

public Bitmap getTwoPageBitmap(int firstPageIndex, intSecondPageIndex) {
    Bitmap first = getViewBitmap(firstPageIndex);
    Bitmap second = getViewBitmap(secondPageIndex);

    Bitmap retBitmap = Bitmap.create(800, 400, first.getConfig());

    Canvas helperCanvas = new Canvas(splitViewBm);
    helperCanvas.drawBitmap(leftPageBitmap, 0, 0, null);
    helperCanvas.drawBitmap(rightPageBitmap, leftPageBitmap.getWidth(), 0, null);

    return retBitmap;
}

And then finally in initiated method, I have this:

public View createView() {
    MyView v = new MyView();
    if(pagePortratit) {
        v.setPageView(getViewBitmap(0));
    } else {
        // if page is landscape
        v.setPageView(getTwoPageBitmap(0, 1));
    }

    return v;
}

Now - I wanna make the getViewBitmap(int) method Asynchronous. Since the "lib.createBitmap(int, int, int)" is pretty slow and it blocks the UI, I want the creation of the bitmap (getViewBitmap(int)) to be in another thread, with possibility to interrupt it's work.

What is the correct design for such design so that the method that is actually heavy goes async?

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

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

发布评论

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

评论(2

緦唸λ蓇 2024-12-01 02:06:15

您可能想要子类 AsyncTask阅读此处) 并将您的 getBitmapView 代码放入 doInBackground() 方法 (@Override)。完成后,让 onPostExecute() 方法更新视图/UI。确定横向或纵向的逻辑将希望位于 AsyncTask 之外,并且您只需将需要的视图分配给任务(使用 .execute())即可。

这可能是一种方法。

You likely want to subclass AsyncTask (read here) and put your getBitmapView code in the doInBackground() method (@Override). When it's done, have the onPostExecute() method update the View/UI. The logic for determining landscape or portrait will want to be outside the AsyncTask and you'll just want to farm out to the task (using .execute()) which ever view is needed.

That might be one approach.

滥情空心 2024-12-01 02:06:15

我认为你能做的最好的事情就是使用 AysncTask 它允许u 直接更新UI,无需对UI线程更新进行特殊处理。

I think the best thing that you can do is to use AysncTask which allows u to update the UI Directly without special handling for UI Thread update.

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