Advertisement
Guest User

Untitled

a guest
Sep 13th, 2012
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package com.wiagames;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Rect;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11.  
  12. /**
  13.  * FastTextView class for fast text changing
  14.  */
  15. public class FastTextView extends View {
  16.  
  17.     private Context mContext;
  18.     private CharSequence mText = "";
  19.     private int mTextSize;
  20.  
  21.     private Paint mPaint;
  22.  
  23.     public void setText(CharSequence text) {
  24.         mText = text;
  25.     }
  26.  
  27.     public FastTextView(Context context) {
  28.         super(context);
  29.  
  30.         initialize();
  31.     }
  32.  
  33.     public FastTextView(Context context, AttributeSet attrs) {
  34.         super(context, attrs);
  35.  
  36.         initialize();
  37.         initializeAttributes(attrs);
  38.     }
  39.  
  40.     @Override
  41.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  42.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  43.  
  44.         Rect rect = new Rect();
  45.         mPaint.getTextBounds(mText.toString(), 0, mText.length(), rect);
  46.  
  47.         int width = Math.max(rect.width(), getSuggestedMinimumWidth());
  48.         int height = Math.max(rect.height(), getSuggestedMinimumHeight());
  49.  
  50.         setMeasuredDimension(width, height);
  51.     }
  52.  
  53.     /**
  54.      * Initialize the view
  55.      */
  56.     private void initialize() {
  57.         mContext = getContext();
  58.         mPaint = new Paint();
  59.         mPaint.setColor(Color.RED);
  60.         mPaint.setStyle(Paint.Style.FILL);
  61.         setBackgroundColor(Color.GREEN);
  62.     }
  63.  
  64.     /**
  65.      * Initialize the view's attributes
  66.      */
  67.     private void initializeAttributes(AttributeSet attrs) {
  68.         TypedArray attributes = mContext.obtainStyledAttributes(attrs, R.styleable.FastTextView);
  69.  
  70.         CharSequence text = attributes.getText(R.styleable.FastTextView_text);
  71.         if (text != null) {
  72.             mText = text;
  73.         }
  74.  
  75.         int textSize = attributes.getInt(R.styleable.FastTextView_textSize, -1);
  76.         if (textSize != -1) {
  77.             mTextSize = textSize;
  78.         }
  79.  
  80.         // FIXME
  81.         mText = "Test";
  82.         mTextSize = 200;
  83.         mPaint.setTextSize(mTextSize);
  84.     }
  85.  
  86.     @Override
  87.     protected void onDraw(Canvas canvas) {
  88.         super.onDraw(canvas);
  89.  
  90.         if (mText.length() > 0) {
  91.             canvas.drawText(mText, 0, mText.length() - 1, 0, 0, mPaint);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement