You are on page 1of 3

13. Develop an android application to make multiple blinking lights animation after pressing button.

JAVA code:
package com.example.practice;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button Blink;
ImageView bulb1,bulb2,bulb3,bulb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Blink=findViewById(R.id.blink);
bulb1=findViewById(R.id.bulb1);
bulb2=findViewById(R.id.bulb2);
bulb3=findViewById(R.id.bulb3);
bulb4=findViewById(R.id.bulb4);

Blink.setOnClickListener(new View.OnClickListener() {
@SuppressLint("WrongConstant")
@Override
public void onClick(View v) {
ObjectAnimator animator1=ObjectAnimator.ofInt(bulb1,"backgroundColor",
Color.YELLOW,Color.WHITE);
animator1.setDuration(500);
animator1.setEvaluator(new ArgbEvaluator());
animator1.setRepeatMode(Animation.REVERSE);
animator1.setRepeatCount(Animation.INFINITE);
animator1.start();
ObjectAnimator animator2=ObjectAnimator.ofInt(bulb2,"backgroundColor",
Color.YELLOW,Color.WHITE);
animator2.setDuration(500);
animator2.setEvaluator(new ArgbEvaluator());
animator2.setRepeatMode(Animation.REVERSE);
animator2.setRepeatCount(Animation.INFINITE);
animator2.start();
ObjectAnimator animator3=ObjectAnimator.ofInt(bulb3,"backgroundColor",
Color.YELLOW,Color.WHITE);
animator3.setDuration(500);
animator3.setEvaluator(new ArgbEvaluator());
animator3.setRepeatMode(Animation.REVERSE);
animator3.setRepeatCount(Animation.INFINITE);
animator3.start();
ObjectAnimator animator4=ObjectAnimator.ofInt(bulb4,"backgroundColor",
Color.YELLOW,Color.WHITE);
animator4.setDuration(500);
animator4.setEvaluator(new ArgbEvaluator());
animator4.setRepeatMode(Animation.REVERSE);
animator4.setRepeatCount(Animation.INFINITE);
animator4.start();

}
});

}
}

XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">

<ImageView
android:id="@+id/bulb1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/circle"/>
<ImageView
android:id="@+id/bulb2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/circle"/>
<ImageView
android:id="@+id/bulb3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/circle"/>
<ImageView
android:id="@+id/bulb4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/circle"/>
<Button
android:id="@+id/blink"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:text="Blink"/>

</LinearLayout>

XML code: circle.xml

<?xml version="1.0" encoding="utf-8"?>


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#78d9ff"/>
</shape>

You might also like