2016-04-30 3 views
0

長いタイトルを残して申し訳ありませんが、特定のものにしたいと思っています。作品に従っていますが、ほとんどの場合、より良い解決策がありますようお願いいたします。ビットマップを初期化しさまざまなアクティビティに必要なアセットから複数のビットマップをロードするときにアンドロイド上のリソースを節約する方法

クラス:主な活動から

public class Images { 
private String[]eyes=new String[]{ 
     "images/character/face/eyes/blue.png" 
}; 
public LinkedList<Bitmap> eyesBM=new LinkedList<Bitmap>(); 
private int eyesIndex = 0; 

private String[]faceandhands=new String[]{ 
     "images/character/face/faceandhands/white.png", 
     "images/character/face/faceandhands/black.png" 
}; 
public LinkedList<Bitmap> faceandhandsBM=new LinkedList<Bitmap>(); 
private int faceandhandsIndex = 0; 
private String[]facialhair=new String[]{ 
     "images/character/face/facialhair/black.png" 
}; 

public LinkedList<Bitmap> facialhairBM=new LinkedList<Bitmap>(); 
private String[]hair=new String[]{ 
     "images/character/face/hair/grey.png" 
}; 
public LinkedList<Bitmap> hairBM=new LinkedList<Bitmap>(); 
private String[]tattoes=new String[]{ 
     "images/character/face/tattoes/test.png" 
}; 
public LinkedList<Bitmap> tattoesBM=new LinkedList<Bitmap>(); 
private String[]legs=new String[]{ 
     "images/character/legs/brown.png" 
}; 
private LinkedList<Bitmap> legsBM=new LinkedList<Bitmap>(); 
private String[]shirt=new String[]{ 
     "images/character/shirt/purple.png" 
}; 
public LinkedList<Bitmap> shirtBM=new LinkedList<Bitmap>(); 
private String[]shoes=new String[]{ 
     "images/character/shoes/red.png" 
}; 
public LinkedList<Bitmap> shoesBM=new LinkedList<Bitmap>(); 
private String[]background=new String[]{ 
     "images/character/background/normal.png" 
}; 
public LinkedList<Bitmap> backgroundBM=new LinkedList<Bitmap>(); 



public Images(Context c) { 
    loadBitmaps(c,eyes,"eyes"); 
    loadBitmaps(c,faceandhands,"faceandhands"); 
    loadBitmaps(c,facialhair,"facialhair"); 
    loadBitmaps(c,hair,"hair"); 
    loadBitmaps(c,tattoes,"tattoes"); 
    loadBitmaps(c,legs,"legs"); 
    loadBitmaps(c,shirt,"shirt"); 
    loadBitmaps(c,shoes,"shoes"); 
    loadBitmaps(c,background,"background"); 
} 

public Bitmap getBitmapFromAsset(Context context, String filePath) { 
    AssetManager assetManager = context.getAssets(); 
    InputStream istr; 
    Bitmap bitmap = null; 
    try { 
     istr = assetManager.open(filePath); 
     bitmap = BitmapFactory.decodeStream(istr); 
    } catch (IOException e) { 
     System.out.println("Fudeu geral !"); 
    } 

    return bitmap; 
} 
public void loadBitmaps(Context c,String[] src,String dest){ 
    Bitmap bit=null; 
    for (int i = 0;i<src.length;i++){ 
     bit= getBitmapFromAsset(c,src[i]); 
     switch (dest){ 
      case "eyes": 
       eyesBM.add(bit); 
       break; 
      case "faceandhands": 
       faceandhandsBM.add(bit); 
       break; 
      case "facialhair": 
       facialhairBM.add(bit); 
      break; 
      case "hair": 
       hairBM.add(bit); 
      break; 
      case "tattoes": 
       tattoesBM.add(bit); 
      break; 
      case "legs": 
       legsBM.add(bit); 
      break; 
      case "shirt": 
       shirtBM.add(bit); 
      break; 
      case "shoes": 
       shoesBM.add(bit); 
      break; 
      case "background": 
       backgroundBM.add(bit); 
      break; 
     } 
     //dest.add(bit); 
     //dest.add(BitmapFactory.decodeResource(r, src[i])); 
     } 
} 
public Bitmap getBitmap(String name){ 
    switch (name){ 
     case "eyes": 
      eyesIndex=eyesIndex>=eyesBM.size()?0:eyesIndex; 
      Bitmap aux =eyesBM.get(eyesIndex); 
      eyesIndex++; 
      return aux; 
     case "faceandhands": 
      faceandhandsIndex=faceandhandsIndex>=faceandhandsBM.size()?0:faceandhandsIndex; 
      Bitmap aux1=faceandhandsBM.get(faceandhandsIndex); 
      faceandhandsIndex++; 
      return aux1; 
      //return faceandhandsBM.get(0); 
     case "facialhair": 
      return facialhairBM.get(0); 
     case "hair": 
      return hairBM.get(0); 
     case "tattoes": 
      return tattoesBM.get(0); 
     case "legs": 
      return legsBM.get(0); 
     case "shirt": 
      return shirtBM.get(0); 
     case "shoes": 
      return shoesBM.get(0); 
     case "background": 
      return backgroundBM.get(0); 
    } 
    return null; 
}} 

「 コード:

setContentView(R.layout.activity_character); 
    final EditText eT=(EditText)findViewById(R.id.characterName); 
    v = (ImageView)findViewById(R.id.characterDisplay); 
    img = new Images(this); 
    LD=null; 
    DD= new Drawable[]{ 
      new BitmapDrawable(img.getBitmap("legs")), 
      new BitmapDrawable(img.getBitmap("faceandhands")), 
      new BitmapDrawable(img.getBitmap("shirt")), 
      new BitmapDrawable(img.getBitmap("eyes")), 
      new BitmapDrawable(img.getBitmap("facialhair")), 
      new BitmapDrawable(img.getBitmap("hair")), 
      new BitmapDrawable(img.getBitmap("shoes")), 
      new BitmapDrawable(img.getBitmap("tattoes")) 
    }; 
    LD=new LayerDrawable(DD); 
    v.setImageDrawable(LD); 
    ((Button)findViewById(R.id.buttonSkin)).setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      DD[1]=new BitmapDrawable(img.getBitmap("faceandhands")); 
      LD=new LayerDrawable(DD); 
      update(LD); 
      //saveCaracter(eT.getText().toString()); 
      //loadGame(eT.getText().toString()); 
     } 
    }); 

アップデート方法:

private void update(LayerDrawable ld) { 
    v.setImageDrawable(ld); 
} 

答えて

0

私の知っている最も簡単な方法は、使用することですピカソの図書館。これは、1トン分のコーディングを削減します。 http://square.github.io/picasso/

関連する問題