Android - 如何通过 alpha masking 提高图像翻译的性能?

发布于 2024-12-25 17:29:42 字数 1290 浏览 6 评论 0原文

在每个触摸事件上调用它来渲染 alpha 掩码位图:

......

        Canvas canvas = new Canvas();
        Bitmap bleed = BitmapFactory.decodeResource(resources, R.drawable.bleed);
        Bitmap photoBG = BitmapFactory.decodeResource(resources, R.drawable.photo_bg);
        Bitmap mask = BitmapFactory.decodeResource(resources, R.drawable.mask);
        Bitmap result = Bitmap.createBitmap(bleed.getWidth(), bleed.getHeight(), Bitmap.Config.ARGB_8888);


        canvas.setBitmap(result);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawBitmap(photoBG, 0, 0, paint);
        canvas.drawBitmap(selectedImage, matrix, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        canvas.drawBitmap(mask, 0, 0, paint);
        paint.setXfermode(null);
        canvas.drawBitmap(bleed, 0, 0, paint);


        myImageView.setImageBitmap(result);
        bleed.recycle();
        mask.recycle();
        img.invalidate();

    }

我有这样的代码,我 结果图像没问题,但是当我拖动图像时,性能非常慢,我附上了解释性图像和我的应用程序屏幕截图(注意:应用程序的背景是灰色的)。

在此处输入图像描述

我应该坚持此代码(在画布上绘图)还是有更好的方法来实现我的目标?

I have this code that I call on each touch event that render an alpha masked bitmap:

...

        Canvas canvas = new Canvas();
        Bitmap bleed = BitmapFactory.decodeResource(resources, R.drawable.bleed);
        Bitmap photoBG = BitmapFactory.decodeResource(resources, R.drawable.photo_bg);
        Bitmap mask = BitmapFactory.decodeResource(resources, R.drawable.mask);
        Bitmap result = Bitmap.createBitmap(bleed.getWidth(), bleed.getHeight(), Bitmap.Config.ARGB_8888);


        canvas.setBitmap(result);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawBitmap(photoBG, 0, 0, paint);
        canvas.drawBitmap(selectedImage, matrix, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        canvas.drawBitmap(mask, 0, 0, paint);
        paint.setXfermode(null);
        canvas.drawBitmap(bleed, 0, 0, paint);


        myImageView.setImageBitmap(result);
        bleed.recycle();
        mask.recycle();
        img.invalidate();

    }

...
The result image is ok but when I drag the image around, the performance is really slow, I've attached an explanatory image and my app screen capture (Note: the background is gray at the app).

enter image description here

Should I stick to this code (drawing on a canvas) or is there a better way achieving my goal?

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

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

发布评论

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

评论(1

纸伞微斜 2025-01-01 17:29:42

每次触摸都会创建 4 个位图,这就是性能问题。这些都是非常昂贵的调用(尤其是decode*() 调用。)仅创建/加载 btimap 一次:)

You are creating 4 bitmaps on every touch, that's your performance issue. These are very expensive calls (especially the decode*() calls.) Create/load the btimaps only once :)

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