gpt4 book ai didi

android - android中如果Viewgroups的dispatchDraw方法被覆盖,ImageViews是不可见的

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:59:34 25 4
gpt4 key购买 nike

我已经编写了一个自定义 View 来在圆圈内动态显示图像。

我重写了Viewgroup的dispatchDraw方法来画圆。在此之后,子 ImageViews 不会显示在屏幕上,如果我不重写该方法,它们就会显示在屏幕上。

这是我的类(class):

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);

如您所见, children 被画在那里。所以如果不调用dispatchDraw的super方法,childs有可能画不出来。

只需调用:

super.dispatchDraw(canvas);

关于android - android中如果Viewgroups的dispatchDraw方法被覆盖,ImageViews是不可见的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34261278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com