Skip to content

Instantly share code, notes, and snippets.

@yageek
Created July 15, 2013 19:58
Show Gist options
  • Save yageek/6002910 to your computer and use it in GitHub Desktop.
Save yageek/6002910 to your computer and use it in GitHub Desktop.
View Group question
public class ChartElementView extends ViewGroup {
private final String myTag = "ChartElementView";
private RectF mHorizontalBarBounds = new RectF();
private HorizontalBar mChartBar;
private Parking mParkingData;
private Paint mHorizontalBarPaint;
private int mHorizontalBarColor;
public ChartElementView(Context context) {
super(context);
init();
}
public ChartElementView(Context context,AttributeSet attrs){
super(context,attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ChartElementView,0,0);
try{
mHorizontalBarColor = a.getColor(R.styleable.ChartElementView_barColor,0xff000000);
}
finally {
a.recycle();
}
init();
}
private void init(){
mHorizontalBarPaint = new Paint();
mHorizontalBarPaint.setStyle(Paint.Style.STROKE);
mHorizontalBarPaint.setColor(0xFF33B5E5);
mHorizontalBarPaint.setStrokeWidth(4);
mChartBar = new HorizontalBar(getContext());
addView(mChartBar);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
final int desiredWidth = 100;
final int desiredHeight = 25;
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width;
int height;
//Measure Width
if(widthMode == MeasureSpec.EXACTLY)
{
width = widthSize;
}
else if(widthMode == MeasureSpec.AT_MOST){
width = Math.min(desiredWidth,widthSize);
}
else{
width = desiredWidth;
}
//Measure Width
if(heightMode == MeasureSpec.EXACTLY)
{
height = heightSize;
}
else if(heightMode == MeasureSpec.AT_MOST){
height = Math.min(desiredHeight,heightSize);
}
else{
height = desiredHeight;
}
setMeasuredDimension(width,height);
getChildAt(0).measure(
MeasureSpec.makeMeasureSpec(width / 2,
MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height / 2,
MeasureSpec.EXACTLY));
}
@Override
protected void onLayout(boolean b, int i, int i2, int i3, int i4) {
getChildAt(0).layout(0, 0, getMeasuredWidth() / 2,
getMeasuredHeight() / 2);
// the second child
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Log.d(myTag,"onDraw ChartElementView");
canvas.drawText("Teste",10,10,mHorizontalBarPaint);
}
private class HorizontalBar extends View {
public HorizontalBar(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Log.d(myTag,"onDraw ChartElementView");
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(0xFF33B5E5);
paint.setStrokeWidth(4);
paint.setTextSize(20);
canvas.drawText("Private class",50,50, paint);
}
}
public void setParkingData(Parking mParkingData) {
this.mParkingData = mParkingData;
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment