1188 lines · 44.3 KB · Java
RawDownload
     * @param axis the axis being studied     * @param r the <code>SizeRequirements</code> object;     *          if <code>null</code> one will be created     * @return the newly initialized <code>SizeRequirements</code> object     */    protected SizeRequirements baselineRequirements(int axis, SizeRequirements r) {        SizeRequirements totalAscent = new SizeRequirements();        SizeRequirements totalDescent = new SizeRequirements();         if (r == null) {            r = new SizeRequirements();        }         r.alignment = 0.5f;         int n = getViewCount();         // loop through all children calculating the max of all their ascents and        // descents at minimum, preferred, and maximum sizes        for (int i = 0; i < n; i++) {            View v = getView(i);            float align = v.getAlignment(axis);            float span;            int ascent;            int descent;             // find the maximum of the preferred ascents and descents            span = v.getPreferredSpan(axis);            ascent = (int)(align * span);            descent = (int)(span - ascent);            totalAscent.preferred = Math.max(ascent, totalAscent.preferred);            totalDescent.preferred = Math.max(descent, totalDescent.preferred);             if (v.getResizeWeight(axis) > 0) {                // if the view is resizable then do the same for the minimum and                // maximum ascents and descents                span = v.getMinimumSpan(axis);                ascent = (int)(align * span);                descent = (int)(span - ascent);                totalAscent.minimum = Math.max(ascent, totalAscent.minimum);                totalDescent.minimum = Math.max(descent, totalDescent.minimum);                 span = v.getMaximumSpan(axis);                ascent = (int)(align * span);                descent = (int)(span - ascent);                totalAscent.maximum = Math.max(ascent, totalAscent.maximum);                totalDescent.maximum = Math.max(descent, totalDescent.maximum);            } else {                // otherwise use the preferred                totalAscent.minimum = Math.max(ascent, totalAscent.minimum);                totalDescent.minimum = Math.max(descent, totalDescent.minimum);                totalAscent.maximum = Math.max(ascent, totalAscent.maximum);                totalDescent.maximum = Math.max(descent, totalDescent.maximum);            }        }         // we now have an overall preferred, minimum, and maximum ascent and descent         // calculate the preferred span as the sum of the preferred ascent and preferred descent        r.preferred = (int)Math.min((long)totalAscent.preferred + (long)totalDescent.preferred,                                    Integer.MAX_VALUE);         // calculate the preferred alignment as the preferred ascent divided by the preferred span        if (r.preferred > 0) {            r.alignment = (float)totalAscent.preferred / r.preferred;        }          if (r.alignment == 0.0f) {            // if the preferred alignment is 0 then the minimum and maximum spans are simply            // the minimum and maximum descents since there's nothing above the baseline            r.minimum = totalDescent.minimum;            r.maximum = totalDescent.maximum;        } else if (r.alignment == 1.0f) {            // if the preferred alignment is 1 then the minimum and maximum spans are simply            // the minimum and maximum ascents since there's nothing below the baseline            r.minimum = totalAscent.minimum;            r.maximum = totalAscent.maximum;        } else {            // we want to honor the preferred alignment so we calculate two possible minimum            // span values using 1) the minimum ascent and the alignment, and 2) the minimum            // descent and the alignment. We'll choose the larger of these two numbers.            r.minimum = Math.round(Math.max(totalAscent.minimum / r.alignment,                                          totalDescent.minimum / (1.0f - r.alignment)));            // a similar calculation is made for the maximum but we choose the smaller number.            r.maximum = Math.round(Math.min(totalAscent.maximum / r.alignment,                                          totalDescent.maximum / (1.0f - r.alignment)));        }         return r;    }     /**     * Fetches the offset of a particular child's current layout.     * @param axis the axis being studied     * @param childIndex the index of the requested child     * @return the offset (location) for the specified child     */    protected int getOffset(int axis, int childIndex) {        int[] offsets = (axis == majorAxis) ? majorOffsets : minorOffsets;        return offsets[childIndex];    }     /**     * Fetches the span of a particular childs current layout.     * @param axis the axis being studied     * @param childIndex the index of the requested child     * @return the span (width or height) of the specified child     */    protected int getSpan(int axis, int childIndex) {        int[] spans = (axis == majorAxis) ? majorSpans : minorSpans;        return spans[childIndex];    }     /**     * Determines in which direction the next view lays.     * Consider the View at index n. Typically the <code>View</code>s     * are layed out from left to right, so that the <code>View</code>     * to the EAST will be at index n + 1, and the <code>View</code>     * to the WEST will be at index n - 1. In certain situations,     * such as with bidirectional text, it is possible     * that the <code>View</code> to EAST is not at index n + 1,     * but rather at index n - 1, or that the <code>View</code>     * to the WEST is not at index n - 1, but index n + 1.     * In this case this method would return true,     * indicating the <code>View</code>s are layed out in     * descending order. Otherwise the method would return false     * indicating the <code>View</code>s are layed out in ascending order.     * <p>     * If the receiver is laying its <code>View</code>s along the     * <code>Y_AXIS</code>, this will will return the value from     * invoking the same method on the <code>View</code>     * responsible for rendering <code>position</code> and     * <code>bias</code>. Otherwise this will return false.     *     * @param position position into the model     * @param bias either <code>Position.Bias.Forward</code> or     *          <code>Position.Bias.Backward</code>     * @return true if the <code>View</code>s surrounding the     *          <code>View</code> responding for rendering     *          <code>position</code> and <code>bias</code>     *          are layed out in descending order; otherwise false     */    protected boolean flipEastAndWestAtEnds(int position,                                            Position.Bias bias) {        if(majorAxis == Y_AXIS) {            int testPos = (bias == Position.Bias.Backward) ?                          Math.max(0, position - 1) : position;            int index = getViewIndexAtPosition(testPos);            if(index != -1) {                View v = getView(index);                if(v != null && v instanceof CompositeView) {                    return ((CompositeView)v).flipEastAndWestAtEnds(position,                                                                    bias);                }            }        }        return false;    }     // --- variables ------------------------------------------------     int majorAxis;     int majorSpan;    int minorSpan;     /*     * Request cache     */    boolean majorReqValid;    boolean minorReqValid;    SizeRequirements majorRequest;    SizeRequirements minorRequest;     /*     * Allocation cache     */    boolean majorAllocValid;    int[] majorOffsets;    int[] majorSpans;    boolean minorAllocValid;    int[] minorOffsets;    int[] minorSpans;     /** used in paint. */    Rectangle tempRect;}

Lines 1001–1188 of 1188. Previous lines