0

私のゲームは、すべてのクラス onCreate で TextView の背景を更新して、プレーヤーの健康状態を表示する必要がありますが、現時点では、これを実行する唯一の方法はこれです

int Health = 100;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_act1);

    if (Health == 100){
        HealthDisplay.setBackgroundResource(R.drawable.health100);
    } else if (Health == 99){
        HealthDisplay.setBackgroundResource(R.drawable.health99);
    } else if (Health == 98){
        HealthDisplay.setBackgroundResource(R.drawable.health98);
    } else if (Health == 99){
        HealthDisplay.setBackgroundResource(R.drawable.health98);
    } else if (Health == 99){
        HealthDisplay.setBackgroundResource(R.drawable.health98);
    } else if (Health == 99){
        HealthDisplay.setBackgroundResource(R.drawable.health98);
    }

etc.
}

特に他の2つの統計についても同様のことを行う必要があるため、これを行うためのより簡単で高速な方法が必要です。

別のクラスでそれを処理し、そのクラスを実行して背景画像を更新してからこれに戻るように onCreate に1行または2行入れることを考えました。

あるいは、おそらくこのようなことが可能ですか?

int Health = 100;

HealthDisplay.setBackgroundResource(R.drawable.health(Health));
4

3 に答える 3

0

私が提案するのは、(完全な健康のために)1つの画像を用意し、毎回それを切り取り、健康レベルに応じてその割合を表示することです. 例えば:

private ImageView healthLevel;
private ClipDrawable clipDrawable;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            healthLevel = (ImageView ) findViewById(R.id.health);   //there a corresponding ImageView in the layout
            BitmapDrawable bitmapDrawable = new BitmapDrawable(BitmapFactory.decodeResource(getResources(),    R.drawable.full_health));
            //vertical bar cropped from top
            clipDrawable = new ClipDrawable(bitmapDrawable, Gravity.BOTTOM, ClipDrawable.VERTICAL);  
            healthLevel.setImageDrawable(clipDrawable);
    }

次に、別のスレッドで次のように呼び出します。

int health = 54;
clipDrawable.setLevel(health);
clipDrawable.invalidateSelf();
于 2013-01-22T15:16:08.850 に答える
0

「私のゲームでは、すべてのクラス onCreate で TextView の背景を更新して、プレーヤーの健康状態を表示する必要があります」

わかりません。実行時にバックグラウンドを更新する場合は、一度だけ呼び出されるため (アクティビティの作成時)、onCreate で実行しないでください。

TextView で setBackgroundResource を呼び出して、この背景を更新するメソッドを作成するだけです。

于 2013-01-22T14:57:26.797 に答える
0

ThomasKa の答えは良いものです。1 つのリソースをトリミングすると、多くの時間を節約でき、適切に行うと見栄えがよくなります。ただし、必要に応じて、100 個の個別のドローアブルを使用できます。

やりたいことは、ドローアブルに適切な名前を付け(数字の接尾辞を付けて)、名前でそれらを取得することです。Resources.getIdentifier()そのために次のようなものを使用できます。

Resources res = getResources();
int resId = res.getIdentifier("health" + Health, "drawable", getPackageName());
HealthDisplay.setBackgroundResource(resId);

この例では、例のように、ドローアブルの名前が health100、health99 などであると想定しています。

于 2013-02-26T21:01:49.207 に答える