I have created a square in android studio, my code for it is below. I don't know how to center it. at the moment I have it starting from an x position of 20 and a y position of 20. How do I make it so that it is centered, do I just have to keep messing around with the coordinates or is there a way to do it exactly?
package com.example.cms0369.somethingelse;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class Draw extends View {
Paint paint = new Paint();
//float startX;
//float startY;
//float stopX;
//float stopY;
public Draw(Context context){
super(context);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas){
canvas.drawLine(20, 20, 200, 20, paint);
canvas.drawLine(20, 20, 20, 200, paint);
canvas.drawLine(20, 200, 200, 200, paint);
canvas.drawLine(200, 200, 200, 20, paint);
}
}
You can try something like this:
int rectSide = ...;
@Override
public void onDraw(Canvas canvas){
int width = canvas.getWidth();
int height = canvas.getHeight();
canvas.drawRect(width/2f - rectSide/2f,
height/2f - rectSide/2f,
width/2f + rectSide/2f,
height/2f + rectSide/2f, paint);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다