2

我有一个自定义视图,并希望在该自定义视图上再添加一个自定义视图。这是我的自定义视图类:

public class CustomCircle extends View{

float radius;
Paint paint = new Paint();

String message = "";
public CustomCircle(Context context, AttributeSet attrs) {
    super(context, attrs);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(getWidth()/2, getHeight()/2,radius, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean isClickInCircle = false;
    float x = event.getX();
    float y = event.getY();

    double check = Math.sqrt((x-getWidth()/2)*(x-getWidth()/2) + (y-getHeight()/2)*(y-getHeight()/2));
    if (check<=radius) {
        isClickInCircle= true;
    }
    if (isClickInCircle) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            Toast.makeText(getContext(),message, Toast.LENGTH_LONG).show();
            return true;

        case MotionEvent.ACTION_UP:
            Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
            return true;

        default:
            break;
        }
    }

    return false;
}

并且正在使用另一个扩展的类LinearLayout

public class B extends LinearLayout {

private Paint paint;

public B(Context context) {
    super(context);
    paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStyle(Style.FILL);
}

public void addCircle() {

    CustomCircle circleBlue = new CustomCircle(getContext(), null);
    circleBlue.paint.setColor(Color.WHITE);
    circleBlue.paint.setAntiAlias(true);
    circleBlue.radius = 160;
    circleBlue.message = "Clicked";
    addView(circleBlue);

    CustomCircle circleRed = new CustomCircle(getContext(), null);
    circleRed.paint.setColor(Color.RED);
    circleRed.paint.setAntiAlias(true);
    circleRed.radius = 80;
    circleRed.message = "Clicked";
    addView(circleRed);

}

我正在使用主要活动类调用 B 类:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    B root = new B(this);
    root.addCircle();
    setContentView(root);
}

输出只显示一个圆圈而不是另一个圆圈内的圆圈。我的代码有什么问题?

4

2 回答 2

1

输出只显示一个圆圈而不是圆圈内的圆圈。

如果你想重叠孩子,你选择了错误的布局,RelativeLayout或者FrameLayout是要走的路。另外,关于您的代码:

public class B extends RelativeLayout {
//...
public void addCircle() {

    // the constructor that uses the AttributeSet should be added if you use the 
    // custom component in the xml layout
    CustomCircle circleBlue = new CustomCircle(getContext());
    // ...
    // add it with LayoutParams
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
    addView(circleBlue, rlp);
    // the same for the other view
}

此外,您的两个圆圈将具有相同的尺寸,因此它们将完美重叠(并且您将无法看到它们),您需要通过 给它们不同的尺寸,LayoutParams例如:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(200, 200);

对于第一个,并且:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(100, 100);

对于第二个。

于 2012-10-03T11:28:51.713 回答
0

布局中是否有可能有 2 个圆圈,但您只能看到一个,因为它与第一个圆圈的颜色相同,并且它们彼此重叠?

改变circleRed.paint.setColor(Color.WHITE);circleRed.paint.setColor(Color.BLACK); 看看它给出了什么

于 2012-10-03T11:30:36.527 回答