Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-27807

Android: convertPointToView() returns pixels instead of default units

    Details

    • Story Points:
      5

      Description

      Summary:
      Titanium's View.convertPointToView() method is hardcoded to return pixels on Android. The returned coordinates should be using the "ti.ui.defaultunit" assigned to the "tiapp.xml" instead, which defaults to "dp".

      iOS returns "dp", not pixels. So, this is a parity issue.

      Steps to reproduce:

      1. Build and run the below on Android.
      2. Attempt to drag one of the squares onscreen.
      3. Notice the square disappears.
      4. Uncomment the Ti.Android code block below. (Converts pixels to dp.)
      5. Rebuild for Android.
      6. Notice that dragging a square now works.
      7. Build and run on iOS.
      8. Notice that dragging a square now works.

      function createDragSquare(top, left, color) {
      	var view = Ti.UI.createView({
      		touchEnabled: true,
      		backgroundColor: color,
      		center: { x: left + 50, y: top + 50 },
      		width: 100,
      		height: 100,
      	});
      	view.addEventListener("touchmove", function(event) {
      		var parentPoint = view.convertPointToView({ x: event.x, y: event.y }, window);
      //		if (Ti.Android) {
      //			parentPoint.x = Ti.UI.convertUnits(parentPoint.x + "px", "dip");
      //			parentPoint.y = Ti.UI.convertUnits(parentPoint.y + "px", "dip");
      //		}
      		Ti.API.info("@@@ touch point: (" + parentPoint.x + ", " + parentPoint.y + ")");
      		view.center = parentPoint;
      		event.cancelBubble = true;
      	});
      	return view;
      }
       
      var window = Ti.UI.createWindow({ backgroundColor: "white" });
      window.add(createDragSquare(40, 40, "red"));
      window.add(createDragSquare(120, 120, "green"));
      window.add(createDragSquare(200, 200, "blue"));
      window.open();
      

      Work-Around:
      Convert the convertPointToView() method's returned results from pixels to dp only on Android as shown below.

      var parentPoint = view.convertPointToView({ x: event.x, y: event.y }, window);
      if (Ti.Android) {
      	parentPoint.x = Ti.UI.convertUnits(parentPoint.x + "px", "dip");
      	parentPoint.y = Ti.UI.convertUnits(parentPoint.y + "px", "dip");
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                jquick Joshua Quick
                Reporter:
                jquick Joshua Quick
                Reviewer:
                Gary Mathews
                Tester:
                Samir Mohammed
              • Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Backbone Issue Sync

                  • Backbone Issue Sync is enabled for your project, but we do not have any synchronization info for this issue.

                    Git Integration