Open In App

How to add KenBurns View in Android?

Last Updated : 18 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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: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.geeksforgeeks.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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads