-
-
Save Volcanoscar/700799742ef4d39a7973 to your computer and use it in GitHub Desktop.
Custom view example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShapeView extends View { | |
private final static String tag = ShapeView.class.getSimpleName(); | |
private final int DEFAULT_SIZE_DP = 100; | |
private int measuredWidth; | |
private int measuredHeight; | |
private Shape shapeType; | |
private int color; | |
private Paint paint = new Paint(); | |
private Rect rect = new Rect(0,0,0,0); | |
private int centerX; | |
private int centerY; | |
private int radius; | |
public ShapeView(Context context) { | |
super(context); | |
} | |
public ShapeView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public ShapeView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs); | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShapeView, defStyle, 0); | |
color = a.getColor(R.styleable.ShapeView_shapeColor, Color.WHITE); | |
paint = new Paint(); | |
paint.setColor(color); | |
paint.setStyle(Style.FILL_AND_STROKE); | |
paint.setAntiAlias(true); | |
shapeType = Shape.getShapeByCode(a.getInt(R.styleable.ShapeView_shapeType, Shape.SQUARE.getCode())); | |
a.recycle(); | |
} | |
@Override | |
public void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
canvas.save(); | |
if (shapeType == Shape.SQUARE) { | |
canvas.drawRect(rect, paint); | |
} else { | |
canvas.drawCircle(centerX, centerY, radius, paint); | |
} | |
canvas.restore(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
measuredWidth = measure(widthMeasureSpec); | |
measuredHeight = measure(heightMeasureSpec); | |
setMeasuredDimension(measuredWidth, measuredHeight); | |
refreshContent(); | |
} | |
private int measure(int measureSpec) { | |
int mode = MeasureSpec.getMode(measureSpec); | |
int size = MeasureSpec.getSize(measureSpec); | |
float density = getResources().getDisplayMetrics().density; | |
if (mode == MeasureSpec.UNSPECIFIED) { | |
Log.i(tag, "measure - unspecified, size - " + size); | |
return (int) (DEFAULT_SIZE_DP * density); | |
} else if (mode == MeasureSpec.EXACTLY) { | |
Log.i(tag, "measure - exactly, size - " + size); | |
return size; | |
} else { | |
Log.i(tag, "measure - at_most, size - " + size); | |
return Math.min(size, (int) (DEFAULT_SIZE_DP * density)); | |
} | |
} | |
private void refreshContent() { | |
if (shapeType == Shape.CIRCLE) { | |
radius = Math.min(measuredWidth, measuredHeight) / 2; | |
centerX = measuredWidth / 2; | |
centerY = measuredHeight / 2; | |
} else { | |
int side = Math.min(measuredWidth, measuredHeight); | |
int left = (measuredWidth - side)/2; | |
int top = (measuredHeight - side)/2; | |
rect = new Rect(left, top, left + side, top + side); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment