Android进度条会重置并重新开始某些得分值

时间:2016-11-20 15:52:30

标签: java android android-studio progress-bar

我正在做一个简单的游戏,其中用户点击和分数将增加其价值。如果分数在200分之间,那么野兽就会在不同的imageView上进化,等等。

问题是,如果我在if语句中达到了它的作用,但是当我通过点击上部按钮更改活动然后将其更改回mainActivity Progress栏时将其状态更改为不同的值,而不是单击并显示正确的填充

如何修复?关闭和打开应用程序后,我还需要进度保存并填充栏....我在ScorePreferences中保存了Score和Proggress值,所以我不知道问题出在哪里。 (对不起EN)。添加一些图片以便于理解。

how it works

我的主要活动代码:

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;
    }

}

我的第二个活动的代码:

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
    }
}

1 个答案:

答案 0 :(得分:2)

在您的活动onResume方法中执行以下操作:

mProgress = (ProgressBar) findViewById(R.id.progressBar);
score = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
mProgress.setProgress(score);