저는 Android에서 현재 캔버스를 배우고 있으며 아래 유형의 그림을 만들고 싶습니다. 나는 그것을 검색했지만 적절한 예를 얻을 수 없습니다.
누구든지 나를 도울 수 있습니까? 어떻게 만들 수 있습니까?
먼저이 코드를 참조하십시오 .
public class CustomView extends View {
public static final int NO_OF_VERTICAL_CIRCLES = 5;
public static final int NO_OF_HORIZONTAL_CIRCLES = 4;
public static final float RADIUS = 60f;
private Paint mPaintImage;
private RectF[] rectangles = new RectF[NO_OF_HORIZONTAL_CIRCLES
* NO_OF_VERTICAL_CIRCLES];
// Just creating a view for drawing
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// This is paint for your drawing
mPaintImage = new Paint();
mPaintImage.setAntiAlias(true);
mPaintImage.setFilterBitmap(true);
mPaintImage.setDither(true);
// color for circle
mPaintImage.setColor(Color.RED);
// fill style
mPaintImage.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// Account for padding
float xpad = (float) (getPaddingLeft() + getPaddingRight());
float ypad = (float) (getPaddingTop() + getPaddingBottom());
// getting area for drawing you can also get it using getWidth() and getHeight()
float ww = (float) w - xpad;
float hh = (float) h - ypad;
Creating Rectangle for drawing your circles
for (short i = 0; i < NO_OF_HORIZONTAL_CIRCLES; i++) {
for (short j = 0; j < NO_OF_VERTICAL_CIRCLES; j++) {
// calculating horizontal centers
float horizontalCenter = RADIUS + i * (ww - RADIUS * 2)
/ (NO_OF_HORIZONTAL_CIRCLES - 1);
// calculating vertical centers
float verticalCenter = RADIUS + j * (hh - RADIUS * 2)
/ (NO_OF_VERTICAL_CIRCLES - 1);
rectangles[j + NO_OF_VERTICAL_CIRCLES * i] = new RectF(
horizontalCenter - RADIUS, verticalCenter - RADIUS,
horizontalCenter + RADIUS, verticalCenter + RADIUS);
}
}
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
// Here drawing circles
for (RectF rect : rectangles) {
canvas.drawCircle(rect.centerX(), rect.centerY(), RADIUS,
mPaintImage);
}
super.onDraw(canvas);
}
여기서 내가 한 일은 그리드에서 sreen을 나눈 다음 중심을 찾아 그에 따라 원을 그리는 것입니다.
비슷한 방법으로 비트 맵, 직사각형을 그릴 수 있습니다.
빈 비트 맵에 그릴 수도 있습니다.
도움이 되길 바랍니다.
PS (스크린 샷) ::
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다