एंड्रॉइड में XML एनिमेशन से स्मूथ इमेज रोटेशन
यह उदाहरण दिखाता है कि res/anim/ फोल्डर में XML एनिमेशन परिभाषित करके इमेज को स्मूथली कैसे घुमाया जाता है।
चरण 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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:src="@drawable/your_image"
android:contentDescription="घूमने वाली इमेज" />
<Button
android:id="@+id/btnRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:layout_marginBottom="10dp"
android:text="घुमाएं" />
</RelativeLayout>
चरण 3: एनिमेशन XML बनाएं
res/anim/rotate.xml फोल्डर और फाइल बनाएं:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="10000"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator" />
</set>
मुख्य विशेषताएं:
pivotX="50%"औरpivotY="50%"- केंद्र बिंदु पर घुमावduration="10000"- 10 सेकंड में पूरा चक्रrepeatCount="infinite"- अनंत बार दोहराएंlinear_interpolator- समान गति (कोई त्वरण/मंदी नहीं)
चरण 4: MainActivity में एनिमेशन लोड करें
MainActivity.java:
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 {
private ImageView imageView;
private Button btnRotate;
private Animation rotateAnimation;
private boolean isRotating = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
btnRotate = findViewById(R.id.btnRotate);
// एनिमेशन लोड करें
rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
btnRotate.setOnClickListener(v -> toggleRotation());
}
private void toggleRotation() {
if (isRotating) {
imageView.clearAnimation();
btnRotate.setText("घुमाएं");
} else {
imageView.startAnimation(rotateAnimation);
btnRotate.setText("रोकें");
}
isRotating = !isRotating;
}
@Override
protected void onDestroy() {
super.onDestroy();
imageView.clearAnimation(); // मेमोरी लीक रोकें
}
}
चरण 5: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
उन्नत: प्रोग्रामेटिकली एनिमेशन (ObjectAnimator)
अधिक नियंत्रण के लिए ObjectAnimator का उपयोग करें:
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f); animator.setDuration(10000); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setInterpolator(new LinearInterpolator()); animator.start(); // रोकने के लिए: animator.cancel();
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। "घुमाएं" बटन दबाने पर इमेज स्मूथली घूमना शुरू करेगी।