Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wiagames;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.view.View;
- /**
- * FastTextView class for fast text changing
- */
- public class FastTextView extends View {
- private Context mContext;
- private CharSequence mText = "";
- private int mTextSize;
- private Paint mPaint;
- public void setText(CharSequence text) {
- mText = text;
- }
- public FastTextView(Context context) {
- super(context);
- initialize();
- }
- public FastTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- initialize();
- initializeAttributes(attrs);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- Rect rect = new Rect();
- mPaint.getTextBounds(mText.toString(), 0, mText.length(), rect);
- int width = Math.max(rect.width(), getSuggestedMinimumWidth());
- int height = Math.max(rect.height(), getSuggestedMinimumHeight());
- setMeasuredDimension(width, height);
- }
- /**
- * Initialize the view
- */
- private void initialize() {
- mContext = getContext();
- mPaint = new Paint();
- mPaint.setColor(Color.RED);
- mPaint.setStyle(Paint.Style.FILL);
- setBackgroundColor(Color.GREEN);
- }
- /**
- * Initialize the view's attributes
- */
- private void initializeAttributes(AttributeSet attrs) {
- TypedArray attributes = mContext.obtainStyledAttributes(attrs, R.styleable.FastTextView);
- CharSequence text = attributes.getText(R.styleable.FastTextView_text);
- if (text != null) {
- mText = text;
- }
- int textSize = attributes.getInt(R.styleable.FastTextView_textSize, -1);
- if (textSize != -1) {
- mTextSize = textSize;
- }
- // FIXME
- mText = "Test";
- mTextSize = 200;
- mPaint.setTextSize(mTextSize);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- if (mText.length() > 0) {
- canvas.drawText(mText, 0, mText.length() - 1, 0, 0, mPaint);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement