Advertisement
Guest User

CircleView

a guest
Dec 9th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. public class CView extends View
  2. {
  3.     private static final String TAG = "CView";
  4.  
  5.     // default value
  6.     private static final int DEFAULT_RADIUS = 30;
  7.     private static final int DEFAULT_TEXT_SIZE = 12;
  8.     private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
  9.     private static final int DEFAULT_BACKGROUND = Color.GRAY;
  10.     private static final int DEFAULT_WIDTH = DEFAULT_RADIUS * 2;
  11.     private static final int DEFAULT_HEIGHT = DEFAULT_RADIUS * 2;
  12.  
  13.     // variables
  14.     private Paint mBackgroundPaint = null;
  15.     private int mWidth = DEFAULT_WIDTH;
  16.     private int mHeight = DEFAULT_HEIGHT;
  17.  
  18.     // attrs
  19.     private int mBackground = DEFAULT_BACKGROUND;
  20.     private float mRadius = DEFAULT_RADIUS;
  21.     private Drawable mDrawable = null;
  22.  
  23.     public CView(Context context)
  24.     {
  25.         super(context, null);
  26.     }
  27.  
  28.     public CView(Context context, AttributeSet attrs)
  29.     {
  30.         super(context, attrs);
  31.         init(attrs);
  32.     }
  33.  
  34.     private void init(AttributeSet attrs)
  35.     {
  36.         Log.d(TAG, "init view");
  37.         if (attrs != null)
  38.         {
  39.             TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CircleButton, 0, 0);
  40.             try
  41.             {
  42.                 mTextColor = a.getColor(R.styleable.CircleButton_textColor, DEFAULT_TEXT_COLOR);
  43.                 mRadius = a.getDimension(R.styleable.CircleButton_radius, DEFAULT_RADIUS);
  44.                 mText = a.getString(R.styleable.CircleButton_text);
  45.                 mTextSize = a.getDimensionPixelOffset(R.styleable.CircleButton_textSize, DEFAULT_TEXT_SIZE);
  46.                 mDrawable = a.getDrawable(R.styleable.CircleButton_background);
  47.             }
  48.             finally
  49.             {
  50.                 a.recycle();
  51.             }
  52.         }
  53.         mBackgroundPaint = new Paint();
  54.  
  55.         this.setMinimumHeight((int) mRadius * 2);
  56.         this.setMinimumWidth((int) mRadius * 2);
  57.         setFocusable(true);
  58.         setClickable(true);
  59.     }
  60.  
  61.     @Override
  62.     protected void drawableStateChanged()
  63.     {
  64.         super.drawableStateChanged();
  65.         invalidate();
  66.     }
  67.  
  68.     private void initLayoutParams()
  69.     {
  70.         LayoutParams params = this.getLayoutParams();
  71.         params.width = mWidth;
  72.         params.height = mHeight;
  73.         Log.d(TAG, "params| width: " + mWidth + " height: " + mHeight);
  74.     }
  75.  
  76.     @Override
  77.     protected void onDraw(Canvas canvas)
  78.     {
  79.         Log.d(TAG, "init onDraw");
  80.         if (canvas != null && mDrawable != null)
  81.         {
  82.             mDrawable.setState(getDrawableState());
  83.  
  84.             canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mBackgroundPaint);
  85.  
  86.             mDrawable.draw(canvas);
  87.         }
  88.     }
  89.    
  90.     @Override
  91.     protected int[] onCreateDrawableState(int extraSpace)
  92.     {
  93.         Log.d(TAG, "onCreateDrawableState: " + extraSpace);
  94.         return super.onCreateDrawableState(extraSpace);
  95.     }
  96.  
  97.     @Override
  98.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  99.     {
  100.         Log.d(TAG, "init onMeasure");
  101.         setWidth(MeasureSpec.getMode(widthMeasureSpec), MeasureSpec.getSize(widthMeasureSpec));
  102.         setHeight(MeasureSpec.getMode(heightMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
  103.         fixedSize();
  104.         this.setMeasuredDimension(mWidth, mHeight);
  105.  
  106.     }
  107.  
  108.     private void fixedSize()
  109.     {
  110.         if (mWidth > mHeight)
  111.         {
  112.             mHeight = mWidth;
  113.         }
  114.         else if (mHeight > mWidth)
  115.         {
  116.             mWidth = mHeight;
  117.         }
  118.     }
  119.  
  120.     private void setWidth(final int modeWidth, final int width)
  121.     {
  122.         ViewGroup view = (ViewGroup) getParent();
  123.         switch (modeWidth)
  124.         {
  125.             case MeasureSpec.AT_MOST:
  126.                 Log.d(TAG, "width mode is AT_MOST");
  127.                 if (mRadius > 0)
  128.                 {
  129.                     mWidth = (int) (mRadius * 2) + getPaddingLeft() + getPaddingRight() + view.getPaddingLeft() + view.getPaddingRight();
  130.                 }
  131.                 else
  132.                 {
  133.                     mWidth = DEFAULT_WIDTH;
  134.                 }
  135.                 mWidth = Math.min(mWidth, width);
  136.                 break;
  137.             case MeasureSpec.EXACTLY:
  138.                 Log.d(TAG, "width mode is EXACTLY");
  139.                 break;
  140.             case MeasureSpec.UNSPECIFIED:
  141.             default:
  142.                 Log.d(TAG, "width mode is UNSPECIFIED");
  143.                 mWidth = DEFAULT_WIDTH;
  144.                 break;
  145.         }
  146.     }
  147.  
  148.     private void setHeight(final int modeHeight, final int height)
  149.     {
  150.         ViewGroup view = (ViewGroup) getParent();
  151.         switch (modeHeight)
  152.         {
  153.             case MeasureSpec.AT_MOST:
  154.                 Log.d(TAG, "height mode is AT_MOST");
  155.                 if (mRadius > 0)
  156.                 {
  157.                     mHeight = (int) (mRadius * 2) + getPaddingTop() + getPaddingBottom() + view.getPaddingTop() + view.getPaddingBottom();
  158.                 }
  159.                 else
  160.                 {
  161.                     mHeight = DEFAULT_HEIGHT;
  162.                 }
  163.                 mHeight = Math.min(mHeight, height);
  164.                 break;
  165.             case MeasureSpec.EXACTLY:
  166.                 Log.d(TAG, "height mode is EXACTLY");
  167.                 break;
  168.             case MeasureSpec.UNSPECIFIED:
  169.             default:
  170.                 Log.d(TAG, "height mode is UNSPECIFIED");
  171.                 mHeight = DEFAULT_HEIGHT;
  172.                 break;
  173.         }
  174.     }
  175.  
  176.     @Override
  177.     protected void onLayout(boolean changed, int left, int top, int right, int bottom)
  178.     {
  179.         // super.onLayout(changed, left, top, right, bottom);
  180.         Log.d(TAG, "init onLayout");
  181.         initLayoutParams();
  182.     }
  183.  
  184.     @Override
  185.     protected void onSizeChanged(int w, int h, int oldw, int oldh)
  186.     {
  187.         super.onSizeChanged(w, h, oldw, oldh);
  188.         Log.d(TAG, "init onSizeChanged width: " + w + " height: " + h + " oldw: " + oldw + " oldh: " + oldh);
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement