summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/NinePatchTexture.java
blob: 4dd772a262470cfd320aa4785a6c47f48a18f677 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.android.camera.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.NinePatchDrawable;

import javax.microedition.khronos.opengles.GL11;

public class NinePatchTexture extends FrameTexture {

    private MyTexture mDelegate;

    private NinePatchDrawable mNinePatch;

    private final Context mContext;
    private final int mResId;

    private int mLastWidth = -1;
    private int mLastHeight = -1;

    private final Rect mPaddings = new Rect();

    public NinePatchTexture(Context context, int resId) {
        this.mContext = context;
        this.mResId = resId;
    }

    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);
    }

    @Override
    public boolean bind(GLRootView root, GL11 gl) {
        if (mLastWidth != mWidth || mLastHeight != mHeight) {
            if (mDelegate != null) mDelegate.deleteFromGL(gl);
            mDelegate = new MyTexture(mWidth, mHeight);
            mLastWidth = mWidth;
            mLastHeight = mHeight;
        }
        return mDelegate.bind(root, gl);
    }

    protected NinePatchDrawable getNinePatch() {
        if (mNinePatch == null) {
            mNinePatch = (NinePatchDrawable)
                    mContext.getResources().getDrawable(mResId);
            mNinePatch.getPadding(mPaddings);
        }
        return mNinePatch;
    }

    private class MyTexture extends CanvasTexture {

        public MyTexture(int width, int height) {
            super(width, height);
        }

        @Override
        protected void onDraw (Canvas canvas, Bitmap backing) {
            NinePatchDrawable npd = getNinePatch();
            npd.setBounds(0, 0, mWidth, mHeight);
            npd.draw(canvas);
        }
    }

    @Override
    protected void freeBitmap(Bitmap bitmap) {
        mDelegate.freeBitmap(bitmap);
    }

    @Override
    protected Bitmap getBitmap() {
        return mDelegate.getBitmap();
    }

    public int getIntrinsicWidth() {
        return getNinePatch().getIntrinsicWidth();
    }

    public int getIntrinsicHeight() {
        return getNinePatch().getIntrinsicHeight();
    }

    @Override
    public Rect getPaddings() {
        // get the paddings from nine patch
        if (mNinePatch == null) getNinePatch();
        return mPaddings;
    }
}