summaryrefslogtreecommitdiffstats
path: root/android/source/src/java/org/libreoffice/canvas/BitmapHandle.java
blob: 51f6f7cf861187feeef21aa7cd52ec52e36c3fd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.libreoffice.canvas;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import androidx.core.content.ContextCompat;

/**
 * Bitmap handle canvas element is used to show a handle on the screen.
 * The handle visual comes from the bitmap, which must be provided in time
 * of construction.
 */
public abstract class BitmapHandle extends CommonCanvasElement {
    public final RectF mDocumentPosition;
    private final Bitmap mBitmap;
    final RectF mScreenPosition;

    BitmapHandle(Bitmap bitmap) {
        mBitmap = bitmap;
        mScreenPosition = new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
        mDocumentPosition = new RectF();
    }

    /**
     * Return a bitmap for a drawable id.
     */
    static Bitmap getBitmapForDrawable(Context context, int drawableId) {
        Drawable drawable = ContextCompat.getDrawable(context, drawableId);

        return ImageUtils.getBitmapForDrawable(drawable);
    }

    /**
     * Draw the bitmap handle to the canvas.
     * @param canvas - the canvas
     */
    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, mScreenPosition.left, mScreenPosition.top, null);
    }

    /**
     * Test if the bitmap has been hit.
     * @param x - x coordinate
     * @param y - y coordinate
     * @return true if the bitmap has been hit
     */
    @Override
    public boolean onHitTest(float x, float y) {
        return mScreenPosition.contains(x, y);
    }

    /**
     * Change the position of the handle.
     * @param x - x coordinate
     * @param y - y coordinate
     */
    public void reposition(float x, float y) {
        mScreenPosition.offsetTo(x, y);
    }
}