带有负值的android cliprect不会更改父视图rect

时间:2012-04-11 08:00:44

标签: android layout view

我有一个子视图,我希望画布能够绘制一个具有"像素"在轴的负侧

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

为了做到这一点,我使用clipRect,这工作正常,我看到完整的cirlce。 但是,当我在父视图上使用requestLayout并更改子视图的x,y位置时 没有显示圆的负sids 请告诉我该怎么做

这是代码:

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


    }

1 个答案:

答案 0 :(得分: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);


        }


    }