我是 android 的初学者,手头有一些 java。我只完成了制作我的第一个应用程序教程。
接下来我想制作一个应用程序来使用简单的 2D 图形
首先,我想找到一种在无限循环中在两种颜色(红色和绿色)之间闪烁屏幕的方法
wait int seconds;
make screen red;
wait int seconds;
make screen green;
loop forever;
任何人都可以向我指出可能有帮助的教程或源代码吗?
非常感谢
您如何制作一个充满屏幕的布局,让我们称之为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
。
感谢您的输入,此代码似乎可以正常工作
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>
摘自这篇文章并针对您的案例进行了编辑:
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);
}
};
你已经有了很多解决方案,但只是添加我自己的......
你需要一个计时器
@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
}
}
此代码实现了颜色数组并且工作正常 - 非常感谢。
(使用相同的清单和布局)包 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;
}