0% found this document useful (0 votes)
58 views4 pages

Develop An Android Application To Make A LED Light Set in Which Single LED Light Blinks Randomly Out of Available 5 Lights. JAVA Code

The document describes the code for an Android application that creates a LED light simulation with 5 lights. When a button is clicked, one of the 5 image views representing lights will blink randomly by changing color for 2 seconds using animations before stopping. The code includes Java code to set up the onclick listener and animations, as well as XML code for the app layout and light image resource.

Uploaded by

Sachin Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

Develop An Android Application To Make A LED Light Set in Which Single LED Light Blinks Randomly Out of Available 5 Lights. JAVA Code

The document describes the code for an Android application that creates a LED light simulation with 5 lights. When a button is clicked, one of the 5 image views representing lights will blink randomly by changing color for 2 seconds using animations before stopping. The code includes Java code to set up the onclick listener and animations, as well as XML code for the app layout and light image resource.

Uploaded by

Sachin Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

15.

Develop an android application to make a LED light set in which single LED light blinks randomly out
of available 5 lights.

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

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity {


Button Blink;
ImageView bulb1,bulb2,bulb3,bulb4, bulb5;
@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);
bulb5=findViewById(R.id.bulb5);

Blink.setOnClickListener(new View.OnClickListener() {
@SuppressLint("WrongConstant")
@Override
public void onClick(View v) {

final int min = 1;


final int max = 5;
final int i = new Random().nextInt((max - min) + 1) + min;
switch (i) {
case 1: final 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();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
animator1.removeAllListeners();
animator1.end();
animator1.cancel();

}
}, 2000);
break;
case 2: final 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();
Handler handler4 = new Handler();
handler4.postDelayed(new Runnable() {
@Override
public void run() {
animator2.removeAllListeners();
animator2.end();
animator2.cancel();
}
}, 2000);
break;
case 3: final 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();
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
animator3.removeAllListeners();
animator3.end();
animator3.cancel();

}
}, 2000);
break;
case 4: final 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();
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
animator4.removeAllListeners();
animator4.end();
animator4.cancel();
}
}, 2000);
break;
case 5: final ObjectAnimator animator5 = ObjectAnimator.ofInt(bulb5,
"backgroundColor", Color.YELLOW, Color.WHITE);
animator5.setDuration(500);
animator5.setEvaluator(new ArgbEvaluator());
animator5.setRepeatMode(Animation.REVERSE);
animator5.setRepeatCount(Animation.INFINITE);
animator5.start();
Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
@Override
public void run() {
animator5.removeAllListeners();
animator5.end();
animator5.cancel();
}
}, 2000);
break;
}
}
});

}
}

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">

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

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