Android Animation : Fading Animation
Animation Enhances the beauty of any application. In this tutorial we will learn about android view animation. We will create fade in and fade out animation.
Anim Directory:
First of all create anim resource directory in resources. And create two file named as fade_in.xml and fade_out.xml.
Codes:
1. fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
2. fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
</set>
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<ImageView
android:id="@+id/logo1"
android:layout_width="400dp"
android:layout_height="200dp"
android:src="@drawable/logo"/>
<Button
android:id="@+id/fadein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FADE IN"
android:textSize="30dp"
android:textColor="@color/blue"
android:padding="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/capsule"/>
<Button
android:layout_marginTop="20dp"
android:id="@+id/fadeout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FADE OUT"
android:textSize="30dp"
android:textColor="@color/blue"
android:padding="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/capsule"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4. ActivityMain.java
package com.bharat_putra_tech.testapp;
import androidx.appcompat.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;
public class MainActivity extends AppCompatActivity {
Animation fade_in,fade_out;
Button fadeIN,fadeOUT;
ImageView logo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo = findViewById(R.id.logo1);
fadeIN = findViewById(R.id.fadein);
fadeOUT = findViewById(R.id.fadeout);
fadeIN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fade_in = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
logo.setVisibility(View.VISIBLE);
logo.startAnimation(fade_in);
}
});
fadeOUT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fade_out = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
logo.setVisibility(View.VISIBLE);
logo.startAnimation(fade_out);
}
});
}
}
Result :
Video Tutorial:
No comments:
Post a Comment