سؤال

I have an android game. I change the color scheme on touch. The code is:

if(rect_dark.contains(x, y)) {    
    isdark = !isdark;
    invalidate();
}

Then in my onDraw method:

        if(isdark){
            this.setBackgroundColor(getResources().getColor(R.color.black_color));
        } else{
            this.setBackgroundColor(getResources().getColor(R.color.light_color));
        }


        if(isdark){
            mScorePaint.setColor(getResources().getColor(R.color.light_color));

        } else{
            mScorePaint.setColor(getResources().getColor(R.color.black_color)); 
        }

So, when I press the button the changings of color are not synchronized. At first the background color is changed and then the text color. Eyes see that.

How to deal this problem? Thanks for all the answers.

هل كانت مفيدة؟

المحلول

Maybe this will improve the performance just enough:

private int mCurrentColor;

if(rect_dark.contains(x, y)) {
    isdark = !isdark;

    if (isdark) {
        mCurrentColor = getResources().getColor(R.color.dark_color);
    } else {
        mCurrentColor = getResources().getColor(R.color.light_color);
    }

    invalidate();
}

This avoids the color assignments in onDraw method. If this is still unsatisfactory, you might try double buffering, as mentioned here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top