最近有人评论了我的代码,我在其中声明了以下内容:
private Bitmap splashBackground;
private Bitmap lightDot;
private static Bitmap scaledBackground;
private static Bitmap scaledLightDot;
他们建议我不要声明 satic 位图。
但是,我已经尝试了所有方法,除非我将它们声明为静态,否则我的代码不起作用。
此外,“public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)”似乎出现在官方 Android 开发者网站上,所以我对我应该做什么和不应该做什么感到有点困惑。
任何指针将不胜感激 - 谢谢
编辑:澄清:
当我从声明中删除静态时,当我到达我的 onDraw() 方法时,缩放位图为空。(我在 initialise() 方法中创建缩放位图对象,一旦创建,它就是有效的(即,不为空)-但随后在 onDraw 处似乎变为空,除非我将其声明为静态。
我正在从我的活动类中调用我的 initialise() 方法。
编辑:根据要求提供更多代码。
我的 OnCreate 方法:如您所见,我将屏幕高度和宽度传递过来,以便创建缩放位图
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
displaySplashScreen= new SplashScreen(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set View
setContentView(new SplashScreen(this));
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
int width=d.getWidth();
int height=d.getHeight();
displaySplashScreen.initialise(width, height);
}
我的初始化方法:
public void initialise(int w, int h)
{
//Get width and height (passed in from Activity)
vwidth=w;
vheight=h;
//Create pre-scaled bitmaps
scaledBackground = Bitmap.createScaledBitmap(splashBackground, vwidth, vheight, true);
scaledLightDot = Bitmap.createScaledBitmap(lightDot, vwidth, vheight, true);
}
我还可以补充一点,如果我以相同的方式使用标准变量(比如 int number;)并将其设置为 initalise(number = 5;),那么在我的初始化方法中 number 仅等于 5。如果我从 onDraw() 记录它,它总是会反复返回'0'!真是莫名其妙。
到目前为止谢谢大家,如果需要更多代码,请告诉我......