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 public 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";
72 private final View m_layout;
73 private CursorView m_cursorView =
null;
74 private PopupWindow m_popup =
null;
75 private final int m_id;
76 private final int m_attr;
77 private final Activity m_activity;
78 private int m_posX = 0;
79 private int m_posY = 0;
83 private final boolean m_rtl;
86 public CursorHandle(Activity activity, View
layout,
int id,
int attr,
boolean rtl) {
87 m_activity = activity;
91 DisplayMetrics
metrics = activity.getResources().getDisplayMetrics();
92 m_yShift = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1
f,
metrics);
93 tolerance = Math.min(1, (
int)(m_yShift / 2
f));
94 m_lastX = m_lastY = -1 - tolerance;
98 private void initOverlay(){
102 Context
context = m_layout.getContext();
103 int[]
attrs = {m_attr};
104 TypedArray
a =
context.getTheme().obtainStyledAttributes(
attrs);
105 Drawable drawable =
a.getDrawable(0);
107 m_cursorView =
new CursorView(
context,
this);
108 m_cursorView.setImageDrawable(drawable);
110 m_popup =
new PopupWindow(
context,
null, android.R.attr.textSelectHandleWindowStyle);
111 m_popup.setSplitTouchEnabled(
true);
112 m_popup.setClippingEnabled(
false);
113 m_popup.setContentView(m_cursorView);
114 if (drawable !=
null) {
115 m_popup.setWidth(drawable.getIntrinsicWidth());
116 m_popup.setHeight(drawable.getIntrinsicHeight());
118 Log.w(QtTag,
"initOverlay(): cannot get width/height for popup " +
119 "from null drawable for attribute " + m_attr);
122 m_layout.getViewTreeObserver().addOnPreDrawListener(
this);
129 final int[] layoutLocation =
new int[2];
130 m_layout.getLocationOnScreen(layoutLocation);
133 final int[] activityLocation =
new int[2];
134 final int[] activityLocationInWindow =
new int[2];
135 m_activity.getWindow().getDecorView().getLocationOnScreen(activityLocation);
136 m_activity.getWindow().getDecorView().getLocationInWindow(activityLocationInWindow);
138 int x2 =
x + layoutLocation[0] - activityLocation[0];
139 int y2 =
y + layoutLocation[1] + m_yShift + (activityLocationInWindow[1] - activityLocation[1]);
141 if (m_id == QtInputDelegate.IdCursorHandle) {
142 x2 -= m_popup.getWidth() / 2 ;
143 }
else if ((m_id == QtInputDelegate.IdLeftHandle && !m_rtl) || (m_id == QtInputDelegate.IdRightHandle && m_rtl)) {
144 x2 -= m_popup.getWidth() * 3 / 4;
146 x2 -= m_popup.getWidth() / 4;
149 if (m_popup.isShowing()) {
150 m_popup.update(
x2,
y2, -1, -1);
151 m_cursorView.adjusted(
x - m_posX,
y - m_posY);
153 m_popup.showAtLocation(m_layout, 0,
x2,
y2);
164 m_cursorView.getLocationOnScreen(
location);
165 return location[1] + m_cursorView.getHeight();
169 if (m_popup !=
null) {
176 return m_cursorView.getDrawable().getIntrinsicWidth();
180 public void updatePosition(
int x,
int y) {
182 if (Math.abs(m_lastX -
x) > tolerance || Math.abs(m_lastY -
y) > tolerance) {
183 QtInputDelegate.handleLocationChanged(m_id,
x + m_posX,
y + m_posY);
190 public boolean onPreDraw() {
194 if (m_popup !=
null && m_popup.isShowing())
static struct AttrInfo attrs[]
n void setPosition(void) \n\
GLuint64 GLenum void * handle
GLint GLint GLint GLint GLint x
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLfixed GLfixed GLfixed y2