summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/UserLocationViewDrawableHolder.java
blob: 131a0aecb369668093cf747dfbb5a3ff7a2bd513 (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
package com.mapbox.mapboxsdk.maps.widgets;

import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


/**
 * <p>Holder for the UserLocationView drawables.</p>
 * <p>Holds the drawable and its shadow if set, and manage their initialization.</p>
 */
class UserLocationViewDrawableHolder {
    Drawable mDrawable;
    Drawable mShadow;
    final RectF mBounds = new RectF();
    final RectF mShadowBounds = new RectF();

    public UserLocationViewDrawableHolder() {
    }

    public UserLocationViewDrawableHolder(Drawable drawable, Drawable shadow) {
        setDrawable(drawable);
        setShadow(shadow);
    }

    void setDrawable(@NonNull Drawable drawable) {
        mDrawable = drawable;
        setDrawable(mDrawable, mBounds);
    }

    void setShadow(@Nullable Drawable drawable) {
        mShadow = drawable;
        setDrawable(mShadow, mShadowBounds);
    }

    boolean hasShadow() {
        return mShadow != null;
    }

    private static void setDrawable(final @Nullable Drawable drawable, final @NonNull RectF bounds) {
        if (drawable != null) {
            int halfWidth = drawable.getIntrinsicWidth() / 2;
            int halfHeight = drawable.getIntrinsicHeight() / 2;
            bounds.set(
                    -halfWidth,
                    -halfHeight,
                    halfWidth,
                    halfHeight);
            drawable.setBounds(new Rect(
                    -halfWidth,
                    -halfHeight,
                    halfWidth,
                    halfHeight));
        } else {
            bounds.setEmpty();
        }
    }
}