0

我有一个子视图,我希望画布在轴的负侧绘制一个具有“像素”的圆圈

    canvas.drawCircle(0, 0, 50f, paint);

为了做到这一点,我使用clipRect,这很好用,我看到了完整的圆圈。但是,当我在父视图上使用 requestLayout 并更改子视图的 x、y 位置时,未显示圆圈的负 sid 请告诉我该怎么做

这是代码:

    package natan.android.TestCanvas;

    import android.app.Activity;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;

    public class TestCanvasActivity extends Activity {

        View childView;
        RelativeLayout parentView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            childView = new View(this)
            {

                @Override
                protected void onDraw(Canvas canvas) {
                    canvas.clipRect(-50, -50, 50, 50,android.graphics.Region.Op.REPLACE);
                    Paint paint = new Paint();
                    paint.setStyle(Paint.Style.FILL);
                    paint.setColor(Color.RED);
                    canvas.drawCircle(0, 0, 50f, paint);
                    super.onDraw(canvas);
                }

            };

            parentView = new RelativeLayout(this);
            parentView.addView(childView);
            LayoutParams layoutParms = (LayoutParams)(childView.getLayoutParams());
            layoutParms.leftMargin=150;
            layoutParms.topMargin=150;
            setContentView(parentView);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            LayoutParams layoutParms = (LayoutParams)(childView.getLayoutParams());
            layoutParms.leftMargin=100;
            layoutParms.topMargin=100;
            parentView.requestLayout();
            return super.onTouchEvent(event);
        }


    }
4

1 回答 1

0

我想我想出了一个我从这里拿走的灵魂

http://www.devdaily.com/java/jwarehouse/android/core/java/android/view/ViewGroup.java.shtml

使用这行代码解决了问题

   parentView.setClipChildren(false);

   parentView.requestLayout();
   parentView.invalidate();

希望这对其他人有帮助,如果有人对此问题有更多了解,请添加

这是完整的代码:

    public class TestCanvasActivity extends Activity {

        View childView;
        RelativeLayout parentView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            childView = new View(this)
            {

                @Override
                protected void onDraw(Canvas canvas) {
                                        Paint paint = new Paint();
                    paint.setStyle(Paint.Style.FILL);
                    paint.setColor(Color.RED);
                    canvas.drawCircle(0, 0, 50f, paint);
                    super.onDraw(canvas);
                }

            };






            parentView = new RelativeLayout(this)
            {

                @Override
                protected void onLayout(boolean changed, int l, int t,
                        int r, int b) {
                    // TODO Auto-generated method stub
                    super.onLayout(changed, l, t, r, b);
                }

            };
            parentView.setClipChildren(false);

            parentView.addView(childView);
            parentView.setClipChildren(false);
            LayoutParams layoutParms = (LayoutParams)(childView.getLayoutParams());
            layoutParms.leftMargin=150;
            layoutParms.topMargin=150;

            setContentView(parentView);

        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            LayoutParams layoutParms = (LayoutParams)(childView.getLayoutParams());
            layoutParms.leftMargin=100;
            layoutParms.topMargin=100;

            parentView.requestLayout();
            parentView.invalidate();

            return super.onTouchEvent(event);


        }


    }
于 2012-04-14T10:06:20.600 回答