我有一个子视图,我希望画布在轴的负侧绘制一个具有“像素”的圆圈
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);
}
}