Skip to content

Commit

Permalink
Merge pull request #885 from ayeung/timob-5819-1_7_X
Browse files Browse the repository at this point in the history
TIMOB-5819: Android: Percentage-based views need to be re-drawn on device rotation
  • Loading branch information
Opie Cyrus committed Dec 9, 2011
2 parents 31d17ec + e575b9c commit c0d8707
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiCompositeLayout;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutArrangement;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.view.TiUIView;

import android.content.Context;
Expand All @@ -37,6 +38,7 @@ private class TiScrollViewLayout extends TiCompositeLayout
{
private static final int AUTO = Integer.MAX_VALUE;
protected int measuredWidth = 0, measuredHeight = 0;
private int parentWidth = 0, parentHeight = 0;

public TiScrollViewLayout(Context context, LayoutArrangement arrangement) {
super(context, arrangement);
Expand All @@ -52,6 +54,16 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
measuredHeight = measuredWidth = 0;
}

public void setParentWidth(int width)
{
parentWidth = width;
}

public void setParentHeight(int height)
{
parentHeight = height;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Expand All @@ -65,7 +77,17 @@ private int getContentProperty(String property) {
if (value.equals(TiC.SIZE_AUTO)) {
return AUTO;
} else if (value instanceof Number) {
return ((Number)value).intValue();
return ((Number) value).intValue();
} else {
int type = 0;
TiDimension dimension;
if (TiC.PROPERTY_CONTENT_HEIGHT.equals(property)) {
type = TiDimension.TYPE_HEIGHT;
} else if (TiC.PROPERTY_CONTENT_WIDTH.equals(property)) {
type = TiDimension.TYPE_WIDTH;
}
dimension = new TiDimension(value.toString(), type);
return dimension.getUnits() == TiDimension.COMPLEX_UNIT_AUTO ? AUTO : dimension.getIntValue();
}
}
return AUTO;
Expand All @@ -78,16 +100,18 @@ private int calculateAbsoluteRight(View child)
if (contentWidth == AUTO) {
int childMeasuredWidth = child.getMeasuredWidth();
if (!p.autoWidth) {
childMeasuredWidth = p.optionWidth.getAsPixels(this);
childMeasuredWidth = getDimensionValue(p.optionWidth, parentWidth);
}
if (p.optionLeft != null) {
childMeasuredWidth += p.optionLeft.getAsPixels(this);
childMeasuredWidth += getDimensionValue(p.optionLeft, parentWidth);
}
if (p.optionRight != null) {
childMeasuredWidth += p.optionRight.getAsPixels(this);
childMeasuredWidth += getDimensionValue(p.optionRight, parentWidth);
}

measuredWidth = Math.max(childMeasuredWidth, measuredWidth);
// Make parentWidth the minimum value
measuredWidth = Math.max(parentWidth, measuredWidth);
} else {
measuredWidth = contentWidth;
}
Expand All @@ -97,28 +121,40 @@ private int calculateAbsoluteRight(View child)

private int calculateAbsoluteBottom(View child)
{
LayoutParams p = (LayoutParams)child.getLayoutParams();
LayoutParams p = (LayoutParams) child.getLayoutParams();
int contentHeight = getContentProperty("contentHeight");

if (contentHeight == AUTO) {
int childMeasuredHeight = child.getMeasuredHeight();
if (!p.autoHeight) {
childMeasuredHeight = p.optionHeight.getAsPixels(this);
childMeasuredHeight = getDimensionValue(p.optionHeight, parentHeight);
}
if (p.optionTop != null) {
childMeasuredHeight += p.optionTop.getAsPixels(this);
childMeasuredHeight += getDimensionValue(p.optionTop, parentHeight);
}
if (p.optionBottom != null) {
childMeasuredHeight += p.optionBottom.getAsPixels(this);
childMeasuredHeight += getDimensionValue(p.optionBottom, parentHeight);
}

measuredHeight = Math.max(childMeasuredHeight, measuredHeight);
// Make parentHeight the minimum value
measuredHeight = Math.max(parentHeight, measuredHeight);
} else {
measuredHeight = contentHeight;
}
return measuredHeight;
}

private int getDimensionValue(TiDimension dimension, int parentValue)
{
// getAsPixels doesn't return the correct value for percentages, so we manually calculate the percentage
// values here
if (dimension.isUnitPercent()) {
return (int) ((dimension.getValue() / 100.0) * parentValue);
}
return dimension.getAsPixels(this);
}

@Override
protected void constrainChild(View child, int width, int wMode,
int height, int hMode) {
Expand Down Expand Up @@ -203,6 +239,15 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) {
data.put("y", t);
getProxy().fireEvent("scroll", data);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
layout.setParentHeight(MeasureSpec.getSize(heightMeasureSpec));
layout.setParentWidth(MeasureSpec.getSize(widthMeasureSpec));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

private class TiHorizontalScrollView extends HorizontalScrollView
Expand Down Expand Up @@ -239,6 +284,15 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) {
data.put("y", t);
getProxy().fireEvent("scroll", data);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
layout.setParentHeight(MeasureSpec.getSize(heightMeasureSpec));
layout.setParentWidth(MeasureSpec.getSize(widthMeasureSpec));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

public TiUIScrollView(TiViewProxy proxy)
Expand Down
2 changes: 2 additions & 0 deletions android/titanium/src/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ public class TiC
public static final String PROPERTY_CODE = "code";
public static final String PROPERTY_COLOR = "color";
public static final String PROPERTY_COORDS = "coords";
public static final String PROPERTY_CONTENT_HEIGHT = "contentHeight";
public static final String PROPERTY_CONTENT_INTENT = "contentIntent";
public static final String PROPERTY_CONTENT_TEXT = "contentText";
public static final String PROPERTY_CONTENT_TITLE = "contentTitle";
public static final String PROPERTY_CONTENT_URL = "contentURL";
public static final String PROPERTY_CONTENT_VIEW = "contentView";
public static final String PROPERTY_CONTENT_WIDTH = "contentWidth";
public static final String PROPERTY_COUNTRY = "country";
public static final String PROPERTY_COUNTRY_CODE = "countryCode";
public static final String PROPERTY_DATA = "data";
Expand Down

0 comments on commit c0d8707

Please sign in to comment.