Animation

By ukmodak | March 31st 2024 10:30:09 AM | viewed 816 times

create a main.xml layout and Add a button to show animation in the location: layout(android studio)

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="426dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Welcome to main activity "
                android:textColor="#ffffff"
                android:textSize="15sp" />

            <Button
                android:id="@+id/btnAnimation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/btnDatabaseSqlite"
                android:layout_below="@+id/btnDatabaseSqlite"
                android:text="Fade in transition"
                android:textSize="8sp" />
          

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

create a ProjectonOverallClassMainActivity activity to animation

package com.uk.modak;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ProjectonOverallClassMainActivity extends Activity {
   
	
	public Button btnAnimation;	
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
         btnAnimation = (Button) findViewById(R.id.btnAnimation);          
        			
			 btnAnimation.setOnClickListener(new View.OnClickListener() {
		
				public void onClick(View v) {				
					Intent d = new Intent(ProjectonOverallClassMainActivity.this,AnimationActivity.class);
					startActivity(d);
				}
          });                      
    }
    
    public void onStart(){
    	super.onStart();
    	 	
    }
      
    
}

to to drawable folder and keet all image

eight.jpg
five.jpg
four.jpg
ic_launcher.png
one.jpg
six.jpg
three.jpg
two.jpg

create a animationlayout.xml layout for animation in the location: layout(android studio)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >



    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/one" />

</LinearLayout>

create an activity in the location: src/main/java/com/uk/modak/AnimationActivity (android studio)


package com.uk.modak;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class AnimationActivity extends Activity {
	
	private ImageView imageView;
	private int[] imageArray;
    private int currentIndex;
    private int startIndex;
    private int endIndex;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animationlayout);
        
        imageView = (ImageView)findViewById(R.id.imageView);
       
        
        imageArray = new int[8];
        imageArray[0] = R.drawable.one;
        imageArray[1] = R.drawable.two;
        imageArray[2] = R.drawable.three;
        imageArray[3] = R.drawable.four;
        imageArray[4] = R.drawable.five;
        imageArray[5] = R.drawable.six;
        imageArray[6] = R.drawable.eight;

        startIndex = 0;
        endIndex = 7;
        nextImage();
        
        
    }
    
    public void nextImage(){
    	imageView.setImageResource(imageArray[currentIndex]);
        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        imageView.startAnimation(rotateimage);
        currentIndex++;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if(currentIndex>endIndex){
                    currentIndex--;
                    previousImage();
                }else{
                    nextImage();
                }

            }
        },1000); // here 1000(1 second) interval to change from current  to next image  

    }
    public void previousImage(){
    	imageView.setImageResource(imageArray[currentIndex]);
        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);
        imageView.startAnimation(rotateimage);
        currentIndex--;
        new Handler().postDelayed(new Runnable() {
            
            public void run() {
                if(currentIndex

to to menifeast and setup activity that we use

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ProjectonOverallClassMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <activity  
            android:name=".AnimationActivity"
            android:label="@string/app_name" >
        </activity>
        
        <receiver android:name=".ConnectionReceive">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
        
    </application>
bONEandALL
Visitor

Total : 26654

Today :3

Today Visit Country :

  • France
  • United States