Activity ফোকাস পেলে, Android ফ্রেমওয়ার্ক
Activity-কে তার লেআউট আঁকতে বলে। Android ফ্রেমওয়ার্ক ড্রয়িংয়ের পদ্ধতি ম্যানেজ করে, তবে
Activity-কে অবশ্যই এর লেআউট হায়ারার্কির রুট নোড প্রদান করতে হবে।
Android ফ্রেমওয়ার্ক লেআউটের রুট নোড আঁকে এবং লেআউট ট্রি পরিমাপ করে আঁকে। এটি
ট্রি-তে হেঁটে এবং ভুল অঞ্চলের সাথে ইন্টারসেক্ট করে এমন প্রতিটি
View রেন্ডার করে আঁকে।
প্রতিটি ViewGroup-এর দায়িত্ব হল
draw()
পদ্ধতি ব্যবহার করে তার প্রতিটি চাইল্ডকে আঁকার অনুরোধ করা এবং প্রতিটি View-এর দায়িত্ব হল নিজেকে আঁকা। যেহেতু ট্রি প্রি-অর্ডার অনুযায়ী
ট্রাভার্স করা হয়, তাই ফ্রেমওয়ার্ক চিলড্রেনের আগে—অন্যভাবে বলতে গেলে, চিলড্রেনের পিছনে—পেরেন্ট
ড্র করে এবং ট্রিতে যেভাবে দেখা যায় সেই ক্রম অনুযায়ী সিবলিং ড্র করে।
Android ফ্রেমওয়ার্ক দুই-পাস প্রসেসে লেআউট ড্র করে: একটি পরিমাপ পাস ও একটি লেআউট পাস। measure(int, int)-এ
ফ্রেমওয়ার্ক পরিমাপ পাস করে এবং
View ট্রি-এর উপর থেকে নিচে ট্রাভার্সাল করে। প্রতিটি View রিকার্সনের সময় ট্রি-এর নিচের দিকে
ডাইমেনশন স্পেসিফিকেশন পুশ করে। পরিমাপ পাস শেষ হওয়ার পরে, প্রতিটি
View তার পরিমাপ স্টোর করে। ফ্রেমওয়ার্কটি দ্বিতীয় পাসটি
layout(int, int, int, int)
-এ করে এবং এটিও উপর থেকে নিচে হয়। এই পাসের সময়, প্রতিটি পেরেন্ট তার সমস্ত চাইল্ডের পজিশন ঠিক করার জন্য দায়ী
পরিমাপ পাসে গণনা করা সাইজ ব্যবহার করে।
লেআউট প্রসেসের দুটি পাস সম্পর্কে নিম্নলিখিত বিভাগে আরও বিস্তারিতভাবে বর্ণনা করা হয়েছে।
পরিমাপ করার পাস শুরু করুন
View অবজেক্টের
measure() পদ্ধতি
রিটার্ন করলে, এটির
getMeasuredWidth()
এবং
getMeasuredHeight()
ভ্যালু সেট করুন, এর সাথে View অবজেক্টের সব ডিসেন্ডেন্টের ভ্যালুও সেট করুন। View
অবজেক্টের পরিমাপ করা প্রস্থ এবং পরিমাপ করা উচ্চতার মান অবশ্যই View অবজেক্টের পেরেন্ট দ্বারা আরোপিত সীমাবদ্ধতা মেনে চলতে হবে। এটি নিশ্চিত করতে সাহায্য করে যে পরিমাপ পাস শেষ হওয়ার পরে, সমস্ত অভিভাবক
তাদের সমস্ত সন্তানের পরিমাপ গ্রহণ করেন।
কোনও পেরেন্ট View তার চাইল্ডের উপর একাধিকবার measure() কল করতে পারে। যেমন, অভিভাবক তার সন্তানের পছন্দের সাইজ নির্ধারণ করার জন্য একবার অনির্দিষ্ট ডাইমেনশন সহ পরিমাপ করতে পারেন। চাইল্ডের আনকনস্ট্রেনড সাইজের যোগফল খুব বড় বা খুব ছোট হলে, পেরেন্ট
হয়ত measure() আবার কল করতে পারে, যার ভ্যালু চাইল্ডের সাইজ কনস্ট্রেন করে।
পরিমাপ পাস ডাইমেনশন কমিউনিকেট করার জন্য দুটি ক্লাস ব্যবহার করে। ViewGroup.LayoutParams
ক্লাস হল কীভাবে View অবজেক্ট তাদের পছন্দের সাইজ এবং পজিশন কমিউনিকেট করে। বেস
ViewGroup.LayoutParams ক্লাস
View-এর পছন্দের প্রস্থ ও উচ্চতা বর্ণনা করে। প্রতিটি ডাইমেনশনের জন্য, এটি নিম্নলিখিতগুলির মধ্যে একটি নির্দিষ্ট করতে পারে:
- সঠিক ডাইমেনশন।
MATCH_PARENT, অর্থাৎView-এর জন্য পছন্দের সাইজ হল এর পেরেন্টের সাইজ, মাইনাস প্যাডিং।WRAP_CONTENT, যার অর্থ হলView-এর জন্য পছন্দের সাইজটি তার কন্টেন্ট, প্যাডিং সহ এনক্লোজ করার জন্য যথেষ্ট বড়।
ViewGroup-এর বিভিন্ন সাবক্লাসের জন্য ViewGroup.LayoutParams-এর সাবক্লাস আছে। যেমন,
RelativeLayout-এর নিজস্ব
ViewGroup.LayoutParams-এর সাবক্লাস আছে যার মধ্যে চাইল্ড
View অবজেক্টকে অনুভূমিক ও উল্লম্বভাবে কেন্দ্রে রাখার ক্ষমতা অন্তর্ভুক্ত।
MeasureSpec অবজেক্ট
প্যারেন্ট থেকে চাইল্ডে প্রয়োজনীয়তা পুশ করার জন্য ব্যবহার করা হয়। MeasureSpec তিনটি মোডের
মধ্যে একটিতে থাকতে পারে:
UNSPECIFIED: অভিভাবক এটি ব্যবহার করে চাইল্ডView-এর টার্গেট ডাইমেনশন নির্ধারণ করেন। যেমন,LinearLayoutতার চাইল্ডের উপরmeasure()কল করতে পারে, যেখানে উচ্চতাUNSPECIFIEDএবং প্রস্থEXACTLY২৪০ সেট করা থাকে, যাতে ২৪০ পিক্সেল প্রস্থের ক্ষেত্রে চাইল্ডViewকতটা লম্বা হতে চায় তা জানা যায়।EXACTLY: অভিভাবক এটি ব্যবহার করে চাইল্ডের উপর সঠিক সাইজ আরোপ করেন। চাইল্ডকে অবশ্যই এই সাইজ ব্যবহার করতে হবে এবং গ্যারেন্টি দিতে হবে যে এর সব ডিসেন্ডেন্ট এই সাইজের মধ্যে ফিট করে।AT MOST: অভিভাবক এটি ব্যবহার করে চাইল্ডের উপর সর্বাধিক সাইজ আরোপ করেন। চাইল্ডকে অবশ্যই গ্যারান্টি দিতে হবে যে এটি এবং এর সমস্ত ডিসেন্ডেন্ট এই সাইজের মধ্যে ফিট করে।
লেআউট পাস শুরু করুন
লেআউট শুরু করতে, requestLayout() কল করুন। এই
পদ্ধতিটি সাধারণত View-এর মাধ্যমে নিজের উপর কল করা হয় যখন এটি মনে করে যে এটি আর তার সীমার মধ্যে
ফিট করতে পারবে না।
কাস্টম পরিমাপ ও লেআউট লজিক প্রয়োগ করা
আপনি যদি কাস্টম পরিমাপ বা লেআউট লজিক প্রয়োগ করতে চান, তাহলে যেখানে লজিক প্রয়োগ করা হয়েছে সেই পদ্ধতি ওভাররাইড করুন:
onMeasure(int, int)
এবং
onLayout(boolean, int, int, int, int)।
এই পদ্ধতিগুলি যথাক্রমে measure(int, int) এবং
layout(int, int, int, int) দ্বারা কল করা হয়। measure(int, int) বা layout(int, int) মেথড ওভাররাইড করার চেষ্টা করবেন না—এই দুটি মেথডই
final, তাই এগুলি ওভাররাইড করা যাবে না।
WindowManager
স্যাম্পেল অ্যাপের
`SplitLayout`
ক্লাসে এটি কীভাবে করতে হয় তা নিম্নলিখিত উদাহরণে দেখানো হয়েছে। SplitLayout-এ দুই বা তার বেশি চাইল্ড ভিউ থাকলে এবং ডিসপ্লেতে ভাঁজ থাকলে,
এটি ভাঁজের দুই পাশে দুটি চাইল্ড ভিউকে পজিশন করে। নিম্নলিখিত উদাহরণে পরিমাপ ও লেআউট ওভাররাইড করার
ব্যবহারিক উদাহরণ দেখানো হয়েছে, তবে প্রোডাকশনের জন্য, আপনি এই আচরণ চাইলে
SlidingPaneLayout
ব্যবহার করুন।
Kotlin
/** * An example of split-layout for two views, separated by a display * feature that goes across the window. When both start and end views are * added, it checks whether there are display features that separate the area * in two—such as a fold or hinge—and places them side-by-side or * top-bottom. */ class SplitLayout : FrameLayout { private var windowLayoutInfo: WindowLayoutInfo? = null private var startViewId = 0 private var endViewId = 0 private var lastWidthMeasureSpec: Int = 0 private var lastHeightMeasureSpec: Int = 0 ... fun updateWindowLayout(windowLayoutInfo: WindowLayoutInfo) { this.windowLayoutInfo = windowLayoutInfo requestLayout() } override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { val startView = findStartView() val endView = findEndView() val splitPositions = splitViewPositions(startView, endView) if (startView != null && endView != null && splitPositions != null) { val startPosition = splitPositions[0] val startWidthSpec = MeasureSpec.makeMeasureSpec(startPosition.width(), EXACTLY) val startHeightSpec = MeasureSpec.makeMeasureSpec(startPosition.height(), EXACTLY) startView.measure(startWidthSpec, startHeightSpec) startView.layout( startPosition.left, startPosition.top, startPosition.right, startPosition.bottom ) val endPosition = splitPositions[1] val endWidthSpec = MeasureSpec.makeMeasureSpec(endPosition.width(), EXACTLY) val endHeightSpec = MeasureSpec.makeMeasureSpec(endPosition.height(), EXACTLY) endView.measure(endWidthSpec, endHeightSpec) endView.layout( endPosition.left, endPosition.top, endPosition.right, endPosition.bottom ) } else { super.onLayout(changed, left, top, right, bottom) } } /** * Gets the position of the split for this view. * @return A rect that defines of split, or {@code null} if there is no split. */ private fun splitViewPositions(startView: View?, endView: View?): Array? { if (windowLayoutInfo == null || startView == null || endView == null) { return null } // Calculate the area for view's content with padding. val paddedWidth = width - paddingLeft - paddingRight val paddedHeight = height - paddingTop - paddingBottom windowLayoutInfo?.displayFeatures ?.firstOrNull { feature -> isValidFoldFeature(feature) } ?.let { feature -> getFeaturePositionInViewRect(feature, this)?.let { if (feature.bounds.left == 0) { // Horizontal layout. val topRect = Rect( paddingLeft, paddingTop, paddingLeft + paddedWidth, it.top ) val bottomRect = Rect( paddingLeft, it.bottom, paddingLeft + paddedWidth, paddingTop + paddedHeight ) if (measureAndCheckMinSize(topRect, startView) && measureAndCheckMinSize(bottomRect, endView) ) { return arrayOf(topRect, bottomRect) } } else if (feature.bounds.top == 0) { // Vertical layout. val leftRect = Rect( paddingLeft, paddingTop, it.left, paddingTop + paddedHeight ) val rightRect = Rect( it.right, paddingTop, paddingLeft + paddedWidth, paddingTop + paddedHeight ) if (measureAndCheckMinSize(leftRect, startView) && measureAndCheckMinSize(rightRect, endView) ) { return arrayOf(leftRect, rightRect) } } } } // You previously tried to fit the children and measure them. Since they // don't fit, measure again to update the stored values. measure(lastWidthMeasureSpec, lastHeightMeasureSpec) return null } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) lastWidthMeasureSpec = widthMeasureSpec lastHeightMeasureSpec = heightMeasureSpec } /** * Measures a child view and sees if it fits in the provided rect. * This method calls [View.measure] on the child view, which updates its * stored values for measured width and height. If the view ends up with * different values, measure again. */ private fun measureAndCheckMinSize(rect: Rect, childView: View): Boolean { val widthSpec = MeasureSpec.makeMeasureSpec(rect.width(), AT_MOST) val heightSpec = MeasureSpec.makeMeasureSpec(rect.height(), AT_MOST) childView.measure(widthSpec, heightSpec) return childView.measuredWidthAndState and MEASURED_STATE_TOO_SMALL == 0 && childView.measuredHeightAndState and MEASURED_STATE_TOO_SMALL == 0 } private fun isValidFoldFeature(displayFeature: DisplayFeature) = (displayFeature as? FoldingFeature)?.let { feature -> getFeaturePositionInViewRect(feature, this) != null } ?: false }
জাভা
/** * An example of split-layout for two views, separated by a display feature * that goes across the window. When both start and end views are added, it checks * whether there are display features that separate the area in two—such as * fold or hinge—and places them side-by-side or top-bottom. */ public class SplitLayout extends FrameLayout { @Nullable private WindowLayoutInfo windowLayoutInfo = null; private int startViewId = 0; private int endViewId = 0; private int lastWidthMeasureSpec = 0; private int lastHeightMeasureSpec = 0; ... void updateWindowLayout(WindowLayoutInfo windowLayoutInfo) { this.windowLayoutInfo = windowLayoutInfo; requestLayout(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { @Nullable View startView = findStartView(); @Nullable View endView = findEndView(); @Nullable ListsplitPositions = splitViewPositions(startView, endView); if (startView != null && endView != null && splitPositions != null) { Rect startPosition = splitPositions.get(0); int startWidthSpec = MeasureSpec.makeMeasureSpec(startPosition.width(), EXACTLY); int startHeightSpec = MeasureSpec.makeMeasureSpec(startPosition.height(), EXACTLY); startView.measure(startWidthSpec, startHeightSpec); startView.layout( startPosition.left, startPosition.top, startPosition.right, startPosition.bottom ); Rect endPosition = splitPositions.get(1); int endWidthSpec = MeasureSpec.makeMeasureSpec(endPosition.width(), EXACTLY); int endHeightSpec = MeasureSpec.makeMeasureSpec(endPosition.height(), EXACTLY); startView.measure(endWidthSpec, endHeightSpec); startView.layout( endPosition.left, endPosition.top, endPosition.right, endPosition.bottom ); } else { super.onLayout(changed, left, top, right, bottom); } } /** * Gets the position of the split for this view. * @return A rect that defines of split, or {@code null} if there is no split. */ @Nullable private List splitViewPositions(@Nullable View startView, @Nullable View endView) { if (windowLayoutInfo == null || startView == null || endView == null) { return null; } int paddedWidth = getWidth() - getPaddingLeft() - getPaddingRight(); int paddedHeight = getHeight() - getPaddingTop() - getPaddingBottom(); List displayFeatures = windowLayoutInfo.getDisplayFeatures(); @Nullable DisplayFeature feature = displayFeatures .stream() .filter(item -> isValidFoldFeature(item) ) .findFirst() .orElse(null); if (feature != null) { Rect position = SampleToolsKt.getFeaturePositionInViewRect(feature, this, true); Rect featureBounds = feature.getBounds(); if (featureBounds.left == 0) { // Horizontal layout. Rect topRect = new Rect( getPaddingLeft(), getPaddingTop(), getPaddingLeft() + paddedWidth, position.top ); Rect bottomRect = new Rect( getPaddingLeft(), position.bottom, getPaddingLeft() + paddedWidth, getPaddingTop() + paddedHeight ); if (measureAndCheckMinSize(topRect, startView) && measureAndCheckMinSize(bottomRect, endView)) { ArrayList rects = new ArrayList (); rects.add(topRect); rects.add(bottomRect); return rects; } } else if (featureBounds.top == 0) { // Vertical layout. Rect leftRect = new Rect( getPaddingLeft(), getPaddingTop(), position.left, getPaddingTop() + paddedHeight ); Rect rightRect = new Rect( position.right, getPaddingTop(), getPaddingLeft() + paddedWidth, getPaddingTop() + paddedHeight ); if (measureAndCheckMinSize(leftRect, startView) && measureAndCheckMinSize(rightRect, endView)) { ArrayList rects = new ArrayList (); rects.add(leftRect); rects.add(rightRect); return rects; } } } // You previously tried to fit the children and measure them. Since // they don't fit, measure again to update the stored values. measure(lastWidthMeasureSpec, lastHeightMeasureSpec); return null; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); lastWidthMeasureSpec = widthMeasureSpec; lastHeightMeasureSpec = heightMeasureSpec; } /** * Measures a child view and sees if it fits in the provided rect. * This method calls [View.measure] on the child view, which updates * its stored values for measured width and height. If the view ends up with * different values, measure again. */ private boolean measureAndCheckMinSize(Rect rect, View childView) { int widthSpec = MeasureSpec.makeMeasureSpec(rect.width(), AT_MOST); int heightSpec = MeasureSpec.makeMeasureSpec(rect.height(), AT_MOST); childView.measure(widthSpec, heightSpec); return (childView.getMeasuredWidthAndState() & MEASURED_STATE_TOO_SMALL) == 0 && (childView.getMeasuredHeightAndState() & MEASURED_STATE_TOO_SMALL) == 0; } private boolean isValidFoldFeature(DisplayFeature displayFeature) { if (displayFeature instanceof FoldingFeature) { return SampleToolsKt.getFeaturePositionInViewRect(displayFeature, this, true) != null; } else { return false; } } }