Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CView extends View
- {
- private static final String TAG = "CView";
- // default value
- private static final int DEFAULT_RADIUS = 30;
- private static final int DEFAULT_TEXT_SIZE = 12;
- private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
- private static final int DEFAULT_BACKGROUND = Color.GRAY;
- private static final int DEFAULT_WIDTH = DEFAULT_RADIUS * 2;
- private static final int DEFAULT_HEIGHT = DEFAULT_RADIUS * 2;
- // variables
- private Paint mBackgroundPaint = null;
- private int mWidth = DEFAULT_WIDTH;
- private int mHeight = DEFAULT_HEIGHT;
- // attrs
- private int mBackground = DEFAULT_BACKGROUND;
- private float mRadius = DEFAULT_RADIUS;
- private Drawable mDrawable = null;
- public CView(Context context)
- {
- super(context, null);
- }
- public CView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- init(attrs);
- }
- private void init(AttributeSet attrs)
- {
- Log.d(TAG, "init view");
- if (attrs != null)
- {
- TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CircleButton, 0, 0);
- try
- {
- mTextColor = a.getColor(R.styleable.CircleButton_textColor, DEFAULT_TEXT_COLOR);
- mRadius = a.getDimension(R.styleable.CircleButton_radius, DEFAULT_RADIUS);
- mText = a.getString(R.styleable.CircleButton_text);
- mTextSize = a.getDimensionPixelOffset(R.styleable.CircleButton_textSize, DEFAULT_TEXT_SIZE);
- mDrawable = a.getDrawable(R.styleable.CircleButton_background);
- }
- finally
- {
- a.recycle();
- }
- }
- mBackgroundPaint = new Paint();
- this.setMinimumHeight((int) mRadius * 2);
- this.setMinimumWidth((int) mRadius * 2);
- setFocusable(true);
- setClickable(true);
- }
- @Override
- protected void drawableStateChanged()
- {
- super.drawableStateChanged();
- invalidate();
- }
- private void initLayoutParams()
- {
- LayoutParams params = this.getLayoutParams();
- params.width = mWidth;
- params.height = mHeight;
- Log.d(TAG, "params| width: " + mWidth + " height: " + mHeight);
- }
- @Override
- protected void onDraw(Canvas canvas)
- {
- Log.d(TAG, "init onDraw");
- if (canvas != null && mDrawable != null)
- {
- mDrawable.setState(getDrawableState());
- canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mBackgroundPaint);
- mDrawable.draw(canvas);
- }
- }
- @Override
- protected int[] onCreateDrawableState(int extraSpace)
- {
- Log.d(TAG, "onCreateDrawableState: " + extraSpace);
- return super.onCreateDrawableState(extraSpace);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
- {
- Log.d(TAG, "init onMeasure");
- setWidth(MeasureSpec.getMode(widthMeasureSpec), MeasureSpec.getSize(widthMeasureSpec));
- setHeight(MeasureSpec.getMode(heightMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
- fixedSize();
- this.setMeasuredDimension(mWidth, mHeight);
- }
- private void fixedSize()
- {
- if (mWidth > mHeight)
- {
- mHeight = mWidth;
- }
- else if (mHeight > mWidth)
- {
- mWidth = mHeight;
- }
- }
- private void setWidth(final int modeWidth, final int width)
- {
- ViewGroup view = (ViewGroup) getParent();
- switch (modeWidth)
- {
- case MeasureSpec.AT_MOST:
- Log.d(TAG, "width mode is AT_MOST");
- if (mRadius > 0)
- {
- mWidth = (int) (mRadius * 2) + getPaddingLeft() + getPaddingRight() + view.getPaddingLeft() + view.getPaddingRight();
- }
- else
- {
- mWidth = DEFAULT_WIDTH;
- }
- mWidth = Math.min(mWidth, width);
- break;
- case MeasureSpec.EXACTLY:
- Log.d(TAG, "width mode is EXACTLY");
- break;
- case MeasureSpec.UNSPECIFIED:
- default:
- Log.d(TAG, "width mode is UNSPECIFIED");
- mWidth = DEFAULT_WIDTH;
- break;
- }
- }
- private void setHeight(final int modeHeight, final int height)
- {
- ViewGroup view = (ViewGroup) getParent();
- switch (modeHeight)
- {
- case MeasureSpec.AT_MOST:
- Log.d(TAG, "height mode is AT_MOST");
- if (mRadius > 0)
- {
- mHeight = (int) (mRadius * 2) + getPaddingTop() + getPaddingBottom() + view.getPaddingTop() + view.getPaddingBottom();
- }
- else
- {
- mHeight = DEFAULT_HEIGHT;
- }
- mHeight = Math.min(mHeight, height);
- break;
- case MeasureSpec.EXACTLY:
- Log.d(TAG, "height mode is EXACTLY");
- break;
- case MeasureSpec.UNSPECIFIED:
- default:
- Log.d(TAG, "height mode is UNSPECIFIED");
- mHeight = DEFAULT_HEIGHT;
- break;
- }
- }
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom)
- {
- // super.onLayout(changed, left, top, right, bottom);
- Log.d(TAG, "init onLayout");
- initLayoutParams();
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh)
- {
- super.onSizeChanged(w, h, oldw, oldh);
- Log.d(TAG, "init onSizeChanged width: " + w + " height: " + h + " oldw: " + oldw + " oldh: " + oldh);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement