如何在SurfaceView上绘制位图? [英] How to draw a bitmap to a SurfaceView?

查看:177
本文介绍了如何在SurfaceView上绘制位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上尝试使用以下代码在我的SurfaceView上绘制位图:(这将在另一个Threadwhile中运行,因为它需要刷新SurfaceView).

I am trying to draw a bitmap to my SurfaceView actually with the following code: (this will be run in another Thread and in a while, because it needs to refresh the SurfaceView).

        while (true)
        {
            try
            {
                // Enable drawing
                // ERROR LINE!
                Canvas ca = mPreview2.Holder.LockCanvas();

                // Get current frame
                Bitmap test = mediaPlayer.CurrentFrame;

                // Actual drawing
                Paint paint = new Paint();
                ca.DrawBitmap(test, 0, 0, paint);   

                // Stop drawing
                mPreview2.Holder.UnlockCanvasAndPost(ca);
            } catch (Exception ex)
            {
                throw ex;
            }
        }

但是我遇到了以下错误:(这是在线发生的:Canvas ca = mPreview2.Holder.LockCanvas();

But I've got the following error: (this is happening on line: Canvas ca = mPreview2.Holder.LockCanvas();

Java.Lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference

推荐答案

现在我可以绘制位图了,但是仍然有一个问题!

Right now I can draw a bitmap, but I have still one problem!

因为右屏幕的质量确实很差(参见图片):

Because the quality of the right screen is really bad (see image):

已解决的问题:

我所做的是使用MemoryStream,它将Bitmap压缩为质量为100的.JPG,并将byte array解码为Bitmap.现在效果很好!参见下面的代码:

What I did is using a MemoryStream which compress the Bitmap to .JPG with a quality of 100 and decode the byte array to a Bitmap. It works great now! See code below:

   private void DrawCanvas()
        {
            while (true)
            {
                Canvas canvas = holder2.LockCanvas();

                if (canvas != null)
                {
                    Bitmap currentBitmap = mediaPlayer.CurrentFrame;

                    if(currentBitmap != null)
                    {
                        Paint paint = new Paint();

                        MemoryStream ms = new MemoryStream();

                        currentBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);

                        byte[] bitmapData = ms.ToArray();

                        Bitmap bitmap = BitmapFactory.DecodeByteArray(bitmapData, 0, bitmapData.Length);

                        Bitmap scaledBitmap = Bitmap.CreateScaledBitmap(bitmap, mPreview2.Width, mPreview2.Height, true);

                        canvas.DrawBitmap(scaledBitmap, 0, 0, paint);

                        bitmap.Recycle();
                        scaledBitmap.Recycle();
                        currentBitmap.Recycle();
                    }
                    holder2.UnlockCanvasAndPost(canvas);
                }
            }
        }

这篇关于如何在SurfaceView上绘制位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆