- onDraw()
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, paint); } //这段代码实现了一个自定义的 View,绘制了一个红色圆形。
- onMeasure()
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = Math.min(width, height);
setMeasuredDimension(size, size);
}
//这段代码实现了一个正方形的 View,通过测量获取 View 的宽高,并设置为相等的大小。
- onLayout()
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); int childCount = getChildCount(); int width = getWidth(); int height = getHeight(); int childWidth = width / childCount; for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); childView.layout(i * childWidth, 0, (i + 1) * childWidth, height); } } //这段代码实现了一个自定义的 ViewGroup,根据子 View 的数量,将父容器分成若干个等宽的区域,并将每个子 View 放置在对应的区域中。
onTouchEvent()
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 处理按下事件
break;
case MotionEvent.ACTION_MOVE:
// 处理移动事件
break;
case MotionEvent.ACTION_UP:
// 处理抬起事件
break;
}
return true;
}
//这段代码实现了一个自定义的 View,处理了用户的触摸事件,根据不同的事件类型执行不同的操作。
希望这些示例代码能够帮助您更好地理解 Android 中 View 重写方法的使用。
ViewGroup重写
package com.yu.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context) {
super(context);
}
public MyViewGroup(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int childCount = getChildCount();
int childWidth = width / childCount;
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int childCount = getChildCount();
int width = getWidth();
int height = getHeight();
int childWidth = width / childCount;
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
childView.layout(i * childWidth, 0, (i + 1) * childWidth, height);
}
}
}