Android - 在自定义视图中滚动位图

发布于 2024-11-06 01:44:11 字数 533 浏览 0 评论 0原文

我有一个尺寸为 400 x 70 像素的位图。我想将此位图向上滚动一行并用新像素填充第 70 行。 我尝试通过将位图复制到 IntBuffer 来滚动位图,将其复制到另一个 IntBuffer (从第二行开始),然后将其写回位图。 这可行,但需要大量的 CPU 能力,而且速度有点慢。 有没有更快的方法来做到这一点?

private Bitmap shift(Bitmap bitmap)
{
    buffer.rewind();
    buffer1.rewind();
    bitmap.copyPixelsToBuffer(buffer);
    buffer.position(bitmap.getWidth());
    buffer1.put(buffer.array(), bitmap.getWidth()*2, bitmap.getWidth() * bitmap.getHeight());
    buffer1.rewind();
    bitmap.copyPixelsFromBuffer(buffer1);

    return bitmap;
}

I have a bitmap with the size of 400 x 70 pixels. I would like to scroll this bitmap up by one row and fill up row 70 with new pixels.
I have tried to scroll the bitmap by copying it to an IntBuffer , copy it to another IntBuffer (starting at the second row) and the then write it back to the bitmap.
This works but it takes a lot of CPU power and it is kind of slow.
Is there any faster way to do this?

private Bitmap shift(Bitmap bitmap)
{
    buffer.rewind();
    buffer1.rewind();
    bitmap.copyPixelsToBuffer(buffer);
    buffer.position(bitmap.getWidth());
    buffer1.put(buffer.array(), bitmap.getWidth()*2, bitmap.getWidth() * bitmap.getHeight());
    buffer1.rewind();
    bitmap.copyPixelsFromBuffer(buffer1);

    return bitmap;
}

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

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

发布评论

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

评论(1

瀟灑尐姊 2024-11-13 01:44:11

这是使用画布实现此目的的一种方法:

    canvas.drawBitmap(bgr, bgrPosition, 0, null); 
    canvas.drawBitmap(bgr, 0 - bgrW + bgrPosition, 0, null);

    //next value for the background's position 
    if ( (bgrPosition += bgrDx) > bgrW){
        bgrPosition = 0;
    }

bgrDx 是滚动速度(尝试 bgrDx = 1),而 bgrW 是 bgr 位图的宽度。
此方法重复图像并产生无限滚动,但是您需要使用循环图像(两端相同)以使滚动看起来无缝。也许您应该尝试一下,看看它是否对您有更好的效果。

This is one way to do it using canvas:

    canvas.drawBitmap(bgr, bgrPosition, 0, null); 
    canvas.drawBitmap(bgr, 0 - bgrW + bgrPosition, 0, null);

    //next value for the background's position 
    if ( (bgrPosition += bgrDx) > bgrW){
        bgrPosition = 0;
    }

bgrDx is the speed of scrolling try bgrDx = 1, while bgrW is the width of the bgr bitmap.
This method repeats the image and produces an infinite scroll, however you would need to use a looped image (the same on both ends) to make the scroll look seamless. Perhaps you should experiment with this and see if it performs any better for you.

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