Fade In और Fade Out एनिमेशन
Fade in और fade out एनिमेशन अल्फा एनिमेशन क्लास पर आधारित काम करती है। यह उदाहरण दर्शाता है कि Java में Android Fade In और Fade Out एनिमेशन का उपयोग कैसे करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML जोड़ें
निम्न कोड को res/layout/login.xml (या activity_main.xml) में जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<Button
android:id="@+id/fadeIn"
android:text="Fade In"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/fadeOut"
android:text="Fade Out"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ऊपर के कोड में, हमने एक ImageView और दो बटन लिए हैं। FadeIn बटन ImageView को फेड इन एनिमेशन देगा और FadeOut बटन फेड आउट एनिमेशन देगा।
चरण 3: MainActivity जावा कोड जोड़ें
निम्न कोड को src/MainActivity.java में जोड़ें:
package com.example.andy.myapplication;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.imageView);
Button fadeIn = findViewById(R.id.fadeIn);
fadeIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(1000);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
imageView.startAnimation(alphaAnimation);
}
});
Button fadeOut = findViewById(R.id.fadeOut);
fadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0);
fadeOut.setDuration(2000);
fadeOut.start();
}
});
}
}
Fade In एनिमेशन कोड
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(1000); alphaAnimation.setRepeatCount(1); alphaAnimation.setRepeatMode(Animation.REVERSE); imageView.startAnimation(alphaAnimation);
Fade Out एनिमेशन कोड
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0); fadeOut.setDuration(2000); fadeOut.start();
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें:

अब Fade In बटन पर क्लिक करें, यह ImageView को एनिमेशन देगा:

अब Fade Out बटन पर क्लिक करें, यह ImageView को फेड आउट एनिमेशन देगा:

दोनों तरीकों का अंतर
- AlphaAnimation (View Animation): पुराना API, व्यू के ड्रॉइंग को प्रभावित करता है, लेआउट नहीं बदलता।
- ObjectAnimator (Property Animation): नया API (API 11+), वास्तविक प्रॉपर्टी मान बदलता है, बेहतर प्रदर्शन।
- आधुनिक ऐप्स के लिए
ObjectAnimatorयाViewPropertyAnimator(animate()) उपयोग करने की सलाह दी जाती है।