[英]ImageViews are invisible if Viewgroups's dispatchDraw method is overwritten in android
我编写了一个自定义视图,以便在圆圈内动态显示图像。
我已经覆盖了Viewgroup的dispatchDraw方法来绘制圆圈。 在此之后,子ImageViews不会显示在屏幕上,如果我没有覆盖该方法,那么它们将显示在屏幕上。
这是我的班级:
public class CustomView extends RelativeLayout {
private Paint paint;
private View mView;
private Context context;
private void init(Context context) {
LinearLayout layout = new LinearLayout(context);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
// Set generic layout parameters
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button button = new Button(context);
button.setText("Button!");
layout.addView(button, params); // Modify this
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.coffe_selected);
layout.addView(imageView);
this.addView(layout);
}
public CustomView(Context mContext) {
super(mContext);
context = mContext;
// create the Paint and set its color
paint = new Paint();
paint.setColor(0xFF1f5b83);
init(context);
}
@Override
protected void dispatchDraw(Canvas canvas) {
int width = this.getWidth();
int height = this.getHeight();
canvas.drawCircle(width / 2, height / 2-64, 200, paint);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
看一下ViewGroup的源代码以及dispatchDraw
发生的事情。
只有一行:
more |= drawChild(canvas, transientChild, drawingTime);
你可以看到,孩子们被吸引到那里。 因此,如果您不调用dispatchDraw
的超级方法,则可能未绘制子项。
只需致电:
super.dispatchDraw(canvas);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.