Android व्यू कैसे ड्रॉ करता है

Android फ़्रेमवर्क लेआउट बनाने के लिए Activity 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.LayoutParams की सब-क्लास हैं ViewGroup. उदाहरण के लिए, RelativeLayout के पास ViewGroup.LayoutParams की सब-क्लास, जिसमें बच्चे को बीच में रखने की सुविधा शामिल है View ऑब्जेक्ट हॉरिज़ॉन्टल और वर्टिकल तौर पर दिखते हैं.

MeasureSpec ऑब्जेक्ट हैं ज़रूरतों को माता-पिता से बच्चे तक पहुंचाने के लिए इस्तेमाल किया जाता है. MeasureSpec इसमें शामिल हो सकता है तीन में से एक मोड:

  • UNSPECIFIED: पैरंट इसका इस्तेमाल करके, चाइल्ड View का टारगेट डाइमेंशन तय करता है. उदाहरण के लिए, LinearLayout शायद कॉल करें measure() की ऊंचाई UNSPECIFIED और चौड़ाई पर सेट है EXACTLY 240 में से ताकि यह पता लगाया जा सके कि बच्चा View की लंबाई को कितना बड़ा करना चाहता है. इसकी चौड़ाई 240 पिक्सल है.
  • 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 हैं, इसलिए उन्हें ओवरराइड नहीं किया जा सकता.

नीचे दिए गए उदाहरण में, `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
}

Java

/**
* 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
      List splitPositions = 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;
      }
   }
}