让android屏幕每秒更换一次颜色

时间:2013-03-08 13:49:35

标签: android graphics 2d

我是android的初学者,有一些java在我的腰带下。我已经完成了make my first app app。

接下来我想创建一个使用简单2D图形的应用程序

首先,我想找到一种在无限循环中在两种颜色(红色和绿色)之间闪烁屏幕的方法

wait int seconds;
make screen red;
wait int seconds;
make screen green;

loop forever;

有人可以指点我可能有帮助的教程或源代码吗?

非常感谢

5 个答案:

答案 0 :(得分:3)

感谢您的投入, 这段代码似乎运行正常

package biz.consett.mydraw;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

/* Flash the screen between red and green each second */
public class MainActivity extends Activity {
    final static int INTERVAL = 1000; // 1000=1sec   
    private static View myView = null;
    boolean whichColor = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myView = (View) findViewById(R.id.my_view);
        myView.setBackgroundColor(Color.RED);// set initial colour
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(INTERVAL);
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    updateColor();
                    whichColor = !whichColor;
                }
            }
        }).start();
    }

    private void updateColor() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (whichColor) 
                    myView.setBackgroundColor(Color.RED);
                else 
                    myView.setBackgroundColor(Color.GREEN);
            }
        });
    }
}

布局,activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<View

android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

</View>

</RelativeLayout>

答案 1 :(得分:2)

如何制作填充屏幕的布局,让我们称之为llayout

把它放在代码中:

static final int[] COLORS = {0xff0000, 0x00ff00};
static final int WAITTIME = 1000;
int currentColor = 0;

public void onCreate(Bundle b) {
    setContentView(R.layout.mylayout);
    final LinearLayout llayout = (LinearLayout) findViewById(R.id.llayout);
    new Thread() {
        public void run() {
            while(true) {
                Thread.sleep(WAITTIME);
                currentColor++;
                if (currentColor > COLORS.length)
                    currentColor = 0;
                YourActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        llayout.setBackgroundColor(COLORS[currentColor]);
                    }
                });
            }
        }
    }.start();
}

这应该有效。使用此代码,您可以向数组COLORS添加其他颜色。

答案 2 :(得分:0)

取自this帖子并针对您的案例进行了编辑:

private int m_interval = 1000; // 1 second by default, can be changed later
private Handler m_handler;
private Layout mYourMainLayout //dont know what type of layout you use
private boolean which;

@Override
protected void onCreate(Bundle bundle)
{
  ...
  mYourMainLayout = (Layout) this.findViewById(R.id.yourMainLayout);
  m_handler = new Handler();
}

Runnable m_colorChanger = new Runnable()
{
     @Override 
     public void run() {
if(which){
         mYourMainLayout.setBackgroundColor(Color.GREEN)
         }
else {
   mYourMainLayout.setBackgroundColor(Color.RED)
    }
which=!which
          m_handler.postDelayed( m_colorChanger, m_interval);
     }
};

答案 3 :(得分:0)

你已经有了很多解决方案,但只是添加了我自己的......

您需要一个计时器

@Override
protected void onCreate(Bundle bundle)
{
   ...
   Timer timer = new Timer();
   TimerTask updateColor = new SwitchColorTask();
   timer.scheduleAtFixedRate(updateColor, 100, 100);
   ...
}

计时器任务。

private boolean colorSwitched;
class SwitchColorTask extends TimerTask {
   public void run() {
       LinearLayout main = (LinearLayout)findViewById(R.id.main);
       if(colorSwitched){
          main.setBackgroundColor (Color.GREEN)
       }
       else{
          main.setBackgroundColor (Color.RED)
       }           
       colorSwitched=!colorSwitched
   }
}

答案 4 :(得分:0)

此代码实现了颜色数组并且工作正常 - 非常感谢 (使用相同的清单和布局)     包biz.consett.mydraw;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

/* 
 * Simple use of Android Thread
 * Flash the screen between an array of colours at an interval 
 * 
 */

public class MainActivity extends Activity


static final int[] COLORS =
{AColor.RED,  AColor.BLUE, AColor.GREEN};// colour array
// Colour Red Green Blue 
// Index [0] [1] [2]

private int currentColor = 0;

private View MYVIEW = null;
//boolean whichColor = true;

final int interval = 100; // 0.6 second static final?

@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MYVIEW = (View) findViewById(R.id.my_view);

    MYVIEW.setBackgroundColor(Color.RED);// set initial colour

    new Thread(new Runnable()
    {

        // @Override
        public void run()
        {

            while (true)
            {
                try
                {
                    Thread.sleep(interval); // sleep for interval

                } 

                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                    updateColor();

            } // end of  while true loop


        }// end of run

    }).start(); // end of runnable

} // end of onCreate bundle

private void updateColor()
{

    runOnUiThread(new Runnable()
    {

        @Override
        public void run()
        {

            if (currentColor > COLORS.length - 1)
            { 
                currentColor = 0;

            }
            MYVIEW.setBackgroundColor(COLORS[currentColor]);

            currentColor++;

        }// end of run
    });

}
}// -------------END OF MainActivity extends Activity-----------------------

package biz.consett.mydraw;

public class AColor
{

final static int RED = 0xFFFF0000; // hex alpha_R_G_B 
final static int GREEN = 0xFF00FF00;
final static int BLUE = 0xFF0000FF; 
final static int PURPLE = 0xFFAA00AA;
}