简体   繁体   English

Android位图-内存不足

[英]Android bitmap - out of memory

I have a problem with a function that I implemented. 我实现的功能有问题。 On some phones I get a out of memory error. 在某些手机上,出现内存不足错误。

private Bitmap getIconMarkerOfPlayer(Player p) {
    Drawable drawable = getIconOfPlayer(p);
    Bitmap img = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(img);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);

    final float scale = this.getResources().getDisplayMetrics().density;
    int heigthPixels = (int) (24 * scale + 0.5f);
    int widthPixels = (int) (24 * scale + 0.5f);
    return Bitmap.createScaledBitmap(img, widthPixels, heigthPixels, false);
}

The getIconMarkerOfPlayer(Player p) function gives a "programmed" drawable depending on the player's status (dead, alive, offline, ...) and its color. getIconMarkerOfPlayer(Player p)函数根据播放器的状态(死,活着,离线,...)及其颜色提供“编程的”可绘制对象。 Each player has a unique color assigned to it in the early games. 在早期的游戏中,每个玩家都具有唯一的颜色。

How can I resize my bitmap from a Drawable object without having a out of memory error? 如何在不出现内存不足错误的情况下从Drawable对象调整位图的大小?

I suggest you to use some ImageLibraries to load Bitmaps efficiently. 我建议您使用一些ImageLibraries有效地加载位图。

Some of them are Fresco , Glide , Piccasio . 其中一些是壁画滑翔Piccasio

I suggest you to go with Glide . 我建议您选择Glide Have a look at it here 这里看看

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

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