यह उदाहरण Android में सेंटर पॉइंट के सापेक्ष इमेज स्केल एनिमेशन के बारे में है।
इमेज स्केल (जूम) एनिमेशन का उदाहरण
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और आवश्यक विवरण भरें।
चरण 2 - लेआउट XML में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:layout_width="150dp"
android:text="Click"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher_round"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
उपरोक्त कोड में, हमने इमेज स्केलिंग एनिमेशन (जूम एनिमेशन) दिखाने के लिए बटन लिया है।
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ImageView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.imageView);
view.setVisibility(View.INVISIBLE);
findViewById(R.id.button).setOnClickListener(v -> {
ScaleAnimation fade_in = new ScaleAnimation(
0f, 1f, 0f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
fade_in.setDuration(1000);
fade_in.setFillAfter(true);
view.startAnimation(fade_in);
});
}
}
जब उपयोगकर्ता बटन पर क्लिक करता है, तो यह निम्नलिखित कोड का उपयोग करके इमेज स्केलिंग एनिमेशन दिखाएगा:
ScaleAnimation fade_in = new ScaleAnimation(
0f, 1f, 0f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
fade_in.setDuration(1000);
fade_in.setFillAfter(true);
view.startAnimation(fade_in);
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें और ऐप चलाएं। बटन पर क्लिक करने पर, इमेज जूमिंग इफेक्ट के साथ दिखाई देगी।