13ed120d2204c73ab20d6093f112367de60129eb3John Reck/*
23ed120d2204c73ab20d6093f112367de60129eb3John Reck * Copyright (C) 2012 The Android Open Source Project
33ed120d2204c73ab20d6093f112367de60129eb3John Reck *
43ed120d2204c73ab20d6093f112367de60129eb3John Reck * Licensed under the Apache License, Version 2.0 (the "License");
53ed120d2204c73ab20d6093f112367de60129eb3John Reck * you may not use this file except in compliance with the License.
63ed120d2204c73ab20d6093f112367de60129eb3John Reck * You may obtain a copy of the License at
73ed120d2204c73ab20d6093f112367de60129eb3John Reck *
83ed120d2204c73ab20d6093f112367de60129eb3John Reck *      http://www.apache.org/licenses/LICENSE-2.0
93ed120d2204c73ab20d6093f112367de60129eb3John Reck *
103ed120d2204c73ab20d6093f112367de60129eb3John Reck * Unless required by applicable law or agreed to in writing, software
113ed120d2204c73ab20d6093f112367de60129eb3John Reck * distributed under the License is distributed on an "AS IS" BASIS,
123ed120d2204c73ab20d6093f112367de60129eb3John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133ed120d2204c73ab20d6093f112367de60129eb3John Reck * See the License for the specific language governing permissions and
143ed120d2204c73ab20d6093f112367de60129eb3John Reck * limitations under the License.
153ed120d2204c73ab20d6093f112367de60129eb3John Reck */
163ed120d2204c73ab20d6093f112367de60129eb3John Reck
173ed120d2204c73ab20d6093f112367de60129eb3John Reckpackage com.android.camera.drawable;
183ed120d2204c73ab20d6093f112367de60129eb3John Reck
193ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.content.res.Resources;
203ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.graphics.Canvas;
213ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.graphics.Color;
223ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.graphics.ColorFilter;
233ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.graphics.Paint;
243ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.graphics.Paint.Align;
253ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.graphics.Rect;
263ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.graphics.drawable.Drawable;
273ed120d2204c73ab20d6093f112367de60129eb3John Reckimport android.util.TypedValue;
283ed120d2204c73ab20d6093f112367de60129eb3John Reck
293ed120d2204c73ab20d6093f112367de60129eb3John Reck
303ed120d2204c73ab20d6093f112367de60129eb3John Reckpublic class TextDrawable extends Drawable {
313ed120d2204c73ab20d6093f112367de60129eb3John Reck
323ed120d2204c73ab20d6093f112367de60129eb3John Reck    private static final int DEFAULT_COLOR = Color.WHITE;
333ed120d2204c73ab20d6093f112367de60129eb3John Reck    private static final int DEFAULT_TEXTSIZE = 15;
343ed120d2204c73ab20d6093f112367de60129eb3John Reck
353ed120d2204c73ab20d6093f112367de60129eb3John Reck    private Paint mPaint;
363ed120d2204c73ab20d6093f112367de60129eb3John Reck    private CharSequence mText;
373ed120d2204c73ab20d6093f112367de60129eb3John Reck    private int mIntrinsicWidth;
383ed120d2204c73ab20d6093f112367de60129eb3John Reck    private int mIntrinsicHeight;
393ed120d2204c73ab20d6093f112367de60129eb3John Reck
403ed120d2204c73ab20d6093f112367de60129eb3John Reck    public TextDrawable(Resources res, CharSequence text) {
413ed120d2204c73ab20d6093f112367de60129eb3John Reck        mText = text;
423ed120d2204c73ab20d6093f112367de60129eb3John Reck        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
433ed120d2204c73ab20d6093f112367de60129eb3John Reck        mPaint.setColor(DEFAULT_COLOR);
443ed120d2204c73ab20d6093f112367de60129eb3John Reck        mPaint.setTextAlign(Align.CENTER);
453ed120d2204c73ab20d6093f112367de60129eb3John Reck        float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
463ed120d2204c73ab20d6093f112367de60129eb3John Reck                DEFAULT_TEXTSIZE, res.getDisplayMetrics());
473ed120d2204c73ab20d6093f112367de60129eb3John Reck        mPaint.setTextSize(textSize);
483ed120d2204c73ab20d6093f112367de60129eb3John Reck        mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
493ed120d2204c73ab20d6093f112367de60129eb3John Reck        mIntrinsicHeight = mPaint.getFontMetricsInt(null);
503ed120d2204c73ab20d6093f112367de60129eb3John Reck    }
513ed120d2204c73ab20d6093f112367de60129eb3John Reck
523ed120d2204c73ab20d6093f112367de60129eb3John Reck    @Override
533ed120d2204c73ab20d6093f112367de60129eb3John Reck    public void draw(Canvas canvas) {
543ed120d2204c73ab20d6093f112367de60129eb3John Reck        Rect bounds = getBounds();
553ed120d2204c73ab20d6093f112367de60129eb3John Reck        canvas.drawText(mText, 0, mText.length(),
563ed120d2204c73ab20d6093f112367de60129eb3John Reck                bounds.centerX(), bounds.centerY(), mPaint);
573ed120d2204c73ab20d6093f112367de60129eb3John Reck    }
583ed120d2204c73ab20d6093f112367de60129eb3John Reck
593ed120d2204c73ab20d6093f112367de60129eb3John Reck    @Override
603ed120d2204c73ab20d6093f112367de60129eb3John Reck    public int getOpacity() {
613ed120d2204c73ab20d6093f112367de60129eb3John Reck        return mPaint.getAlpha();
623ed120d2204c73ab20d6093f112367de60129eb3John Reck    }
633ed120d2204c73ab20d6093f112367de60129eb3John Reck
643ed120d2204c73ab20d6093f112367de60129eb3John Reck    @Override
653ed120d2204c73ab20d6093f112367de60129eb3John Reck    public int getIntrinsicWidth() {
663ed120d2204c73ab20d6093f112367de60129eb3John Reck        return mIntrinsicWidth;
673ed120d2204c73ab20d6093f112367de60129eb3John Reck    }
683ed120d2204c73ab20d6093f112367de60129eb3John Reck
693ed120d2204c73ab20d6093f112367de60129eb3John Reck    @Override
703ed120d2204c73ab20d6093f112367de60129eb3John Reck    public int getIntrinsicHeight() {
713ed120d2204c73ab20d6093f112367de60129eb3John Reck        return mIntrinsicHeight;
723ed120d2204c73ab20d6093f112367de60129eb3John Reck    }
733ed120d2204c73ab20d6093f112367de60129eb3John Reck
743ed120d2204c73ab20d6093f112367de60129eb3John Reck    @Override
753ed120d2204c73ab20d6093f112367de60129eb3John Reck    public void setAlpha(int alpha) {
763ed120d2204c73ab20d6093f112367de60129eb3John Reck        mPaint.setAlpha(alpha);
773ed120d2204c73ab20d6093f112367de60129eb3John Reck    }
783ed120d2204c73ab20d6093f112367de60129eb3John Reck
793ed120d2204c73ab20d6093f112367de60129eb3John Reck    @Override
803ed120d2204c73ab20d6093f112367de60129eb3John Reck    public void setColorFilter(ColorFilter filter) {
813ed120d2204c73ab20d6093f112367de60129eb3John Reck        mPaint.setColorFilter(filter);
823ed120d2204c73ab20d6093f112367de60129eb3John Reck    }
833ed120d2204c73ab20d6093f112367de60129eb3John Reck
843ed120d2204c73ab20d6093f112367de60129eb3John Reck}
85