Android 进度条重置并从某个分数值重新开始
Android Progress Bar reset and start over on some score value
我正在制作一个简单的游戏,用户点击和得分会增加它的价值。如果分数在某个点上,比如 200,beast 将在不同的 imageView 上进化等等。
问题是,如果我到达 if 语句中的点,它会工作,但是当我通过单击上部按钮更改 activity,然后将其更改回主Activity 进度条时,其状态将更改为不同的值,比我点击它显示正确的填充。
如何解决?我还需要进度自己保存并在关闭和打开应用程序后填充栏....我在 SharedPreferences 中保存了分数和进度值,所以我不知道问题出在哪里。 (对不起 EN)。加几张图方便理解。
我的主要 Activity 代码:
public class MainActivity extends Activity
{
int score;
int progress = score;
ImageButton beast;
SoundPool sp = new SoundPool(15, AudioManager.STREAM_MUSIC, 0);
ProgressBar mProgress;
DecimalFormat df = new DecimalFormat("#,###,###");
private ImageView mScanner;
private Animation mAnimation;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final int soundDd = sp.load(this, R.raw.menu, 1);
mProgress = (ProgressBar) findViewById(R.id.progressBar);
mProgress.getProgressDrawable().setColorFilter(
Color.rgb(0, 199, 140), android.graphics.PorterDuff.Mode.SRC_IN);
mProgress.setScaleY(5f);
fontChange();
addIntegerValue();
mScanner = (ImageView) findViewById(R.id.imageChanger);
mScanner.setVisibility(View.VISIBLE);
mAnimation = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.05f);
mAnimation.setDuration(2000);
mAnimation.setRepeatCount(- 1);
mAnimation.setRepeatMode(Animation.REVERSE);
mAnimation.setInterpolator(new LinearInterpolator());
mScanner.setAnimation(mAnimation);
score = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
TextView ts = (TextView) findViewById(R.id.textview2);
beast = (ImageButton) findViewById(R.id.beastButton);
beast.setSoundEffectsEnabled(false);
if (progress >= 0 && progress <= 199)
{
mProgress.setMax(200);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 200 && progress <= 399)
{
mProgress.setMax(400);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 400 && progress <= 599)
{
mProgress.setMax(600);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 600 && progress <= 799)
{
mProgress.setMax(800);
mScanner.setBackgroundResource(R.drawable.worm);
}
beast.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
sp.play(soundDd, 1, 1, 0, 0, 1);
Intent i = new Intent(getApplicationContext(), BeastSelect.class);
startActivity(i);
}
});
}
public void fontChange()
{
//font set
TextView ts = (TextView) findViewById(R.id.textview2);
TextView tl = (TextView) findViewById(R.id.textLadder);
TextView tb = (TextView) findViewById(R.id.textBeast);
TextView te = (TextView) findViewById(R.id.textevolve);
int low = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
mProgress.setProgress(low);
ts.setText("SCORE : " + " " + df.format(low));
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Phage Regular.otf");
ts.setTypeface(custom_font);
tl.setTypeface(custom_font);
tb.setTypeface(custom_font);
te.setTypeface(custom_font);
//font set over
}
public void addIntegerValue()
{
RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
rl.setSoundEffectsEnabled(false);
final TextView ts = (TextView) findViewById(R.id.textview2);
final int soundId = sp.load(this, R.raw.coin, 1);
rl.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
score++;
mProgress.setProgress(score);
sp.play(soundId, 1, 1, 0, 0, 1);
ts.setText("SCORE : " + " " + df.format(score));
if (progress >= 0 && progress <= 199)
{
mProgress.setMax(200);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 200 && progress <= 399)
{
mProgress.setMax(400);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 400 && progress <= 599)
{
mProgress.setMax(600);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 600 && progress <= 799)
{
mProgress.setMax(800);
mScanner.setBackgroundResource(R.drawable.worm);
}
}
});
}
protected void onPause()
{
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("value", score).apply();
}
protected void onDestroy()
{
super.onDestroy();
sp.release();
sp = null;
}
}
我的第二个Activity的代码:
public class BeastSelect extends Activity
{
ImageButton back;
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_beast_select);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final int soundDd = sp.load(this, R.raw.menu, 1);
back = (ImageButton) findViewById(R.id.imageBack);
back.setSoundEffectsEnabled(false);
fontChange();
back.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
sp.play(soundDd, 1, 1, 0, 0, 1);
Intent b = new Intent(getApplicationContext(), MainActivity.class);
startActivity(b);
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
// do something on back.
Intent b = new Intent(getApplicationContext(), MainActivity.class);
startActivity(b);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void fontChange()
{
//font set
TextView hd = (TextView) findViewById(R.id.beasttext);
TextView cm = (TextView) findViewById(R.id.textcommon);
TextView tm = (TextView) findViewById(R.id.textmedium);
TextView pr = (TextView) findViewById(R.id.textpro);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Phage Regular.otf");
hd.setTypeface(custom_font);
cm.setTypeface(custom_font);
tm.setTypeface(custom_font);
pr.setTypeface(custom_font);
//font set over
}
}
在您的 activity onResume 方法中执行此操作:
mProgress = (ProgressBar) findViewById(R.id.progressBar);
score = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
mProgress.setProgress(score);
我正在制作一个简单的游戏,用户点击和得分会增加它的价值。如果分数在某个点上,比如 200,beast 将在不同的 imageView 上进化等等。
问题是,如果我到达 if 语句中的点,它会工作,但是当我通过单击上部按钮更改 activity,然后将其更改回主Activity 进度条时,其状态将更改为不同的值,比我点击它显示正确的填充。
如何解决?我还需要进度自己保存并在关闭和打开应用程序后填充栏....我在 SharedPreferences 中保存了分数和进度值,所以我不知道问题出在哪里。 (对不起 EN)。加几张图方便理解。
我的主要 Activity 代码:
public class MainActivity extends Activity
{
int score;
int progress = score;
ImageButton beast;
SoundPool sp = new SoundPool(15, AudioManager.STREAM_MUSIC, 0);
ProgressBar mProgress;
DecimalFormat df = new DecimalFormat("#,###,###");
private ImageView mScanner;
private Animation mAnimation;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final int soundDd = sp.load(this, R.raw.menu, 1);
mProgress = (ProgressBar) findViewById(R.id.progressBar);
mProgress.getProgressDrawable().setColorFilter(
Color.rgb(0, 199, 140), android.graphics.PorterDuff.Mode.SRC_IN);
mProgress.setScaleY(5f);
fontChange();
addIntegerValue();
mScanner = (ImageView) findViewById(R.id.imageChanger);
mScanner.setVisibility(View.VISIBLE);
mAnimation = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.05f);
mAnimation.setDuration(2000);
mAnimation.setRepeatCount(- 1);
mAnimation.setRepeatMode(Animation.REVERSE);
mAnimation.setInterpolator(new LinearInterpolator());
mScanner.setAnimation(mAnimation);
score = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
TextView ts = (TextView) findViewById(R.id.textview2);
beast = (ImageButton) findViewById(R.id.beastButton);
beast.setSoundEffectsEnabled(false);
if (progress >= 0 && progress <= 199)
{
mProgress.setMax(200);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 200 && progress <= 399)
{
mProgress.setMax(400);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 400 && progress <= 599)
{
mProgress.setMax(600);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 600 && progress <= 799)
{
mProgress.setMax(800);
mScanner.setBackgroundResource(R.drawable.worm);
}
beast.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
sp.play(soundDd, 1, 1, 0, 0, 1);
Intent i = new Intent(getApplicationContext(), BeastSelect.class);
startActivity(i);
}
});
}
public void fontChange()
{
//font set
TextView ts = (TextView) findViewById(R.id.textview2);
TextView tl = (TextView) findViewById(R.id.textLadder);
TextView tb = (TextView) findViewById(R.id.textBeast);
TextView te = (TextView) findViewById(R.id.textevolve);
int low = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
mProgress.setProgress(low);
ts.setText("SCORE : " + " " + df.format(low));
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Phage Regular.otf");
ts.setTypeface(custom_font);
tl.setTypeface(custom_font);
tb.setTypeface(custom_font);
te.setTypeface(custom_font);
//font set over
}
public void addIntegerValue()
{
RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
rl.setSoundEffectsEnabled(false);
final TextView ts = (TextView) findViewById(R.id.textview2);
final int soundId = sp.load(this, R.raw.coin, 1);
rl.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
score++;
mProgress.setProgress(score);
sp.play(soundId, 1, 1, 0, 0, 1);
ts.setText("SCORE : " + " " + df.format(score));
if (progress >= 0 && progress <= 199)
{
mProgress.setMax(200);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 200 && progress <= 399)
{
mProgress.setMax(400);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 400 && progress <= 599)
{
mProgress.setMax(600);
mScanner.setBackgroundResource(R.drawable.worm);
}
if (progress >= 600 && progress <= 799)
{
mProgress.setMax(800);
mScanner.setBackgroundResource(R.drawable.worm);
}
}
});
}
protected void onPause()
{
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("value", score).apply();
}
protected void onDestroy()
{
super.onDestroy();
sp.release();
sp = null;
}
}
我的第二个Activity的代码:
public class BeastSelect extends Activity
{
ImageButton back;
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_beast_select);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final int soundDd = sp.load(this, R.raw.menu, 1);
back = (ImageButton) findViewById(R.id.imageBack);
back.setSoundEffectsEnabled(false);
fontChange();
back.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
sp.play(soundDd, 1, 1, 0, 0, 1);
Intent b = new Intent(getApplicationContext(), MainActivity.class);
startActivity(b);
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
// do something on back.
Intent b = new Intent(getApplicationContext(), MainActivity.class);
startActivity(b);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void fontChange()
{
//font set
TextView hd = (TextView) findViewById(R.id.beasttext);
TextView cm = (TextView) findViewById(R.id.textcommon);
TextView tm = (TextView) findViewById(R.id.textmedium);
TextView pr = (TextView) findViewById(R.id.textpro);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Phage Regular.otf");
hd.setTypeface(custom_font);
cm.setTypeface(custom_font);
tm.setTypeface(custom_font);
pr.setTypeface(custom_font);
//font set over
}
}
在您的 activity onResume 方法中执行此操作:
mProgress = (ProgressBar) findViewById(R.id.progressBar);
score = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
mProgress.setProgress(score);