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:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.    
  4. <alpha android:fromAlpha="0.0"  
  5.       android:toAlpha="1.0"  
  6.       android:interpolator="@android:anim/accelerate_interpolator"  
  7.       android:duration="600"  
  8.       android:repeatMode="reverse"  
  9.       android:repeatCount="infinite"/>  
  10. </set> 
Step 2
 
Create an XML file and enter the following in it:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.    
  7.   <!-- <TextView android:id="@+id/txtMessage"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="This is fadein animation"  
  11.         android:layout_centerInParent="true"  
  12.         android:textSize="25dp"  
  13.         android:padding="20dp"  
  14.         android:background="#000000"  
  15.         android:visibility="gone"/> -->  
  16.    
  17.   <ImageView  
  18.           android:id="@+id/imageview"  
  19.           android:layout_height="wrap_content"  
  20.           android:layout_width="wrap_content"  
  21.           android:background="@drawable/image1">  
  22.    
  23.   </ImageView>  
  24.    
  25.    
  26.   <Button android:id="@+id/btnStart"  
  27.       android:layout_width="wrap_content"  
  28.       android:layout_height="wrap_content"  
  29.       android:text="Start Animation"  
  30.       android:layout_marginTop="30dp"  
  31.       android:layout_alignParentBottom="true"  
  32.       android:layout_centerHorizontal="true"  
  33.       android:layout_marginBottom="20dp"/>  
  34.    
  35. </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().
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.animation.Animation;  
  5. import android.view.animation.Animation.AnimationListener;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9.    
  10. public class BlinkActivity extends Activity implements AnimationListener {  
  11.    
  12.        Button btnStart;  
  13.        Animation Blink;  
  14.    
  15.        @Override  
  16.        protected void onCreate(Bundle savedInstanceState) {  
  17.               // TODO Auto-generated method stub  
  18.               super.onCreate(savedInstanceState);  
  19.               setContentView(R.layout.activity_blink);  
  20.    final ImageView imageview=(ImageView)findViewById(R.id.imageview);  
  21.               //txtMessage = (TextView) findViewById(R.id.txtMessage);  
  22.               btnStart = (Button) findViewById(R.id.btnStart);  
  23.    
  24.               // load the animation  
  25.     Blink = AnimationUtils.loadAnimation(getApplicationContext(),  
  26.                            R.anim.blink);  
  27.                
  28.               // set animation listener  
  29.               Blink.setAnimationListener(this);  
  30.    
  31.    
  32.               btnStart.setOnClickListener(new View.OnClickListener() {  
  33.    
  34.                      @Override  
  35.                      public void onClick(View v) {  
  36.                            imageview.setVisibility(View.VISIBLE);  
  37.                            imageview.startAnimation(Blink);  
  38.                      }  
  39.               });  
  40.    
  41.        }  
  42.    
  43.        @Override  
  44.        public void onAnimationEnd(Animation animation) {  
  45.               if (animation == Blink) {  
  46.               }  
  47.        }  
  48.    
  49.        @Override  
  50.        public void onAnimationRepeat(Animation animation) {  
  51.    
  52.        }  
  53.    
  54.        @Override  
  55.        public void onAnimationStart(Animation animation) {  
  56.        }  
Step 4
 
When you run the application:
 
1.jpg
 
Step 5
 
When you click on the startAnimation button:
 
4.jpg
 
5.jpg
 
The image will blink untill you stop the application.


Similar Articles