我对 Android 也很陌生,但我会尝试一下。
既然您说希望它闪烁,那么您应该能够使用简单的“for”循环在蓝色和红色之间切换实际图像。按下按钮时,您可以将布尔值的状态从 false 更改为 true。然后,当“for”语句不再为真时,它会跳转到下一组代码,从而停止它。这就是我要做的。
两个按钮的 XML:
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
android:Clickable="true"
android:onClick="start"
/>
<Button
android:id="@+id/stop" <!-- Gives the button an ID to use in java code -->
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop" <!-- sets the text on the button -->
android:Clickable="true" <!-- makes the button clickable -->
android:onClick="stop" <!-- The method it calls when it is clicked, removes the necessity of an OnClickListener -->
/>
Run Code Online (Sandbox Code Playgroud)
现在,您将有 2 个 ImageView:blue_rectangle和red_rectangle,它们都位于布局中的同一位置。这是两个 ImageView 的 XML
<ImageView
android:id="@+id/blue_rectangle"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:src="@drawable/blue_rectangle" />
<ImageView
android:id="@+id/red_rectangle"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="5dp"
android:src="@drawable/red_rectangle" />
Run Code Online (Sandbox Code Playgroud)
下一部分是棘手的部分。
这里是Java。
package your.package.name.thingy.here;
//everything it tells you to import goes here
public class BlinkingActivity extends Activity{
ImageView blueRectangle;
ImageView redRectangle;
Button start;
Button stop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
blueRectangle = (ImageView) findViewById(R.id.blue_rectangle);
redRectangle = (ImageView) findViewById(R.id.red_rectangle);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
Boolean isBlinking = new Boolean(false);
blinkRectangle(whateverVariableThisNeeds);
}
public static void blinkRectangle(View view){
blueRectangle.setVisibility(View.INVISIBLE);
redRectangle.setVisibility(View.INVISIBLE);
for(initialization; isBlinking; update){
blueRectangle.setVisibility(View.VISIBLE);
blueRectangle.postDelayed(new Runnable() {
@Override
public void run() {
blueRectangle.setVisibility(View.INVISIBLE);
}
}, 2000); //the 2000 is the number of milliseconds for how long blue is visible
redRectangle.setVisibility(View.VISIBLE);
redRectangle.postDelayed(new Runnable() {
@Override
public void run(){
redRectangle.setVisibility(View.INVISIBLE);
}
}, 2000);
blueRectangle.setVisibility(View.VISIBLE); //This prevents a bug where if the user hits the stop button at just the right time, both rectangles will be invisible.
}
public static void start(View view){ //no need to call this anywhere, the XML onClick does it for you
isBlinking = true; //I think this is how you set a boolean, if not, correct me.
}
public static void stop(View view){//same here
isBlinking = false; //again, correct me if I'm wrong.
}
}
Run Code Online (Sandbox Code Playgroud)
这是代码的基本功能。
它有一个布尔值,默认为 false。虽然它是假的,但矩形不会闪烁。虽然这是真的,但该for声明仍在blinkRectangle()运行。该for循环使蓝色可见,等待 2 秒,使其不可见,使红色可见,等待两秒,然后重复。和start()方法stop()分别将布尔值切换为 true 和 false。我认为这种类型的for循环在返回顶部时会重新检查布尔值。我以前从未使用过它。这就是我从我浏览的网站上收集到的信息。
嗯,我想差不多就这样了。如果您不明白任何代码的作用,或者它不起作用,或者我的问题错误,或者我完全错误,或者任何事情,请对此答案发表评论。我希望这能起作用!
祝你好运!
PS以下是我参考的网站
http://www.tutorialspoint.com/java/java_loop_control.htm
http://www.java-examples.com/java-boolean-example
哇...我刚刚意识到这个问题已经有两年了。尽管如此,我还是希望这对你有帮助!