অ্যান্ড্রয়েডে শেক/ওবল অ্যানিমেশন
এই উদাহরণটি দেখায় কীভাবে অ্যান্ড্রয়েডে ভিউতে শেকিং বা ওবল অ্যানিমেশন দেখানো যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project-এ যান এবং প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইলে কোড যোগ করুন
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="ক্লিক করুন"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher_round"
android:layout_width="match_parent"
android:layout_height="300dp" />
</RelativeLayout>
এখানে একটি বাটন এবং একটি ImageView রয়েছে। বাটনে ক্লিক করলে ImageView-এ শেক অ্যানিমেশন হবে।
ধাপ ৩: MainActivity.java-তে কোড যোগ করুন
package com.example.andy.myapplication;
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 view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.imageView);
findViewById(R.id.button).setOnClickListener(v -> {
Animation animShake = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake);
view.startAnimation(animShake);
});
}
}
ধাপ ৪: anim/shake.xml তৈরি করুন
res/anim/shake.xml ফাইলটি তৈরি করে নিচের কোড যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate
android:duration="150"
android:fromXDelta="-10%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="10%"/>
</set>
এই অ্যানিমেশনটি ভিউকে X-অক্ষে -10% থেকে +10% পর্যন্ত ৫ বার রিভার্স মোডে সরিয়ে শেক ইফেক্ট তৈরি করে।
অ্যাপ্লিকেশন চালান
অ্যাপটি চালান। "ক্লিক করুন" বাটনে ক্লিক করলে ইমেজ ভিউ শেক করবে।