XML Animation Resource ব্যবহার করে ছবি রোটেট করা
এই টিউটোরিয়ালে দেখানো হয়েছে কিভাবে res/anim/ ফোল্ডারে animation XML ফাইল তৈরি করে একটি ImageView-სmooth rotation animation অ্যাপ্লাই করা যায়।
ধাপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট শুরু করুন।
ধাপ ২: লেআউট (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" />
<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>
ধাপ ৩: Animation XML (res/anim/rotate.xml)
res ফোল্ডারের ওপর রাইট ক্লিক করে New → Android Resource Directory নির্বাচন করুন, Resource type: 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" />
</set>
ধাপ ৪: MainActivity.java
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;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button btnRotate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
btnRotate = findViewById(R.id.btnRotate);
btnRotate.setOnClickListener(v -> {
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
imageView.startAnimation(rotation);
});
}
}
Animation XML অ্যাট্রিবিউট ব্যাখ্যা
| অ্যাট্রিবিউট | ব্যাখ্যা |
|---|---|
| fromDegrees | শুরুর কোণ (০ ডিগ্রি) |
| toDegrees | শেষ কোণ (৩৬০ ডিগ্রি = পূর্ণ ঘূর্ণন) |
| pivotX / pivotY | ঘূর্ণন কেন্দ্র (৫০% = ছবির মাঝপথ) |
| duration | অ্যানিমেশন সময় মিলিসেকেন্ডে (১০০০০ = ১০ সেকেন্ড) |
ফলাফল
"ঘুরান" বাটনে ট্যাপ করলে ছবিটি ১০ সেকেন্ড ধরে সুস্পষ্টভাবে ৩৬০ ডিগ্রি ঘুরবে।