Skip to content

Commit

Permalink
Merge pull request #3982 from pingwang2011/timob-12583
Browse files Browse the repository at this point in the history
Timob 12583: Android: Maps V2 module: Adding custom views in the pin
  • Loading branch information
hieupham007 committed Mar 20, 2013
2 parents 5ccf04a + 6889478 commit 941872c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
9 changes: 9 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,11 @@ public class TiC
*/
public static final String PROPERTY_COUNTRY_CODE = "country_code";

/**
* @module.api
*/
public static final String PROPERTY_CROP_RECT = "cropRect";

/**
* @module.api
*/ // TIMOB-4478
Expand Down Expand Up @@ -1402,6 +1407,10 @@ public class TiC
*/
public static final String PROPERTY_MAX_LENGTH = "maxLength";

/**
* @module.api
*/
public static final String PROPERTY_MEDIA = "media";

/**
* @module.api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,27 +586,27 @@ public static StateListDrawable buildBackgroundDrawable(
public static KrollDict createDictForImage(int width, int height, byte[] data)
{
KrollDict d = new KrollDict();
d.put("x", 0);
d.put("y", 0);
d.put("width", width);
d.put("height", height);
d.put(TiC.PROPERTY_X, 0);
d.put(TiC.PROPERTY_Y, 0);
d.put(TiC.PROPERTY_WIDTH, width);
d.put(TiC.PROPERTY_HEIGHT, height);

KrollDict cropRect = new KrollDict();
cropRect.put("x", 0);
cropRect.put("y", 0);
cropRect.put("width", width);
cropRect.put("height", height);
d.put("cropRect", cropRect);
d.put("media", TiBlob.blobFromData(data, "image/png"));
cropRect.put(TiC.PROPERTY_X, 0);
cropRect.put(TiC.PROPERTY_X, 0);
cropRect.put(TiC.PROPERTY_WIDTH, width);
cropRect.put(TiC.PROPERTY_HEIGHT, height);
d.put(TiC.PROPERTY_CROP_RECT, cropRect);
d.put(TiC.PROPERTY_MEDIA, TiBlob.blobFromData(data, "image/png"));

return d;
}

public static TiBlob getImageFromDict(KrollDict dict)
{
if (dict != null) {
if (dict.containsKey("media")) {
Object media = dict.get("media");
if (dict.containsKey(TiC.PROPERTY_MEDIA)) {
Object media = dict.get(TiC.PROPERTY_MEDIA);
if (media instanceof TiBlob) {
return (TiBlob) media;
}
Expand All @@ -624,53 +624,50 @@ public static KrollDict viewToImage(KrollDict proxyDict, View view)
int height = view.getHeight();

// maybe move this out to a separate method once other refactor regarding "getWidth", etc is done
if(view.getWidth() == 0) {
if(proxyDict != null) {
if(proxyDict.containsKey(TiC.PROPERTY_WIDTH)) {
TiDimension widthDimension = new TiDimension(proxyDict.getString(TiC.PROPERTY_WIDTH), TiDimension.TYPE_WIDTH);
width = widthDimension.getAsPixels(view);
}
}
if (view.getWidth() == 0 && proxyDict != null && proxyDict.containsKey(TiC.PROPERTY_WIDTH)) {
TiDimension widthDimension = new TiDimension(proxyDict.getString(TiC.PROPERTY_WIDTH), TiDimension.TYPE_WIDTH);
width = widthDimension.getAsPixels(view);
}
if(view.getHeight() == 0) {
if(proxyDict != null) {
if(proxyDict.containsKey(TiC.PROPERTY_HEIGHT)) {
TiDimension heightDimension = new TiDimension(proxyDict.getString(TiC.PROPERTY_HEIGHT), TiDimension.TYPE_HEIGHT);
height = heightDimension.getAsPixels(view);
}
}
}
view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
if (view.getParent() == null) {
Log.i(TAG, "View does not have parent, calling layout", Log.DEBUG_MODE);
view.layout(0, 0, width, height);
if (view.getHeight() == 0 && proxyDict != null && proxyDict.containsKey(TiC.PROPERTY_HEIGHT)) {
TiDimension heightDimension = new TiDimension(proxyDict.getString(TiC.PROPERTY_HEIGHT),
TiDimension.TYPE_HEIGHT);
height = heightDimension.getAsPixels(view);
}

// now that we have forced the view to layout itself, grab dimensions
int wmode = width == 0 ? MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
int hmode = height == 0 ? MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
view.measure(MeasureSpec.makeMeasureSpec(width, wmode), MeasureSpec.makeMeasureSpec(height, hmode));

// Will force the view to layout itself, grab dimensions
width = view.getMeasuredWidth();
height = view.getMeasuredHeight();

// set a default BS value if the dimension is still 0 and log a warning
if(width == 0) {
if (width == 0) {
width = 100;
Log.e(TAG, "Width property is 0 for view, display view before calling toImage()", Log.DEBUG_MODE);
}
if(height == 0) {
if (height == 0) {
height = 100;
Log.e(TAG, "Height property is 0 for view, display view before calling toImage()", Log.DEBUG_MODE);
}

if (view.getParent() == null) {
Log.i(TAG, "View does not have parent, calling layout", Log.DEBUG_MODE);
view.layout(0, 0, width, height);
}

// opacity should support transparency by default
Config bitmapConfig = Config.ARGB_8888;

Drawable viewBackground = view.getBackground();
if (viewBackground != null) {
/*
* If the background is opaque then we should be able to safely use a space saving format that
* does not support the alpha channel. Basically, if a view has a background color set then the
* the pixel format will be opaque. If a background image supports an alpha channel, the pixel
* format will report transparency (even if the image doesn't actually look transparent). In
* short, most of the time the Config.ARGB_8888 format will be used when viewToImage is used
* If the background is opaque then we should be able to safely use a space saving format that
* does not support the alpha channel. Basically, if a view has a background color set then the
* the pixel format will be opaque. If a background image supports an alpha channel, the pixel
* format will report transparency (even if the image doesn't actually look transparent). In
* short, most of the time the Config.ARGB_8888 format will be used when viewToImage is used
* but in the cases where the background is opaque, the lower memory approach will be used.
*/
if (viewBackground.getOpacity() == PixelFormat.OPAQUE) {
Expand Down
2 changes: 1 addition & 1 deletion apidoc/Titanium/Map/Annotation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ properties:
- name: image
summary: Image to use for the the pin.
description: |
The image can be specified using a local URL or an image `Blob`. This is ignored if the customeView property is set.
The image can be specified using a local URL or an image `Blob`. This is ignored if the customView property is set.
type: [String, Titanium.Blob]
default: If not specified, a standard map pin image is used.

Expand Down

0 comments on commit 941872c

Please sign in to comment.