Skip to content

Commit

Permalink
Merge pull request #1769 from dizzymonkey/timob-6586
Browse files Browse the repository at this point in the history
Timob 6586  add support for backgroundRepeat property that will allow tiling of background image
  • Loading branch information
marshall committed Mar 21, 2012
2 parents 880373c + cd2ba37 commit cb3d1f8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ public class TiC
*/
public static final String PROPERTY_BACKGROUND_PREFIX = "background";

public static final String PROPERTY_BACKGROUND_REPEAT = "backgroundRepeat";

/**
* @module.api
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
*/
@Kroll.proxy(propertyAccessors={
// background properties
"backgroundImage", "backgroundSelectedImage", "backgroundFocusedImage",
"backgroundDisabledImage", "backgroundColor", "backgroundSelectedColor",
"backgroundFocusedColor", "backgroundDisabledColor", "backgroundPadding",
"backgroundImage", "backgroundRepeat", "backgroundSelectedImage",
"backgroundFocusedImage", "backgroundDisabledImage", "backgroundColor",
"backgroundSelectedColor", "backgroundFocusedColor", "backgroundDisabledColor",
"backgroundPadding",

// border properties
"borderColor", "borderRadius", "borderWidth",
Expand Down Expand Up @@ -100,6 +101,8 @@ public TiViewProxy()
{
langConversionTable = getLangConversionTable();
pendingAnimationLock = new Object();

defaultValues.put(TiC.PROPERTY_BACKGROUND_REPEAT, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
Expand Down Expand Up @@ -452,6 +453,7 @@ public static void setTextViewDIPPadding(TextView textView, int horizontalPaddin

public static StateListDrawable buildBackgroundDrawable(
String image,
boolean tileImage,
String color,
String selectedImage,
String selectedColor,
Expand All @@ -471,45 +473,58 @@ public static StateListDrawable buildBackgroundDrawable(
TiFileHelper tfh = new TiFileHelper(appContext);

if (image != null) {
bgDrawable = tfh.loadDrawable(image, false, true);
if (tileImage) {
InputStream inputStream;
try {
inputStream = tfh.openInputStream(image, false);
if (inputStream != null) {
BitmapDrawable tiledBackground = new BitmapDrawable(appContext.getResources(), inputStream);
tiledBackground.setTileModeX(Shader.TileMode.REPEAT);
tiledBackground.setTileModeY(Shader.TileMode.REPEAT);

bgDrawable = tiledBackground;
}

} catch (IOException e) {
Log.e(LCAT, "Exception occured when trying to open stream to specified background image: ", e);
}

} else {
bgDrawable = tfh.loadDrawable(image, false, true);
}

} else if (color != null) {
bgDrawable = new ColorDrawable(TiConvert.toColor(color));
}

if (selectedImage != null) {
bgSelectedDrawable = tfh.loadDrawable(selectedImage, false, true);

} else if (selectedColor != null) {
bgSelectedDrawable = new ColorDrawable(TiConvert.toColor(selectedColor));
} else {
if (image != null) {
bgSelectedDrawable = tfh.loadDrawable(image, false, true);
} else if (color != null) {
bgSelectedDrawable = new ColorDrawable(TiConvert.toColor(color));
}

} else if (bgDrawable != null) {
bgSelectedDrawable = bgDrawable;
}

if (focusedImage != null) {
bgFocusedDrawable = tfh.loadDrawable(focusedImage, false, true);

} else if (focusedColor != null) {
bgFocusedDrawable = new ColorDrawable(TiConvert.toColor(focusedColor));
} else {
if (image != null) {
bgFocusedDrawable = tfh.loadDrawable(image, false, true);
} else if (color != null) {
bgFocusedDrawable = new ColorDrawable(TiConvert.toColor(color));
}

} else if (bgDrawable != null) {
bgFocusedDrawable = bgDrawable;
}

if (disabledImage != null) {
bgDisabledDrawable = tfh.loadDrawable(disabledImage, false, true);

} else if (disabledColor != null) {
bgDisabledDrawable = new ColorDrawable(TiConvert.toColor(disabledColor));
} else {
if (image != null) {
bgDisabledDrawable = tfh.loadDrawable(image, false, true);
} else if (color != null) {
bgDisabledDrawable = new ColorDrawable(TiConvert.toColor(color));
}

} else if (bgDrawable != null) {
bgDisabledDrawable = bgDrawable;
}

if (bgDrawable != null || bgSelectedDrawable != null || bgFocusedDrawable != null || bgDisabledDrawable != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ private boolean hasImage(KrollDict d)
|| d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_DISABLED_IMAGE);
}

private boolean hasRepeat(KrollDict d)
{
return d.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_REPEAT);
}

private boolean hasBorder(KrollDict d)
{
return d.containsKeyAndNotNull(TiC.PROPERTY_BORDER_COLOR)
Expand Down Expand Up @@ -337,7 +342,6 @@ public void setzIndexChanged(boolean zIndexChanged)

public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy)
{

if (key.equals(TiC.PROPERTY_LEFT)) {
if (newValue != null) {
layoutParams.optionLeft = TiConvert.toTiDimension(TiConvert.toString(newValue), TiDimension.TYPE_LEFT);
Expand Down Expand Up @@ -440,11 +444,12 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
KrollDict d = proxy.getProperties();

boolean hasImage = hasImage(d);
boolean hasRepeat = hasRepeat(d);
boolean hasColorState = hasColorState(d);
boolean hasBorder = hasBorder(d);
boolean nativeViewNull = (nativeView == null);

boolean requiresCustomBackground = hasImage || hasColorState || hasBorder;
boolean requiresCustomBackground = hasImage || hasRepeat || hasColorState || hasBorder;

if (!requiresCustomBackground) {
if (background != null) {
Expand Down Expand Up @@ -485,7 +490,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}
}

if (hasImage || hasColorState) {
if (hasImage || hasRepeat || hasColorState) {
if (newBackground || key.startsWith(TiC.PROPERTY_BACKGROUND_PREFIX)) {
handleBackgroundImage(d);
}
Expand Down Expand Up @@ -622,6 +627,7 @@ private void applyCustomBackground(boolean reuseCurrentDrawable)
if (currentDrawable != null) {
if (reuseCurrentDrawable) {
background.setBackgroundDrawable(currentDrawable);

} else {
nativeView.setBackgroundDrawable(null);
currentDrawable.setCallback(null);
Expand Down Expand Up @@ -774,7 +780,17 @@ private void handleBackgroundImage(KrollDict d)
applyCustomBackground(false);
}

Drawable bgDrawable = TiUIHelper.buildBackgroundDrawable(bg, bgColor, bgSelected, bgSelectedColor, bgDisabled, bgDisabledColor, bgFocused, bgFocusedColor);
Drawable bgDrawable = TiUIHelper.buildBackgroundDrawable(
bg,
d.getBoolean(TiC.PROPERTY_BACKGROUND_REPEAT),
bgColor,
bgSelected,
bgSelectedColor,
bgDisabled,
bgDisabledColor,
bgFocused,
bgFocusedColor);

background.setBackgroundDrawable(bgDrawable);
}
}
Expand Down

0 comments on commit cb3d1f8

Please sign in to comment.