Adding transitions and playing videos in Android

Adding transitions and playing videos in Android

In Android, you can add transitions between activities to make the user interface more visually appealing. The platform provides various built-in animations such as slide, fade, zoom, and rotate, which can be applied to activities and views.

To add a transition animation between two activities, you can use the overridePendingTransition() method. This method should be called immediately after the startActivity() or finish() method is called. Here’s an example:

// Start the new activity

Intent intent = new Intent(this, SecondActivity.class);

startActivity(intent);

// Add a transition animation

overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

In this example, the slide_in_right and slide_out_left animations are applied to the new activity. These animations are defined as XML resources in the res/anim folder.

To display images in Android, you can use the ImageView class. This class provides various methods for loading and displaying images from different sources such as resources, files, and URLs. Here’s an example:

<!– Define an ImageView in a layout XML file –>

<ImageView

    android:id=”@+id/imageView”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:src=”@drawable/my_image” />

In this example, the src attribute of the ImageView specifies the image resource to be displayed. The my_image resource should be defined in the res/drawable folder.

To add animation to images, you can use the AnimationDrawable class. This class allows you to define a sequence of images as frames and animate them. Here’s an example:

<!– Define an ImageView for the animation in a layout XML file –>

<ImageView

    android:id=”@+id/animationView”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:background=”@drawable/my_animation” />

In this example, the background attribute of the ImageView specifies the animation resource to be displayed. The my_animation resource should be defined as an animation-list in the res/drawable folder.

To play videos in Android, you can use the VideoView class. This class provides various methods for loading and playing videos from different sources such as resources, files, and URLs. Here’s an example:

<!– Define a VideoView in a layout XML file –>

<VideoView

    android:id=”@+id/videoView”

    android:layout_width=”match_parent”

    android:layout_height=”wrap_content” />

<!– Load and play the video in a Java code –>

VideoView videoView = findViewById(R.id.videoView);

Uri videoUri = Uri.parse(“android.resource://” + getPackageName() + “/” + R.raw.my_video);

videoView.setVideoURI(videoUri);

videoView.start(); In this example, the setVideoURI() method of the VideoView is used to load the video from the my_video resource in the res/raw folder. The start() method is used to play the video.

Apply for Android Apps certification!

https://www.vskills.in/certification/certified-android-apps-developer

Back to Tutorials

Displaying images and animation in Android
Android Events

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?