Blink Activity in Android Studio

Introduction

This article explains Blink Animation in Android Studio.

In this article you will first use an imageview and a button in an XML file. The Imageview is used to show the blink activity and the button is used to start the blink animation. So in the output when you click on the Start Animation button the animation on the Imageview will start.

To provide the blink activity to the ImageView you will create an XML file inside the anim folder that is present inside the res folder.

So in this article you first use an Imageview and a Button in an XML file. Create the instance of an imageview and the button in a Java file. Now declare an instance of the animation class on which you will load the animation by calling the loadAniamtion() method of the AndroidUtills class. Now set the animation on the setOnClickListener() method to set the animation. Set the button on its clickListener to start the animation on a button click. Inside the click event you will set the imageview to visible and startAniamtion().

Step 1

The XML file inside the anim folder is as in the following:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 
<
alpha android:fromAlpha="0.0"
      android:toAlpha="1.0"
      android:interpolator="@android:anim/accelerate_interpolator"
      android:duration="600"
      android:repeatMode="reverse"
     
android:repeatCount="infinite"/>

</set>


Step 2

Create an XML file and enter the following in it:

 

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

  <!-- <TextView android:id="@+id/txtMessage"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="This is fadein animation"

        android:layout_centerInParent="true"

        android:textSize="25dp"

        android:padding="20dp"

        android:background="#000000"

        android:visibility="gone"/> -->

 

  <ImageView

          android:id="@+id/imageview"

          android:layout_height="wrap_content"

          android:layout_width="wrap_content"

          android:background="@drawable/image1">

 

  </ImageView>

 

 

  <Button android:id="@+id/btnStart"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="Start Animation"

      android:layout_marginTop="30dp"

      android:layout_alignParentBottom="true"

      android:layout_centerHorizontal="true"

      android:layout_marginBottom="20dp"/>

 

</RelativeLayout>

 
Step 3

So in this article you first use an Imageview and a Button in an XML file. Create the instance of the imageview and button in a Java file. Now declare an instance of the animation class to load the animation by calling the loadAniamtion() method of the AndroidUtills class. Now set the animation on the setOnClickListener() method to set the animation. Set the button on its clickListener to start the animation on a button click. Inside it on its click event you will set the imageview to visible and startAniamtion().

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.animation.Animation;

import android.view.animation.Animation.AnimationListener;

import android.view.animation.AnimationUtils;

import android.widget.Button;

import android.widget.ImageView;

 

public class BlinkActivity extends Activity implements AnimationListener {

 

       Button btnStart;

       Animation Blink;

 

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              // TODO Auto-generated method stub

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_blink);

   final ImageView imageview=(ImageView)findViewById(R.id.imageview);

              //txtMessage = (TextView) findViewById(R.id.txtMessage);

              btnStart = (Button) findViewById(R.id.btnStart);

 

              // load the animation

    Blink = AnimationUtils.loadAnimation(getApplicationContext(),

                           R.anim.blink);

             

              // set animation listener

              Blink.setAnimationListener(this);

 

 

              btnStart.setOnClickListener(new View.OnClickListener() {

 

                     @Override

                     public void onClick(View v) {

                           imageview.setVisibility(View.VISIBLE);

                           imageview.startAnimation(Blink);

                     }

              });

 

       }

 

       @Override

       public void onAnimationEnd(Animation animation) {

              if (animation == Blink) {

              }

       }

 

       @Override

       public void onAnimationRepeat(Animation animation) {

 

       }

 

       @Override

       public void onAnimationStart(Animation animation) {

       }

}

 

Step 4

When you run the applaication:

1.jpg

 Step 5

When you click on the startAnimation button:

4.jpg


5.jpg

The image will blink untill you stop the application.

Up Next
    Ebook Download
    View all
    Learn
    View all