简体   繁体   English

如何在Android中将相邻的位图绘制到画布上?

[英]How do I draw adjacent bitmaps to a canvas in Android?

I've spent all night trying to figure out why I can't draw a series of bitmaps completely adjacent to each other (with no gaps in between), using Android. 我花了一整夜的时间来弄清楚为什么我不能使用Android绘制一系列彼此完全相邻的位图(彼此之间没有间隙)。

For context, I am building a spectrogram application which displays a vertical bitmap for each 'window' of audio data that comes in, providing the user with a heatmap of frequencies. 就上下文而言,我正在构建一个频谱图应用程序,该应用程序为传入的每个音频数据“窗口”显示一个垂直位图,为用户提供频率的热图。 At the moment I'm using pre-recorded audio so I can perform all my calculations before I have to display anything - I have an ArrayList of integer arrays, each of which represents one window's bitmap, which is drawn to a canvas using a timer thread. 目前,我正在使用预先录制的音频,因此我可以执行所有计算,然后再显示任何内容-我有一个由整数数组组成的ArrayList,每个数组代表一个窗口的位图,使用计时器将其绘制到画布上线。

I am aware that the approach below will ultimately break when the application tries to draw past the dimensions of the screen, but I am not worrying about that for now. 我知道,当应用程序尝试绘制屏幕尺寸时,下面的方法最终会中断,但是我现在不必担心。 The problem I would like to solve is that the below code results in a one-pixel (ish) gap between the drawn bitmaps, when I would actually like them to be absolutely adjacent. 我要解决的问题是,当我实际上希望它们绝对相邻时,以下代码导致绘制的位图之间存在一个像素(ish)的间隙。

This is the run() method for my timer thread: 这是我的计时器线程的run()方法:

    public void run() {
        Canvas c = null;
        try {
            c = sh.lockCanvas(null);
            synchronized(sh) {
                doDraw(c);
            }
        } finally {
            if (c!=null) {
                sh.unlockCanvasAndPost(c);
            }
        }
    }

This is the doDraw() method which draws the bitmaps, and then skips along to the end of that drawn bitmap in order to draw the next one. 这是doDraw()方法,它绘制位图,然后跳到该绘制位图的末尾以绘制下一个位图。 It simply does so by incrementing the 'windowsDrawn' field: 只需通过增加“ windowsDrawn”字段即可:

    private void doDraw(Canvas canvas) {
        canvas.drawBitmap(spec.getBitmapWindow(windowsDrawn), 0, 1, windowsDrawn, 0, 1, h, false, null);
        System.out.println("Windows drawn: "+windowsDrawn);
        windowsDrawn++;
    }

spec.getBitmapWindow(windowsDrawn) simply returns an integer array of pixel values for the vertical window to be drawn. spec.getBitmapWindow(windowsDrawn)仅返回要绘制的垂直窗口的像素值的整数数组。

Here's a screenshot to show what I'm talking about. 这是显示我在说什么的屏幕截图。 The image looks as if it is behind tiny prison bars and I would like to get rid of these. 该图像看起来好像是在小监狱里,我想摆脱这些。

Thanks! 谢谢!

I found out what I was doing wrong. 我发现自己在做什么错。 The 'prison bars' effect was actually a side-effect of me writing incremental updates to the back-buffer, which was presumably being flipped every so often (hence losing some of my updates to the other buffer, giving the black vertical lines). “监狱条”效应实际上是我向后缓冲区写入增量更新的副作用,这大概是每隔一段时间就会翻转一次(因此,我的一些更新会丢失到另一个缓冲区,以黑色竖线显示)。 The problem was solved by ensuring that I was instead writing to a buffer bitmap and then redrawing the entire frame each time. 通过确保我改为写入缓冲区位图,然后每次重新绘制整个帧,解决了该问题。 Incremental updates to the screen (like I was trying to do) are not allowed in Android. 在Android中,不允许对屏幕进行增量更新(就像我试图做的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM