4package org.qtproject.qt.android;
6import android.annotation.SuppressLint;
7import android.app.Activity;
8import android.content.Context;
9import android.content.res.TypedArray;
10import android.graphics.drawable.Drawable;
11import android.util.DisplayMetrics;
12import android.util.Log;
13import android.util.TypedValue;
14import android.view.MotionEvent;
15import android.view.View;
16import android.view.ViewTreeObserver;
17import android.widget.ImageView;
18import android.widget.PopupWindow;
21@SuppressLint(
"ViewConstructor")
22class CursorView extends ImageView
24 private final CursorHandle mHandle;
26 private float m_offsetX;
27 private float m_offsetY;
28 private boolean m_pressed =
false;
36 void adjusted(
int dx,
int dy) {
42 public boolean onTouchEvent(MotionEvent ev) {
43 switch (ev.getActionMasked()) {
44 case MotionEvent.ACTION_DOWN: {
45 m_offsetX = ev.getRawX();
46 m_offsetY = ev.getRawY() + (float) getHeight() / 2;
51 case MotionEvent.ACTION_MOVE: {
54 mHandle.updatePosition(Math.round(ev.getRawX() - m_offsetX),
55 Math.round(ev.getRawY() - m_offsetY));
59 case MotionEvent.ACTION_UP:
60 case MotionEvent.ACTION_CANCEL:
69class CursorHandle
implements ViewTreeObserver.OnPreDrawListener
71 private static final String QtTag =
"QtCursorHandle";
74 static final int IdCursorHandle = 1;
75 static final int IdLeftHandle = 2;
76 static final int IdRightHandle = 3;
78 private final View m_layout;
79 private CursorView m_cursorView =
null;
80 private PopupWindow m_popup =
null;
81 private final int m_id;
82 private final int m_attr;
83 private final Activity m_activity;
84 private int m_posX = 0;
85 private int m_posY = 0;
89 private final boolean m_rtl;
92 CursorHandle(Activity activity, View
layout,
int id,
int attr,
boolean rtl) {
98 m_yShift = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1
f,
metrics);
99 tolerance = Math.min(1, (
int)(m_yShift / 2
f));
100 m_lastX = m_lastY = -1 - tolerance;
104 private void initOverlay(){
109 int[]
attrs = {m_attr};
111 TypedArray
a =
context.getTheme().obtainStyledAttributes(
attrs);
113 drawable =
a.getDrawable(0);
118 m_cursorView =
new CursorView(
context,
this);
119 m_cursorView.setImageDrawable(drawable);
121 m_popup =
new PopupWindow(
context,
null, android.R.attr.textSelectHandleWindowStyle);
122 m_popup.setSplitTouchEnabled(
true);
123 m_popup.setClippingEnabled(
false);
124 m_popup.setContentView(m_cursorView);
125 if (drawable !=
null) {
126 m_popup.setWidth(drawable.getIntrinsicWidth());
127 m_popup.setHeight(drawable.getIntrinsicHeight());
129 Log.w(QtTag,
"initOverlay(): cannot get width/height for popup " +
130 "from null drawable for attribute " + m_attr);
133 m_layout.getViewTreeObserver().addOnPreDrawListener(
this);
140 final int[] layoutLocation =
new int[2];
145 View positioningView = (View) m_layout.getParent();
146 if (positioningView ==
null)
147 positioningView = m_layout;
149 positioningView.getLocationOnScreen(layoutLocation);
152 final int[] activityLocation =
new int[2];
153 final int[] activityLocationInWindow =
new int[2];
154 m_activity.getWindow().getDecorView().getLocationOnScreen(activityLocation);
155 m_activity.getWindow().getDecorView().getLocationInWindow(activityLocationInWindow);
157 int x2 =
x + layoutLocation[0] - activityLocation[0];
158 int y2 =
y + layoutLocation[1] + m_yShift + (activityLocationInWindow[1] - activityLocation[1]);
160 if (m_id == IdCursorHandle) {
161 x2 -= m_popup.getWidth() / 2 ;
162 }
else if ((m_id == IdLeftHandle && !m_rtl) || (m_id == IdRightHandle && m_rtl)) {
163 x2 -= m_popup.getWidth() * 3 / 4;
165 x2 -= m_popup.getWidth() / 4;
168 if (m_popup.isShowing()) {
169 m_popup.update(
x2,
y2, -1, -1);
170 m_cursorView.adjusted(
x - m_posX,
y - m_posY);
172 m_popup.showAtLocation(positioningView, 0,
x2,
y2);
183 m_cursorView.getLocationOnScreen(
location);
184 return location[1] + m_cursorView.getHeight();
188 if (m_popup !=
null) {
195 if (m_cursorView ==
null)
198 return m_cursorView.getDrawable().getIntrinsicWidth();
202 void updatePosition(
int x,
int y) {
204 if (Math.abs(m_lastX -
x) > tolerance || Math.abs(m_lastY -
y) > tolerance) {
205 QtInputDelegate.handleLocationChanged(m_id,
x + m_posX,
y + m_posY);
212 public boolean onPreDraw() {
216 if (m_popup !=
null && m_popup.isShowing())
static const QString context()
Q_CORE_EXPORT QtJniTypes::Activity activity()
static struct AttrInfo attrs[]
n void setPosition(void) \n\
GLint GLint GLint GLint GLint x
GLboolean GLboolean GLboolean GLboolean a
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLfixed GLfixed GLfixed y2
EGLImageKHR EGLint EGLint * handle
QGraphicsGridLayout * layout