Android में स्मूथ इमेज रोटेशन एनिमेशन
यह उदाहरण दर्शाता है कि Android में स्मूथ इमेज रोटेशन कैसे बनाएं।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2 - activity_main.xml लेआउट
res/layout/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">
<TextView
android:id="@+id/text"
android:textSize="18sp"
android:textAlignment="center"
android:text="इमेज घुमाएं"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/rotateImage"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layerType="software" />
</LinearLayout>
उपरोक्त कोड में, हमने एक टेक्स्ट व्यू और इमेज व्यू लिया है। जब आप टेक्स्ट व्यू पर क्लिक करते हैं, तो इमेज एंटी-क्लॉक दिशा में घूम जाएगी।
चरण 3 - MainActivity.java
src/MainActivity.java में निम्न कोड जोड़ें:
package com.example.andy.myapplication;
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.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
TextView text;
ImageView image;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
text = findViewById(R.id.text);
image = findViewById(R.id.rotateImage);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RotateAnimation rotate = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(5000);
rotate.setInterpolator(new LinearInterpolator());
image.startAnimation(rotate);
}
});
}
}
उपरोक्त कोड में, हमने RotateAnimation क्लास का उपयोग किया है। यह Android में एक पूर्वनिर्धारित क्लास है। नीचे दिए गए कोड का उपयोग करके इमेज को घुमाएं:
RotateAnimation rotate = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotate.setDuration(5000); rotate.setInterpolator(new LinearInterpolator()); image.startAnimation(rotate);
आइए अपने एप्लिकेशन को चलाने का प्रयास करें। मान लें कि आपने अपने वास्तविक Android मोबाइल डिवाइस को कंप्यूटर से जोड़ा है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। विकल्प के रूप में अपना मोबाइल डिवाइस चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा।

उपरोक्त कोड में, यह प्रारंभिक स्क्रीन दिखाता है जब उपयोगकर्ता टेक्स्ट व्यू पर क्लिक करता है, तो यह नीचे दिखाए अनुसार इमेज को घुमाएगा:
