android: add Cursor class for drawing the cursor to the canvas
Change-Id: I5d8191ffb3d3dd3d3ab11c757a3b7d5dc3fb2e82
This commit is contained in:
committed by
Miklos Vajna
parent
4e0d2fcdda
commit
6d9af88998
@@ -0,0 +1,38 @@
|
|||||||
|
package org.libreoffice.canvas;
|
||||||
|
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.Paint;
|
||||||
|
import android.graphics.RectF;
|
||||||
|
|
||||||
|
public class Cursor extends CommonCanvasElement {
|
||||||
|
private static final float CURSOR_WIDTH = 2f;
|
||||||
|
private final Paint mCursorPaint = new Paint();
|
||||||
|
public RectF mPosition = new RectF();
|
||||||
|
public RectF mScaledPosition = new RectF();
|
||||||
|
public int mAlpha = 0;
|
||||||
|
|
||||||
|
public Cursor() {
|
||||||
|
mCursorPaint.setColor(Color.BLACK);
|
||||||
|
mCursorPaint.setAlpha(0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onHitTest(float x, float y) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDraw(Canvas canvas) {
|
||||||
|
canvas.drawRect(mScaledPosition, mCursorPaint);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reposition(RectF rect) {
|
||||||
|
mScaledPosition = rect;
|
||||||
|
mScaledPosition.right = mScaledPosition.left + CURSOR_WIDTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cycleAlpha() {
|
||||||
|
mCursorPaint.setAlpha(mCursorPaint.getAlpha() == 0 ? 0xFF : 0);
|
||||||
|
}
|
||||||
|
}
|
@@ -23,6 +23,7 @@ import android.view.View;
|
|||||||
|
|
||||||
import org.libreoffice.LOKitShell;
|
import org.libreoffice.LOKitShell;
|
||||||
import org.libreoffice.R;
|
import org.libreoffice.R;
|
||||||
|
import org.libreoffice.canvas.Cursor;
|
||||||
import org.libreoffice.canvas.GraphicSelection;
|
import org.libreoffice.canvas.GraphicSelection;
|
||||||
import org.libreoffice.canvas.SelectionHandle;
|
import org.libreoffice.canvas.SelectionHandle;
|
||||||
import org.libreoffice.canvas.SelectionHandleEnd;
|
import org.libreoffice.canvas.SelectionHandleEnd;
|
||||||
@@ -40,15 +41,10 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class TextCursorView extends View implements View.OnTouchListener {
|
public class TextCursorView extends View implements View.OnTouchListener {
|
||||||
private static final String LOGTAG = TextCursorView.class.getSimpleName();
|
private static final String LOGTAG = TextCursorView.class.getSimpleName();
|
||||||
private static final float CURSOR_WIDTH = 2f;
|
|
||||||
private static final int CURSOR_BLINK_TIME = 500;
|
private static final int CURSOR_BLINK_TIME = 500;
|
||||||
|
|
||||||
private boolean mInitialized = false;
|
private boolean mInitialized = false;
|
||||||
private RectF mCursorPosition = new RectF();
|
|
||||||
private RectF mCursorScaledPosition = new RectF();
|
|
||||||
private Paint mCursorPaint = new Paint();
|
|
||||||
private int mCursorAlpha = 0;
|
|
||||||
private boolean mCursorVisible;
|
|
||||||
|
|
||||||
private List<RectF> mSelections = new ArrayList<RectF>();
|
private List<RectF> mSelections = new ArrayList<RectF>();
|
||||||
private List<RectF> mScaledSelections = new ArrayList<RectF>();
|
private List<RectF> mScaledSelections = new ArrayList<RectF>();
|
||||||
@@ -65,6 +61,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
private SelectionHandle mHandleStart;
|
private SelectionHandle mHandleStart;
|
||||||
private SelectionHandle mHandleEnd;
|
private SelectionHandle mHandleEnd;
|
||||||
|
|
||||||
|
private Cursor mCursor;
|
||||||
|
|
||||||
private SelectionHandle mDragHandle = null;
|
private SelectionHandle mDragHandle = null;
|
||||||
|
|
||||||
public TextCursorView(Context context) {
|
public TextCursorView(Context context) {
|
||||||
@@ -87,9 +85,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
setOnTouchListener(this);
|
setOnTouchListener(this);
|
||||||
mLayerView = layerView;
|
mLayerView = layerView;
|
||||||
|
|
||||||
mCursorPaint.setColor(Color.BLACK);
|
mCursor = new Cursor();
|
||||||
mCursorPaint.setAlpha(0xFF);
|
mCursor.setVisible(false);
|
||||||
mCursorVisible = false;
|
|
||||||
|
|
||||||
mSelectionPaint.setColor(Color.BLUE);
|
mSelectionPaint.setColor(Color.BLUE);
|
||||||
mSelectionPaint.setAlpha(50);
|
mSelectionPaint.setAlpha(50);
|
||||||
@@ -113,10 +110,10 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
* @param position - new position of the cursor
|
* @param position - new position of the cursor
|
||||||
*/
|
*/
|
||||||
public void changeCursorPosition(RectF position) {
|
public void changeCursorPosition(RectF position) {
|
||||||
if (RectUtils.fuzzyEquals(mCursorPosition, position)) {
|
if (RectUtils.fuzzyEquals(mCursor.mPosition, position)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mCursorPosition = position;
|
mCursor.mPosition = position;
|
||||||
|
|
||||||
ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
|
ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
|
||||||
repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
|
repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
|
||||||
@@ -149,10 +146,10 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void repositionWithViewport(float x, float y, float zoom) {
|
public void repositionWithViewport(float x, float y, float zoom) {
|
||||||
mCursorScaledPosition = convertToScreen(mCursorPosition, x, y, zoom);
|
RectF rect = convertToScreen(mCursor.mPosition, x, y, zoom);
|
||||||
mCursorScaledPosition.right = mCursorScaledPosition.left + CURSOR_WIDTH;
|
mCursor.reposition(rect);
|
||||||
|
|
||||||
RectF rect = convertToScreen(mHandleMiddle.mDocumentPosition, x, y, zoom);
|
rect = convertToScreen(mHandleMiddle.mDocumentPosition, x, y, zoom);
|
||||||
mHandleMiddle.reposition(rect.left, rect.bottom);
|
mHandleMiddle.reposition(rect.left, rect.bottom);
|
||||||
|
|
||||||
rect = convertToScreen(mHandleStart.mDocumentPosition, x, y, zoom);
|
rect = convertToScreen(mHandleStart.mDocumentPosition, x, y, zoom);
|
||||||
@@ -189,9 +186,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
|
|
||||||
if (mCursorVisible) {
|
mCursor.draw(canvas);
|
||||||
canvas.drawRect(mCursorScaledPosition, mCursorPaint);
|
|
||||||
}
|
|
||||||
mHandleMiddle.draw(canvas);
|
mHandleMiddle.draw(canvas);
|
||||||
mHandleStart.draw(canvas);
|
mHandleStart.draw(canvas);
|
||||||
mHandleEnd.draw(canvas);
|
mHandleEnd.draw(canvas);
|
||||||
@@ -211,8 +207,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
*/
|
*/
|
||||||
private Runnable cursorAnimation = new Runnable() {
|
private Runnable cursorAnimation = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (mCursorVisible) {
|
if (mCursor.isVisible()) {
|
||||||
mCursorPaint.setAlpha(mCursorPaint.getAlpha() == 0 ? 0xFF : 0);
|
mCursor.cycleAlpha();
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
postDelayed(cursorAnimation, CURSOR_BLINK_TIME);
|
postDelayed(cursorAnimation, CURSOR_BLINK_TIME);
|
||||||
@@ -223,8 +219,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
* Show the cursor on the view.
|
* Show the cursor on the view.
|
||||||
*/
|
*/
|
||||||
public void showCursor() {
|
public void showCursor() {
|
||||||
if (!mCursorVisible) {
|
if (!mCursor.isVisible()) {
|
||||||
mCursorVisible = true;
|
mCursor.setVisible(true);
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,8 +229,8 @@ public class TextCursorView extends View implements View.OnTouchListener {
|
|||||||
* Hide the cursor.
|
* Hide the cursor.
|
||||||
*/
|
*/
|
||||||
public void hideCursor() {
|
public void hideCursor() {
|
||||||
if (mCursorVisible) {
|
if (mCursor.isVisible()) {
|
||||||
mCursorVisible = false;
|
mCursor.setVisible(false);
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user