Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-27807] Android: convertPointToView() returns pixels instead of default units

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sRelease 10.0.0
ComponentsAndroid
Labels2020-Q4, android, breaking-change, drag, parity, touch, unit
ReporterJoshua Quick
AssigneeJoshua Quick
Created2020-03-12T01:49:55.000+0000
Updated2020-12-11T13:04:38.000+0000

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:*

Build and run the below on Android.

Attempt to drag one of the squares onscreen.

Notice the square disappears.

Uncomment the Ti.Android code block below. _(Converts pixels to dp.)_

Rebuild for Android.

Notice that dragging a square now works.

Build and run on iOS.

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");
}

Comments

  1. Joshua Quick 2020-12-05

    PR (master): https://github.com/appcelerator/titanium_mobile/pull/12320
  2. Samir Mohammed 2020-12-11

    FR Passed, waiting on Jenkins build.

JSON Source