私のゲームは、すべてのクラス 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));