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:
- 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'Â Â Â Â Â Â}Â Â Â Â Â Â Â Â Â - Now add the following code in the activity_main.xml file. In this file we add KenBurns View in our layout.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout   Â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> - 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
packageorg.neveropen.gfganimatedGradient;ÂÂimportandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.animation.AccelerateDecelerateInterpolator;importcom.flaviofaria.kenburnsview.KenBurnsView;importcom.flaviofaria.kenburnsview.RandomTransitionGenerator;ÂÂpublicclassMainActivityextendsAppCompatActivity {   ÂKenBurnsView kenBurnsView;   Âbooleanmoving =true;   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       ÂkenBurnsView = findViewById(R.id.kView);       ÂAccelerateDecelerateInterpolator interpolator           Â=newAccelerateDecelerateInterpolator();       Â// It is used to change the duration and       Â// the interpolator of transitions       ÂRandomTransitionGenerator generator           Â=newRandomTransitionGenerator(2000, interpolator);       ÂkenBurnsView.setTransitionGenerator(generator);       ÂkenBurnsView.setOnClickListener(newView.OnClickListener() {           Â@Override           ÂpublicvoidonClick(View v)           Â{               Âif(moving) {                   ÂkenBurnsView.pause();                   Âmoving =false;               Â}               Âelse{                   ÂkenBurnsView.resume();                   Âmoving =true;               Â}           Â}       Â});   Â}} - Now compile and run the Android app.
Output:
