在画布上绘制固定大小的位图图像

我正在创建一个纸牌游戏,为此我创建了一个自定义表面视图,其中图像正在加载。由于图像是从互联网上下载的,因此它们的大小不同,在屏幕上看起来很糟糕。我想在这里实现两件事。

  1. 加载固定大小的图像或动态调整图像大小。

  2. 从屏幕底部向上绘制图像。

对于第一点,我使用了 CreateBitmap 方法,但低于异常。

java.lang.OutOfMemoryError: Failed to allocate a 1915060280 byte allocation with 4194304 free bytes and 123MB until OOM error

为了解决这个问题,我想根据这个问题这个使用 Glide/Picasso ,但我发现 Glide/Picasso 只在 imageview 上加载图像,但我没有任何 imageview,我只有一个线性布局内的自定义表面视图.

对于第二点,我使用了图像的旋转。以下是它的代码。

  public  void Render(Canvas paramCanvas)

    {


        try

        {

            // paramCanvas.DrawColor(Android.Graphics.Color.Blue);



            int i = 0;

            Down_Card_Gap = 0;

            foreach (Cards localcard in FaceDownDeck.ToList())

            {



                Bitmap localimage = BitmapFactory.DecodeResource(Resources, localcard.GetImageId(context));  

                Bitmap rotatedimage = RotateBitmap(localimage, 180);

                paramCanvas.DrawBitmap(rotatedimage, (Screen_Center_X - Card_Width / 2)+Down_Card_Gap, (Screen_Height - Card_Height), null);

               //   paramCanvas.DrawBitmap(localimage, (Screen_Center_X - Card_Width / 2), (Screen_Center_Y - Card_Height), null);



                if (i++ == 7)

                { break; }

                if (Down_Card_Gap > 0)

                {

                    Down_Card_Gap += Card_Width / 2; 

                }

                else

                {

                    Down_Card_Gap -= Card_Width / 2;

                }

                Down_Card_Gap *= -1;

            }

        }

        catch (Exception ex)

        {

            System.Diagnostics.Debug.WriteLine(ex.ToString());

        }

    }


    private Bitmap RotateBitmap(Bitmap localimage, float angle)

    {

         Matrix matrix = new Matrix();

        matrix.PostRotate(angle);

        matrix.PostScale(Card_Width, Card_Height);

        Bitmap resized= Bitmap.CreateBitmap(localimage, 0, 0, localimage.Width, localimage.Height, matrix, true);

        localimage.Recycle();

        return resized;

    }

我想知道这是否是一种正确的方法,或者是否有更好的方法来实现该功能。


胡说叔叔
浏览 233回答 1
1回答

炎炎设计

加载固定大小的图像或动态调整图像大小。关于fixed size和resize,可以参考这个,finddecodeFile方法:&nbsp; &nbsp;protected Bitmap decodeFile(File f) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //decode image size&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BitmapFactory.Options o = new BitmapFactory.Options();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o.inJustDecodeBounds = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BitmapFactory.decodeStream(new FileInputStream(f), null, o);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Find the correct scale value. It should be the power of 2.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int REQUIRED_SIZE = 150;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int width_tmp = o.outWidth, height_tmp = o.outHeight;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int scale = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width_tmp /= 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height_tmp /= 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scale *= 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //decode with inSampleSize&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BitmapFactory.Options o2 = new BitmapFactory.Options();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o2.inSampleSize = scale;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);&nbsp; &nbsp; &nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp;&nbsp; &nbsp; }&nbsp;可以看到,它用于 BitmapFactory.Options.inJustDecodeBounds= true预加载位图,并缩放位图。也可以参考官方文档。阅读本文以压缩位图的质量。
打开App,查看更多内容
随时随地看视频慕课网APP