Avoid View class to update itself

  • 0 Antworten
  • Neuester Beitrag
Diskutiere Avoid View class to update itself im Android App Entwicklung im Bereich Betriebssysteme & Apps.
U

UndeadLeech

Neues Mitglied
I have a GameLayout class, which is a View class for animating a Sprite, next to my main game XML.

I update this animation by using view.invalidate() in the Runnable of my main game activity. So every time the run() method is called the Bitmap updates 4 times (so it's exactly through 1 time).

My problem is, that if I click on the button on my XML the Bitmap sometimes refreshs, too, what destroys everything.

If you have any solutions for my problem how I can avoid my Image to refresh please tell me.

This is the GameLayout class:
Code:
public class GameLayout extends View
{
    private Sprite sprite;
    private Bitmap bmp;
    
    public GameLayout(Context context) 
    {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.froschanimation);
        sprite = new Sprite(bmp, 0, 0, 400, 100, 5, 4);
    }
    
    @Override
    protected void onDraw(Canvas canvas)
    {
        sprite.draw(canvas);
        update(System.currentTimeMillis());
    }

    public void update(long currentTimeMillis) 
    {
        sprite.update(currentTimeMillis);
    }
}
And this is my main Game Activity:

Code:
public class GameActivity extends Activity implements OnClickListener, Runnable
{
    private int punkte;
    private int highscore;
    private Handler handler = new Handler();
    private int balken = 0;
    private boolean abnehmend = false;
    private int Klick = 0;
    private boolean balkenAktiv = true;
    private boolean gewartet = false;
    private int versuche;
    private View view;
    private int counter = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        // inflate mainXML->
        View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
        // find container->
        LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
        // initialize your custom view->
        view = new GameLayout(this);
        // add your custom view to container->
        container.addView(view);
        
        setContentView(mainView);
        
        versuche = leseVersuche();
        highscore = leseHighscore();
        setupActionBar();
        neueRunde();    
        
        Button button = (Button) findViewById(R.id.thebutton);
        button.setOnClickListener(this);
    }
    
    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() 
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
        {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()) 
        {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private void neueRunde()
    {
        punkte = 0;
        refreshScreen();
    }
    
    private void refreshScreen()
    {
        TextView tvPunkte = (TextView) findViewById(R.id.points);
        tvPunkte.setText("Punkte: " + Integer.toString(punkte));    
        
        TextView tvHighscores = (TextView) findViewById(R.id.highscores);
        tvHighscores.setText("Highscore: " + Integer.toString(highscore));
        
        TextView tvVersuche = (TextView) findViewById(R.id.versuches);
        tvVersuche.setText("Versuche: " + Integer.toString(versuche));
    }
        
    @Override
    public void onClick(View v)
    {
        if(Klick == 0)
        {
            handler.postDelayed(this, 0);
            Klick = 1;
        }
        else
        {
            if(Klick == 1)
            {    
                balkenAktiv = false;
                handler.postDelayed(this, 500);
                punkte = balken;
                versuche++;
                schreibeVersuche(versuche);
                highscore += punkte;
                schreibeHighscore(highscore);
                Klick = 2;
                refreshScreen();
            }
            else
            {
            if(Klick == 2)
                {
                    Klick = 0;
                    punkte = 0;
                    balken = 0;
                    ProgressBar proBar = (ProgressBar) findViewById(R.id.sprung);
                    proBar.setProgress(balken);
                    refreshScreen();
                }
            }
        }
    }
    
    @Override
    public void run()
    {    
        if(balkenAktiv == true)
        {
            gewartet = false;
            ProgressBar proBar = (ProgressBar) findViewById(R.id.sprung);
            if(abnehmend == false)
            {
                if(balken < 100)
                {
                    balken+=5;
                    proBar.setProgress(balken);
                    refreshScreen();
                    handler.postDelayed(this, 0);
                }
                else if(balken == 100)
                {
                    abnehmend = true;
                    proBar.setProgress(balken);
                    refreshScreen();
                    handler.postDelayed(this, 0);
                }
            }
            
            else if(abnehmend == true)
            {
                if(balken > 0)
                {
                    balken-=5;
                    proBar.setProgress(balken);
                    refreshScreen();
                    handler.postDelayed(this, 0);
                }
                else if (balken == 0)
                {
                    abnehmend = false;
                    proBar.setProgress(balken);
                    refreshScreen();
                    handler.postDelayed(this, 0);
                }
            }        
        }        
        else if (balkenAktiv == false)
        {        
            if(gewartet == true)
            {
                if(counter < 4)
                {
                    counter++;
                    view.invalidate();
                    handler.postDelayed(this, 500);
                }
                else if (counter >= 4)
                {
                    balkenAktiv = true;
                    counter = 0;
                }
            }
            gewartet = true;
        }
    }    
    
    private void schreibeHighscore(int highscore)
    {
        SharedPreferences pref = getSharedPreferences("GAME", 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("HIGHSCORE", highscore);
        editor.commit();
    }
    
    private void schreibeVersuche(int versuche)
    {
        SharedPreferences pref = getSharedPreferences("GAME", 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("VERSUCHE", versuche);
        editor.commit();
    }
    
    private int leseVersuche()
    {
        SharedPreferences pref = getSharedPreferences("GAME", 0);
        return pref.getInt("VERSUCHE", 0);
    }
    
    private int leseHighscore()
    {
        SharedPreferences pref = getSharedPreferences("GAME", 0);
        return pref.getInt("HIGHSCORE", 0);
    }    
}
Would be nice if you could help me out with this!
 
Ähnliche Themen - Avoid View class to update itself Antworten Datum
0