Android color animation

I would like to create a loop somewhere in my Android code that changes the color of a drawn rectangle between two colors continuously at a certain speed. I would like to start and stop it blinking with two buttons. I have done a lot of research, but just can't figure out how to do this. I am new to android and have no experience with run () methods. But I guess I need to make some kind of rectangle class using the run () method, which will animate it in a color change.

+5
source share
1 answer

I am also quite new to Android, but I will give him a chance.

, , , , , , "". , 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_rectangle red_rectangle, . XML ImageViews

<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 , , , . start() stop() true false . , for , . . , -, .

, , . , - , , , , , . , !

!

P.S. ,

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

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

... , 2 . , , !

+1

All Articles