Introduction
In this blog, you will learn how to create View Animator apps in Android, using Android Studio. Now, more and more applications are mainly used for animation because the animation is a good graphical user interface.
Requirements
- Windows 10 (OS is free available source in online (https//www.microsoft.com/en-in/software-download/windows10).
- Android Studio 2.1.3 (free source available in online (https//developer.android.com/studio/index.html?gclid=CNP3qbH5htECFdiKaAodeFIKtA)
If you want develop ViewAnimator app, it should follow the steps given below.
Step 1
First of all, you will open Android Studio and you can go to the file. Select New and click NewProject.
Step 2
Now, you will write the Application's name and choose a file saving location. Afterwards, you click the Next button.
Step 3
Now, you will click select the form factors and your app will run on the different platforms which may require separate SDKs, which is like Phone and Tablet Minimum SDK (API 16Android 4.1(Jellybean)) and afterwards, you will click the Next button.
Step 4
Now, you will choose Activity, which is like (Basic Activity,Empty activity, extra) and click the next button.
Here, you will write the activity name and click finish button.
Step 5
Now, open your project and you will go to activity_main.xml and afterwards, build the design. You should choose the toolbox, if you want some options (ViewAnimator, button) and use the drag and drop method.
Here, you will see Graphical User Interface page
Step 6
Now, you will click the MainActivity.java page and you will build Java code.
First of all, you will declare the Header file (Extension file).
Here, you will build the full Java code in MainActivity.java.
- package xyz.rvconstructions.www.viewanimatorapp;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.ViewAnimator;
- public class MainActivity extends AppCompatActivity {
- private ViewAnimator firstViewAnimator;
- Button btnNext;
- int[] images = {
- R.drawable.imgfirst,
- R.drawable.imgsecond,
- R.drawable.imgthird
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnNext = (Button) findViewById(R.id.buttonNext);
- firstViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator);
- for (int i = 0; i < images.length; i++) {
- ImageView imageView = new ImageView(getApplicationContext());
- imageView.setImageResource(images[i]);
- firstViewAnimator.addView(imageView);
- }
- Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
- Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
- firstViewAnimator.setInAnimation( in );
- firstViewAnimator.setOutAnimation(out);
- firstViewAnimator.setAnimateFirstView(false);
- btnNext.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- firstViewAnimator.showNext();
- }
- });
- }
- }
Step 7
Now, you will see XML code, which is like activity_main.xml.- <?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" tools:context="xyz.rvconstructions.www.viewanimatorapp.MainActivity">
- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:textColor="#ff6600" android:layout_marginTop="5dp" android:text="C# corner" />
- <ViewAnimator android:id="@+id/simpleViewAnimator" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp"> </ViewAnimator>
- <Button android:id="@+id/buttonNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="200dp" android:background="#055" android:text="NEXT" android:textColor="#fff" android:textStyle="bold" /> </RelativeLayout>
Step 8
Now, you will go to run and select Run'app'.
Here, you will choose emulators and devices. Afterwards, you will click OK button.
Step 9
Now, you will see the output.
Now, you will click the next button and ViewAnimator changes.