gpt4 book ai didi

android - java.lang.OutOfMemoryError 在 android.graphics.BitmapFactory.decodeResource (BitmapFactory.java :374)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:10 25 4
gpt4 key购买 nike

我在向 gridview 添加可变背景时遇到 OutOfMemoryError...在

final Bitmap shelfBackground = BitmapFactory.decodeResource(resources, background);

这是我的代码。

public class ShelvesView extends GridView {
private Bitmap mShelfBackground;
private int mShelfWidth;
private int mShelfHeight;
public ShelvesView(Context context) {
super(context);
}
public ShelvesView(Context context, AttributeSet attrs) {
super(context, attrs);
load(context, attrs, 0);
}
public ShelvesView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
load(context, attrs, defStyle);
}

private void load(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShelvesView, defStyle, 0);
int imageWidth = options.outWidth;
final Resources resources = getResources();
final int background = a.getResourceId(R.styleable.ShelvesView_shelfBackground, 0);
final Bitmap shelfBackground = BitmapFactory.decodeResource(resources, background);
if (shelfBackground != null) {
mShelfWidth = shelfBackground.getWidth();
mShelfHeight = shelfBackground.getHeight();
mShelfBackground = shelfBackground;
}

a.recycle();
}

@Override
protected void dispatchDraw(Canvas canvas) {
final int count = getChildCount();
final int top = count > 0 ? getChildAt(0).getTop() : 0;
final int shelfWidth = mShelfWidth;
final int shelfHeight = mShelfHeight;
final int width = getWidth();
final int height = getHeight();
int bottom = (count > 0) ? getChildAt(count - 5).getBottom() + shelfHeight : 0;
final Bitmap background = mShelfBackground;
for (int x = 0; x < width; x += shelfWidth) {
for (int y = top; y < height; y += shelfHeight) {
canvas.drawBitmap(background, x, y, null);
}
}

//drawDecorations(canvas, top, shelfHeight, width);

// canvas.drawBitmap(mShelfLeftLayer,0,bottom, null);

super.dispatchDraw(canvas);
}

}

最佳答案

为其添加位图选项 -

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
shelfBackground = BitmapFactory.decodeResource(resources, background, options);

关于android - java.lang.OutOfMemoryError 在 android.graphics.BitmapFactory.decodeResource (BitmapFactory.java :374),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12596640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com