Android 在特定时间后改变颜色

Android 在特定时间后改变颜色,android,Android,如何使应用程序在特定时间后自动改变颜色。我的代码由于某种原因无法工作 Random r = new Random(); Timer t = new Timer(); LinearLayout ll = (LinearLayout) findViewById(R.id.ll); long m = System.nanoTime(); int seconds = (int) (m/1000); if(seconds <= 5){

如何使应用程序在特定时间后自动改变颜色。我的代码由于某种原因无法工作

    Random r = new Random();
    Timer t = new Timer();
    LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

    long m = System.nanoTime();
    int seconds = (int) (m/1000);

    if(seconds <= 5){
       ll.setBackgroundColor(Color.BLACK);
    }else{
       ll.setBackgroundColor(Color.WHITE);
    }
Random r=new Random();
定时器t=新定时器();
LinearLayout ll=(LinearLayout)findViewById(R.id.ll);
long m=System.nanoTime();
整数秒=(整数)(m/1000);

如果(seconds您试图将纳秒转换为秒,但实际上您所做的并不相同

试一试

要转换为nan0秒到秒

编辑:

如果您只想不断更改颜色,请使用以下代码:

public static int color = 1;
android.os.Handler customHandler = new android.os.Handler();
                customHandler.postDelayed(updateTimerThread, 0);
及其定义:

private Runnable updateTimerThread = new Runnable()
{
        public void run()
        {
            //write here whaterver you want to repeat
            if(color == 1){
            ll.setBackgroundColor(Color.BLACK);
            color = 2;
            }else{
              ll.setBackgroundColor(Color.WHITE);
              color = 1;
            }

            customHandler.postDelayed(this, 5000);// will repeat after 5 seconds
        }
};

你试图将纳秒转换为秒,但实际上你所做的是不一样的

试一试

要转换为nan0秒到秒

编辑:

如果您只想不断更改颜色,请使用以下代码:

public static int color = 1;
android.os.Handler customHandler = new android.os.Handler();
                customHandler.postDelayed(updateTimerThread, 0);
及其定义:

private Runnable updateTimerThread = new Runnable()
{
        public void run()
        {
            //write here whaterver you want to repeat
            if(color == 1){
            ll.setBackgroundColor(Color.BLACK);
            color = 2;
            }else{
              ll.setBackgroundColor(Color.WHITE);
              color = 1;
            }

            customHandler.postDelayed(this, 5000);// will repeat after 5 seconds
        }
};
尝试按照代码进行操作

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);
//Adding Log Statement to check

Log.i(Tag, String.valueOf(seconds);
if(seconds % 10 < 5){
       ll.setBackgroundColor(Color.BLACK);
    }else{
       ll.setBackgroundColor(Color.WHITE);
    }
Calendar c=Calendar.getInstance();
int seconds=c.get(日历秒);
//添加要检查的日志语句
Log.i(标签、字符串、值)(秒);
如果(秒数%10<5){
ll.setBackgroundColor(颜色为黑色);
}否则{
ll.setBackgroundColor(颜色:白色);
}
尝试按照代码进行操作

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);
//Adding Log Statement to check

Log.i(Tag, String.valueOf(seconds);
if(seconds % 10 < 5){
       ll.setBackgroundColor(Color.BLACK);
    }else{
       ll.setBackgroundColor(Color.WHITE);
    }
Calendar c=Calendar.getInstance();
int seconds=c.get(日历秒);
//添加要检查的日志语句
Log.i(标签、字符串、值)(秒);
如果(秒数%10<5){
ll.setBackgroundColor(颜色为黑色);
}否则{
ll.setBackgroundColor(颜色:白色);
}

您可以使用tread来管理此操作,尝试此操作。线程将是实现此操作的最佳方法,因为您希望以固定间隔连续更改颜色

Handler handler ;
LinearLayout ll ;
int i = 0;
int colors[] = {Color.BLACK, Color.WHITE, Color.BLUE, Color.GREEN, Color.GRAY};
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        ll.setBackgroundColor(colors[i]);
        i++;
     }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ......
    .......
    handler  = new Handler();
    ll = (LinearLayout) findViewById(R.id.ll);

    Thread myTread = new Thread(){
        public void run() {
            try {
                while(true){
                    sleep(3000);
                    handler.post(runnable);

                }

            }
            catch (InterruptedException e) {}
        }

    };
    myTread.start();
}
代码将按照代码中的说明每3秒更改一次背景色
sleep(3000)
。此外,每次更改时,还将从一组预定义的颜色数组中进行选择


您也可以使用随机生成颜色。您可以随机生成
rgb
值,也可以像我所做的那样将颜色存储在一个数组中并随机迭代它

您可以使用tread来管理它,试试这个。线程将是实现这一点的最佳方式,因为您希望在常规inte时连续更改颜色里瓦尔

Handler handler ;
LinearLayout ll ;
int i = 0;
int colors[] = {Color.BLACK, Color.WHITE, Color.BLUE, Color.GREEN, Color.GRAY};
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        ll.setBackgroundColor(colors[i]);
        i++;
     }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ......
    .......
    handler  = new Handler();
    ll = (LinearLayout) findViewById(R.id.ll);

    Thread myTread = new Thread(){
        public void run() {
            try {
                while(true){
                    sleep(3000);
                    handler.post(runnable);

                }

            }
            catch (InterruptedException e) {}
        }

    };
    myTread.start();
}
代码将按照代码中的说明每3秒更改一次背景色
sleep(3000)
。此外,每次更改时,还将从一组预定义的颜色数组中进行选择


您也可以使用随机生成颜色。您可以随机生成
rgb
值,也可以像我所做的那样将颜色存储在一个数组中并随机迭代它

请尝试以下代码进行颜色更改。 使用倒计时器根据条件更改颜色

new CountDownTimer(5000,1000)
{
     onFinish()
     {

          if(flag==1)
          {
                     //color set black
                     flag=0;
                     //start the countdown timer again.
           }
          else
           {
                     //color set white
                     flag=1;
                     //start the countdown timer again.
           }
     }
}

请尝试以下代码进行颜色更改。 使用倒计时器根据条件更改颜色

new CountDownTimer(5000,1000)
{
     onFinish()
     {

          if(flag==1)
          {
                     //color set black
                     flag=0;
                     //start the countdown timer again.
           }
          else
           {
                     //color set white
                     flag=1;
                     //start the countdown timer again.
           }
     }
}

它显示的是什么颜色?你需要将你的
if条件
放入计时器的
onTick()
方法中,最好使用
CountdownTimer
看看它显示的是什么颜色?你需要将你的
if条件
放入
onTick()中
计时器的方法,最好使用
倒计时
现在查看此处屏幕仅为黑色。尝试记录以查看秒值以更好地理解当前屏幕仅为黑色。尝试记录以查看秒值以更好地理解编辑。请尝试。您可能不再需要任何循环迭代!已编辑。请尝试。您可能不需要任何loop迭代了!