অ্যান্ড্রয়েডে ImageView-এ অনুপাত রক্ষা করে ছবি স্কেল করার পদ্ধতি
এই উদাহরণটি দেখায় কিভাবে Android-এ ImageView-এ একটি ছবি স্কেল করতে হয় যাতে এর অনুপাত (aspect ratio) অক্ষুণ্ণ থাকে।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML-এ কোড যোগ করুন
res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/my_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
গুরুত্বপূর্ণ অ্যাট্রিবিউট:
android:adjustViewBounds="true"— ছবির বাউন্ডস সামঞ্জস্য করে অনুপাত রক্ষা করে।android:scaleType="fitCenter"— ছবিটি কন্টেইনারের মধ্যে ফিট করে কেন্দ্রে রাখে, অনুপাত বজায় রেখে। (পূর্বেরfitXYঅনুপাত ভাঙে।)
ধাপ ৩: MainActivity.java-এ কোড যোগ করুন
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView myImage = findViewById(R.id.my_image);
myImage.setImageResource(R.drawable.flower); // setBackgroundResource-এর পরিবর্তে setImageResource ব্যবহার করুন
}
}
ধাপ ৪: AndroidManifest.xml
ম্যানিফেস্ট ফাইল সাধারণত অপরিবর্তিত থাকে। নিচে স্ট্যান্ডার্ড কনফিগারেশন দেওয়া হলো:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.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>
অ্যাপটি চালান
আপনার Android ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে প্রজেক্টের যেকোনো অ্যাক্টিভিটি ফাইল খুলে টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং দেখুন ছবিটি অনুপাত রক্ষা করে প্রদর্শিত হচ্ছে।
