Blinking Image Animation on the ImageView

Home/Android/Blinking Image Animation on the ImageView

Blinking Image Animation on the ImageView




This post is about blinking the image on the ImageView, which is useful for apps using animations. This can be a good image blinking animation to make an image automatically blink without using any touch which is used in Android App animation these days. Animations are playing the most important part in android application these days it has its application in Gaming, the opening of the apps and many more.  We present a small demo of moving image animation which rotates the image according to the user specifications. You can also use button that can work as a click to start your animation which I will show you in the next article. For now I am not using any button to start the animation, the animation starts itself whenever the application launches. 

In this example, I’ll try to introduce you doing simple animations with Android. It is simple enough that no need to click any button, an images starts blinking from full to fade degree in seconds the time you specify. You can also change the time and speed of blinking not necessarily going from full to fade degree.You can also specify number of blink that you want your image to blink. Lets begin with the animation XML. Once you have created a new Android project, create a folder named “anim” in res and a file named ‘blink.xml’ inside res/anim.   Everything is  explained in detail below.

NOTE :  This project is developed in Android Studio, and tested in Android Marshmallow 6.0 .

Follow the steps below to blink an image automatically:

  • Create a new project with any minimum SDK of your choice.
  • Copy and paste an image which you want to have on your ImageView in the drawable folder (I named the image image.jpg)

 

image

 

  • Create an ImageView in the activity_main.xml and display the image. See the following code to create the imageview.

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/screen"
    tools:context=".MainActivity" >


    <ImageView
        android:id="@+id/icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/image" />

</RelativeLayout>

 

  • In res folder, Create New Directory with name anim.
  • In anim directory, Create a new XML file with name blink and add the following code in it

 

blink

 

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000" >
    </alpha>

   <alpha
        android:startOffset="2000"
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="2000" >
    </alpha>

</set>

 

  • Now, add the final code in your main file i.e MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    ImageView icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        icon = (ImageView) findViewById(R.id.icon);


        Animation animBlinkIn_icon = AnimationUtils.loadAnimation(this, R.anim.blink);
        icon.startAnimation(animBlinkIn_icon);


    }
}

Run the application

2017-04-02 (3) 2017-04-02 (4) 2017-04-02 (5)

 

Try out these simple animations and we will bring you a bunch of animations more like Moving Image Animation, Zooming Image Animation, etc. to create a perfect animated app. You can also have a look at other interesting android projects like Slider making, some basic day to day hacks that are very useful. You can also subscribe to our newsletter so you do not miss any updates and you can also put up your Queries on the Contact us page.

 

By | 2017-05-22T19:49:26+00:00 December 27th, 2016|Android|1 Comment

About the Author:

One Comment

  1. Abhinav December 27, 2016 at 5:32 pm - Reply

    That was helpful !!!

Leave A Comment

2 × 4 =

Avada WordPress Theme