我正在开发一个动态壁纸,允许用户从他们的图库中选择静态图像并将其作为主屏幕上的背景。我一直在关注这里接受的答案:为动态壁纸选择背景,一切正常,直到我不得不实现以下代码(答案中的最后一部分):
void getBackground() {
if (this.cvwidth == 0 || this.cvheight == 0 || this.visibleWidth == 0) {
this.cvwidth = 480;
this.cvheight = 854;
this.visibleWidth = 480;}
if(new File(imageBg).exists()) {
int SampleSize = 1;
do {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bg = BitmapFactory.decodeFile(imageBg, options);
SampleSize = (int) (Math.ceil(options.outWidth/(this.visibleWidth * 2))*2);
options.inJustDecodeBounds = false;
try {options.inSampleSize = SampleSize;
bg = BitmapFactory.decodeFile(imageBg, options);}
catch (OutOfMemoryError e) {
SampleSize = SampleSize * 2;
}
} while (bg == null);
bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);}
else {bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);}
LoadText = "";
}
添加适当的变量后,其他所有内容都按原样工作。这里让我感到困惑的是这条线else {bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
,它给了我错误bg cannot be resolved or is not a field
,指的是R.drawable.bg
. 我在这里想念什么?
任何人?