diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2015-02-26 13:25:58 +0900 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2015-03-02 08:50:55 +0100 |
commit | c1299f0cab7eaa7fae2b445fca3b9c808c5030cd (patch) | |
tree | 947c0f1237cb578a642117c0829bc5cbe410bdcd /android | |
parent | 4a440511f570f876651bc6e4ce48cf2a5481d9ba (diff) |
android: change TextCursorView parent to View and draw with canvas
Previously TextCursorView was subclassing ImageView and a drawable
to draw the cursor by scaling the drawable, which is not an ideal
solution. This is now changed so that the TextCursorView is a
proper subclass of View and draws the cursor using canvas.
Change-Id: I27b1ea73911afbfe9eee6bf382db5368ae449308
Diffstat (limited to 'android')
3 files changed, 50 insertions, 45 deletions
diff --git a/android/experimental/LOAndroid3/res/drawable/text_cursor.xml b/android/experimental/LOAndroid3/res/drawable/text_cursor.xml deleted file mode 100644 index f39e6e8a6d1f..000000000000 --- a/android/experimental/LOAndroid3/res/drawable/text_cursor.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > - <size android:width="2px" /> - <size android:height="1px" /> - <solid android:color="#000000"/> -</shape>
\ No newline at end of file diff --git a/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml b/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml index 8d8d0148bb0b..3de21f65d320 100644 --- a/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml +++ b/android/experimental/LOAndroid3/res/layout/text_selection_handles.xml @@ -28,9 +28,8 @@ gecko:handleType="end"/> <org.libreoffice.TextCursorView android:id="@+id/text_cursor_view" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:src="@drawable/text_cursor" + android:layout_width="fill_parent" + android:layout_height="fill_parent" android:visibility="gone"/> </merge> diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java index b2508b6d3a50..93600c639ea8 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/TextCursorView.java @@ -1,10 +1,14 @@ package org.libreoffice; import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.PointF; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; -import android.widget.ImageView; +import android.view.View; import android.widget.RelativeLayout; import org.mozilla.gecko.gfx.ImmutableViewportMetrics; @@ -14,22 +18,38 @@ import org.mozilla.gecko.gfx.RectUtils; /** * Text cursor view responsible to show the cursor drawable on the screen. */ -public class TextCursorView extends ImageView { +public class TextCursorView extends View { private static final String LOGTAG = TextCursorView.class.getSimpleName(); - private RectF mPosition; - private RelativeLayout.LayoutParams mLayoutParams; + private boolean mCursorAnimationEnabled = false; + private RectF mCursorPosition = new RectF(); + private PointF mCursorScaledPosition = new PointF(); - private int mLeft; - private int mTop; + private float mCursorHeight = 0f; + private float mCursorWidth = 2f; - private int mWidth; - private int mHeight; - private int mAlpha = 0; + private int mCursorAlpha = 0; + + public TextCursorView(Context context) { + super(context); + startCursorAnimation(); + } public TextCursorView(Context context, AttributeSet attrs) { super(context, attrs); - postDelayed(cursorAnimation, 500); + startCursorAnimation(); + } + + public TextCursorView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + startCursorAnimation(); + } + + private void startCursorAnimation() { + if (!mCursorAnimationEnabled) { + mCursorAnimationEnabled = true; + postDelayed(cursorAnimation, 500); + } } public void changePosition(RectF position) { @@ -39,45 +59,37 @@ public class TextCursorView extends ImageView { return; } - mPosition = position; - - mWidth = Math.round(position.width()); - mHeight = Math.round(position.height()); - + mCursorPosition = position; ImmutableViewportMetrics metrics = layerView.getViewportMetrics(); repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor); } public void repositionWithViewport(float x, float y, float zoom) { - RectF scaled = RectUtils.scale(mPosition, zoom); + RectF scaledRectangle = RectUtils.scale(mCursorPosition, zoom); - mLeft = Math.round(scaled.centerX() - x); - mTop = Math.round(scaled.centerY() - y); + mCursorScaledPosition = new PointF(scaledRectangle.left - x, scaledRectangle.top - y); + mCursorHeight = scaledRectangle.height(); - setScaleY(scaled.height()); - setLayoutPosition(); + invalidate(); } - private void setLayoutPosition() { - if (mLayoutParams == null) { - mLayoutParams = (RelativeLayout.LayoutParams) getLayoutParams(); - // Set negative right/bottom margins so that the handles can be dragged outside of - // the content area (if they are dragged to the left/top, the dyanmic margins set - // below will take care of that). - mLayoutParams.rightMargin = 0 - mWidth; - mLayoutParams.bottomMargin = 0 - mHeight; - } - - mLayoutParams.leftMargin = mLeft; - mLayoutParams.topMargin = mTop; - - setLayoutParams(mLayoutParams); + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + Paint paint = new Paint(); + paint.setColor(Color.BLACK); + paint.setAlpha(mCursorAlpha); + RectF cursorRect = new RectF( + mCursorScaledPosition.x, + mCursorScaledPosition.y, + mCursorScaledPosition.x + mCursorWidth, + mCursorScaledPosition.y + mCursorHeight); + canvas.drawRect(cursorRect, paint); } private Runnable cursorAnimation = new Runnable() { public void run() { - mAlpha = mAlpha == 0 ? 0xFF : 0; - getDrawable().setAlpha(mAlpha); + mCursorAlpha = mCursorAlpha == 0 ? 0xFF : 0; invalidate(); postDelayed(cursorAnimation, 500); } |