Saturday, October 25, 2025
HomeLanguagesJavaHow to add KenBurns View in Android?

How to add KenBurns View in Android?

In this article, we will learn about how to add KenBurns View in android using java. KenBurns View is a useful library that is an extension of ImageView. It creates an immersive experience by animating its Drawable. We can use RandomTransitionGenerator to change the duration and acts as a interpolator of transitions. If we want to have more control over transition then we can implement our own TransitionGenerator.
Approach:

  1. Add the support Library in build.gradle file and add dependency in the dependencies section. Through this KenBurns view can be directly added in xml files and have many inbuilt functions to customize it easily.




    dependencies {         
          implementation 'com.flaviofaria:kenburnsview:1.0.7'      
    }         

    
    
  2. Now add the following code in the activity_main.xml file. In this file we add KenBurns View in our layout.

    activity_main.xml




    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        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"
        tools:context=".MainActivity">
      
        <com.flaviofaria.kenburnsview.KenBurnsView
            android:id="@+id/kView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/g"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
      
    </androidx.constraintlayout.widget.ConstraintLayout>

    
    
  3. Now add the following code in the MainActivity.java file. onClickListener is added with the kenBurns view. It makes the animation pause if it is in motion and vice versa.

    MainActivity.java




    package org.neveropen.gfganimatedGradient;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.AccelerateDecelerateInterpolator;
    import com.flaviofaria.kenburnsview.KenBurnsView;
    import com.flaviofaria.kenburnsview.RandomTransitionGenerator;
      
    public class MainActivity extends AppCompatActivity {
        KenBurnsView kenBurnsView;
        boolean moving = true;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            kenBurnsView = findViewById(R.id.kView);
      
            AccelerateDecelerateInterpolator interpolator
                = new AccelerateDecelerateInterpolator();
      
            // It is used to change the duration and
            // the interpolator of transitions
            RandomTransitionGenerator generator
                = new RandomTransitionGenerator(2000, interpolator);
            kenBurnsView.setTransitionGenerator(generator);
      
            kenBurnsView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    if (moving) {
                        kenBurnsView.pause();
                        moving = false;
                    }
                    else {
                        kenBurnsView.resume();
                        moving = true;
                    }
                }
            });
        }
    }

    
    
  4. Now compile and run the Android app.

Output:

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS