java - Android 进度条重置并重新开始一些分数值

标签 java android android-studio progress-bar

我正在制作一个简单的游戏,用户点击和得分会增加它的值(value)。如果分数在某个点上,比如 200,beast 将在不同的 imageView 上进化等等。

问题是,如果我到达 if 语句中的点,它会工作,但是当我通过单击上部按钮更改 Activity ,然后将其更改回 mainActivity 进度条时,将其状态更改为不同的值,然后我单击并显示正确的填充.

如何解决?我还需要进度自己保存并在关闭和打开应用程序后填充栏....我在 SharedPreferences 中保存了分数和进度值,所以我不知道问题出在哪里。 (对不起 EN)。添加一些图片以使其更容易理解。

how it works

我的主要 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);

关于java - Android 进度条重置并重新开始一些分数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40706076/

相关文章:

SplashScreen 上的 Android Marshmallow 运行时权限

java - JVM 如何优化 Fluent api

java - Java 新手,对名称/变量引用有疑问

java - 我可以在文本文件中使用 Scanner 类来获取信息吗?

java - Android File.exists() 始终为 true

android - "LintError"发现正在处理 android lint 报告

java - 如何在所有可能的组合中找到 3 个数字,总和结果等于 n?

Android 依赖项 'com.android.support:support-v4' 对于编译 (26.1.0) 和运行时 (27.1.1) 类路径错误有不同的版本

java - Android自定义view获取多个属性

java - Android WebView 中的地理位置权限