无法弄清楚如何获得相对布局的中心
Cant figure out how to get center of relativelayout
在过去的 2 小时里,我一直在为此苦苦挣扎,但我就是找不到答案。我想要 relativelayout 的中心,因为我想用 canvas 在那里画一个圆。我已经尝试过的:
- ViewTreeObserver.OnGlobalLayoutListener
- OnStart()
这是我的代码:
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
Bitmap bg = Bitmap.createBitmap(480, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bg);
rel = (RelativeLayout) findViewById(R.id.relPaint);
//canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(centreX, centreY, 50, paint);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rel.setBackgroundDrawable(new BitmapDrawable(getResources(), bg));
} else {
rel.setBackground(new BitmapDrawable(getResources(), bg));
}
}
@Override
protected void onStart() {
centreX = rel.getX() + rel.getWidth() / 2;
centreY = rel.getY() + rel.getHeight() / 2;
super.onStart();
}
变量 rel、centreX 和 centreY 是全局声明的。谢谢
看看Activity Lifecycle。 onCreate()
在 onStart()
之前调用。您首先绘制圆圈,然后计算布局的中心。在 OnCreate()
.
中完成所有操作
然后像这样计算 centreX
和 centreY
。
rel.post(new Runnable() {
@Override
public void run() {
centreX = rel.getWidth() / 2;
centreY = rel.getHeight() / 2;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
Bitmap bg = Bitmap.createBitmap(480, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bg);
rel = (RelativeLayout) findViewById(R.id.relPaint);
//canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(centreX, centreY, 50, paint);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rel.setBackgroundDrawable(new BitmapDrawable(getResources(), bg));
} else {
rel.setBackground(new BitmapDrawable(getResources(), bg));
}
}
});
在过去的 2 小时里,我一直在为此苦苦挣扎,但我就是找不到答案。我想要 relativelayout 的中心,因为我想用 canvas 在那里画一个圆。我已经尝试过的:
- ViewTreeObserver.OnGlobalLayoutListener
- OnStart()
这是我的代码:
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
Bitmap bg = Bitmap.createBitmap(480, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bg);
rel = (RelativeLayout) findViewById(R.id.relPaint);
//canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(centreX, centreY, 50, paint);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rel.setBackgroundDrawable(new BitmapDrawable(getResources(), bg));
} else {
rel.setBackground(new BitmapDrawable(getResources(), bg));
}
}
@Override
protected void onStart() {
centreX = rel.getX() + rel.getWidth() / 2;
centreY = rel.getY() + rel.getHeight() / 2;
super.onStart();
}
变量 rel、centreX 和 centreY 是全局声明的。谢谢
看看Activity Lifecycle。 onCreate()
在 onStart()
之前调用。您首先绘制圆圈,然后计算布局的中心。在 OnCreate()
.
然后像这样计算 centreX
和 centreY
。
rel.post(new Runnable() {
@Override
public void run() {
centreX = rel.getWidth() / 2;
centreY = rel.getHeight() / 2;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
Bitmap bg = Bitmap.createBitmap(480, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bg);
rel = (RelativeLayout) findViewById(R.id.relPaint);
//canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(centreX, centreY, 50, paint);
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rel.setBackgroundDrawable(new BitmapDrawable(getResources(), bg));
} else {
rel.setBackground(new BitmapDrawable(getResources(), bg));
}
}
});