5

我想在我的Android代码中的某处创建一个循环,以某种速率连续改变两种颜色之间的可绘制矩形的颜色。我想用两个按钮开始和停止它的闪烁。我做了很多研究,但似乎无法弄清楚如何去做。我是 android 新手,没有使用 run() 方法的经验。但我猜我必须使用 run() 方法制作某种矩形类,将其动画化为改变颜色。

4

1 回答 1

2

我对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 -->
     />

现在,您将拥有 2 个 ImageViews:blue_rectanglered_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" />

下一部分是棘手的部分。

这里是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.

}

}

这是代码的基本作用。

它有一个布尔值,默认为 false。虽然它是假的,但矩形不会闪烁。虽然这是真的,但运行中的for语句blinkRectangle()。该for循环使蓝色可见,等待 2 秒,使其不可见,使红色可见,等待 2 秒,然后重复。start()和方法将stop()布尔值分别切换为 true 和 false。我认为这种类型的for循环在返回顶部时会重新检查布尔值。我以前从未使用过它。这就是我可以从我查看的网站收集到的信息。

好吧,我认为这可以做到。如果您不了解任何代码的作用,或者它不起作用,或者我的问题错了,或者我完全错了,或者任何事情,请评论这个答案。我希望这行得通!

祝你好运!

PS这里是我用来参考的网站

http://www.tutorialspoint.com/java/java_loop_control.htm

http://www.java-examples.com/java-boolean-example

哇...我刚刚意识到这个问题已经 2 岁了。不过,我希望这对你有帮助!

于 2014-05-25T18:16:52.523 回答