簡體   English   中英

使用字符串更改位圖

[英]Using a String to change a Bitmap

我有以下問題:

    String explosion_type = this.prefs.getString("explosion_type", "ring_explosion");

    BitmapFactory.Options optionsExplosion = new BitmapFactory.Options();
    optionsExplosion.inPurgeable = true;
    this._explosionSprite = BitmapFactory.decodeResource(_context.getResources(), 
com.forwardapps.liveitems.R.drawable.explosion, optionsExplosion);

我試圖使用String代替資源名稱,這樣我可以在首選項菜單中熱交換資源。 此方法導致應用崩潰。

實施這種情況的正確方法是什么? (無需太復雜或使用if語句)

在Resources對象上使用方法getIdentifier來獲取可繪制對象的ID:

String explosion_type = this.prefs.getString("explosion_type", "ring_explosion");
    Log.i("imagename", explosion_type);
    BitmapFactory.Options optionsExplosion = new BitmapFactory.Options();
    optionsExplosion.inPurgeable = true;

     int imageResource = getResources().getIdentifier(explosion_type, "drawable", getPackageName());

        this._explosionSprite = BitmapFactory.decodeResource(_context.getResources(), imageResource, optionsExplosion);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM