summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/CachedTextView.java
blob: f9f68a0d49a9dede14741cf87885cd5f0464f2d2 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher2;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.util.AttributeSet;
import android.widget.TextView;

/*
 * This class is a bit of a hack, designed to speed up long text labels in Launcher. It caches the
 * text in a TextView to a bitmap and then just draws that Bitmap instead afterward, speeding up
 * rendering. Marquee scrolling is not currently supported.
 *
 */
public class CachedTextView extends TextView {
    private Bitmap mCache;
    private final Paint mCachePaint = new Paint();
    private final Canvas mCacheCanvas = new Canvas();

    private int mPrevAlpha = -1;
    private boolean mIsBuildingCache;
    boolean mIsTextCacheDirty;
    float mTextCacheLeft;
    float mTextCacheTop;
    float mTextCacheScrollX;
    float mRectLeft, mRectTop;
    private float mPaddingH = 0;
    private float mPaddingV = 0;
    private CharSequence mText;
    private boolean mEnabled = true;

    public CachedTextView(Context context) {
        super(context);
    }

    public CachedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CachedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    protected int getCacheTopPadding() {
        return 0;
    }
    protected int getCacheLeftPadding() {
        return 0;
    }
    protected int getCacheRightPadding() {
        return 0;
    }
    protected int getCacheBottomPadding() {
        return 0;
    }

    public void disableCache() {
        mEnabled = false;
    }

    public void setText(CharSequence text, BufferType type) {
        super.setText(text, type);
        mIsTextCacheDirty = true;
    }

    private void buildAndUpdateCache() {
        final Layout layout = getLayout();
        final int left = getCompoundPaddingLeft();
        final int top = getExtendedPaddingTop();
        final float prevAlpha = getAlpha();

        mTextCacheLeft = layout.getLineLeft(0) - getCacheLeftPadding();
        mTextCacheTop = top + layout.getLineTop(0) - mPaddingV - getCacheTopPadding();

        mRectLeft = mScrollX + getLeft();
        mRectTop = 0;
        mTextCacheScrollX = mScrollX;

        final float textCacheRight =
            Math.min(left + layout.getLineRight(0) + mPaddingH, mScrollX + mRight - mLeft) +
            getCacheRightPadding();
        final float textCacheBottom = top + layout.getLineBottom(0) + mPaddingV +
            getCacheBottomPadding();
        final float xCharWidth = getPaint().measureText("x");

        int width = (int) (textCacheRight - mTextCacheLeft + (2 * xCharWidth));
        int height = (int) (textCacheBottom - mTextCacheTop);

        if (width > 0 && height > 0) {
            if (mCache != null) {
                if (mCache.getWidth() != width || mCache.getHeight() != height) {
                    mCache.recycle();
                    mCache = null;
                }
            }
            if (mCache == null) {
                mCache = Bitmap.createBitmap(width, height, Config.ARGB_8888);
                mCacheCanvas.setBitmap(mCache);
            } else {
                mCacheCanvas.drawColor(0, Mode.CLEAR);
            }

            mCacheCanvas.save();
            mCacheCanvas.translate(-mTextCacheLeft, -mTextCacheTop);

            mIsBuildingCache = true;
            setAlpha(1.0f);
            draw(mCacheCanvas);
            setAlpha(prevAlpha);
            mIsBuildingCache = false;
            mCacheCanvas.restore();
            mCacheCanvas.setBitmap(null);

            // A hack-- we set the text to be one space (we don't make it empty just to avoid any
            // potential issues with text measurement, like line height, etc.) so that the text view
            // doesn't draw it anymore, since it's been cached.
            mText = getText();
            setText(" ");
        }
    }

    public CharSequence getText() {
        return (mText == null) ? super.getText() : mText;
    }

    public void draw(Canvas canvas) {
        if (mEnabled && mIsTextCacheDirty && !mIsBuildingCache) {
            buildAndUpdateCache();
            mIsTextCacheDirty = false;
        }
        if (mCache != null && !mIsBuildingCache) {
            canvas.drawBitmap(mCache, mTextCacheLeft - mTextCacheScrollX + mScrollX,
                    mTextCacheTop, mCachePaint);
        }
        super.draw(canvas);
    }

    protected boolean isBuildingCache() {
        return mIsBuildingCache;
    }

    @Override
    protected boolean onSetAlpha(int alpha) {
        if (mPrevAlpha != alpha) {
            mPrevAlpha = alpha;
            mCachePaint.setAlpha(alpha);

            // We manually update the drawables alpha since the default TextView implementation may
            // not do this if there is a background set (which we may due to the focus bg)
            final Drawable[] dr = getCompoundDrawables();
            for (int i = 0; i < dr.length; ++i) {
                if (dr[i] != null) {
                    dr[i].mutate().setAlpha(alpha);
                }
            }

            super.onSetAlpha(alpha);
        }
        return true;
    }
}