1.自定义view基础
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
}
2.自定义圆形
public class Circle extends View {
public Circle(Context context) {
super(context);
}
public Circle(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int radius = 50;
int centerX = getLeft() + radius;
int centerY = getTop() + radius;
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
canvas.drawCircle(centerX, centerY, radius, paint);
}
}
3.自定义进度条
public class ProgressBar extends View {
private static final String TAG = "xiao";
private int progress;
private int max;
private int backgroundColor;
private int progressColor;
private Paint backgroundPaint;
private Paint progressPaint;
private RectF progressRect;
public ProgressBar(Context context) {
super(context);
init();
}
public ProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public ProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
progress = 0;
max = 100;
backgroundColor = Color.GRAY;
progressColor = Color.BLUE;
backgroundPaint = new Paint();
backgroundPaint.setColor(backgroundColor);
backgroundPaint.setStyle(Paint.Style.FILL);
progressPaint = new Paint();
progressPaint.setColor(progressColor);
progressPaint.setStyle(Paint.Style.FILL);
progressRect = new RectF();
}
public void setProgress(int progress) {
this.progress = progress;
invalidate();
}
public void setMax(int max) {
this.max = max;
invalidate();
}
public void setBackgroundColor(int color) {
this.backgroundColor = color;
backgroundPaint.setColor(color);
invalidate();
}
public void setProgressColor(int color) {
this.progressColor = color;
progressPaint.setColor(color);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
Log.d(TAG, "onDraw: width = " + width + ",height=" + height);
canvas.drawRect(0, 0, width, height, backgroundPaint);
float progressWidth = max * ((float) progress / max);
progressRect.set(0, 0, progressWidth, height);
canvas.drawRect(progressRect, progressPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int touchX;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchX = (int) event.getX();
case MotionEvent.ACTION_MOVE:
touchX = (int) event.getX();
Log.d(TAG, "ACTION_MOVE: touchX = " + touchX + ",max = " + max);
if (touchX < max) {
setProgress(touchX);
}
case MotionEvent.ACTION_UP:
touchX = (int) event.getX();
Log.d(TAG, "ACTION_UP: touchX = " + touchX + ",max = " + max);
if (touchX < max) {
setProgress(touchX);
}
}
return super.onTouchEvent(event);
}
}
4.自定义ViewGroup,子view依次在最左,最右
public class CustomLinearLayout extends ViewGroup {
public CustomLinearLayout(Context context) {
super(context);
}
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
width = Math.max(width, child.getMeasuredWidth());
height += child.getMeasuredHeight();
}
width += getPaddingLeft() + getPaddingRight();
height += getPaddingTop() + getPaddingBottom();
width = resolveSizeAndState(width, widthMeasureSpec, 0);
height = resolveSizeAndState(height, heightMeasureSpec, 0);
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int top = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if ((i & 1) == 0) {
child.layout(0, top, child.getMeasuredWidth(), top + child.getMeasuredHeight());
} else {
child.layout(getMeasuredWidth() - child.getMeasuredWidth(), top, getMeasuredWidth(), top + child.getMeasuredHeight());
top += child.getMeasuredHeight();
}
}
}
}