2011-07-14 151 views
1

根据屏幕分辨率,我将图像添加到屏幕。因此,根据屏幕分辨率,我添加适合尺寸的图像。这是否落入任何设计模式?还是有更适合这种要求的设计模式?根据屏幕分辨率加载图像的设计模式

ImageFactory.getBitmap(); 

    public static Bitmap getBitmap(){ 

     if(screenWidth == 360 && screenHeight== 480){ 
      return new Bitmap("360480.bmp"); 
     } 
     else { 
      return new Bitmap("320240.bmp"); 
     } 
    } 

回答

1

这看起来像一个工厂模式。你的工厂很聪明,通过考虑屏幕尺寸来创建/返回哪些位图,并且从我所看到的我认为你在那里做得很好。

当你在这里,你可能想要考虑图像的命名模式(我认为像“640x480.bmp”比“640480.bmp”更容易)。

1

看起来,在这种特殊情况下,您可以避开常规战略(而不是设计模式)。

事情是这样的:

public static Bitmap getBitmap(){ 
    String fileName = Integer.toString(screenWidth) 
     + Integer.toString(screenHeight) + ".bmp" 

    File f = new File(fileName); 

    if(f.exists()){ 
     return new Bitmap(fileName); 
    } 
    else { 
     return new Bitmap("320240.bmp"); 
    } 
}