본문 바로가기

안드로이드 Android

[모바일프로그래밍] 7-1 안드로이드에서의 그래픽

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;

class MyCustomView extends View {
    public MyCustomView(Context myContext) {
        super(myContext);
        setBackgroundColor(Color.LTGRAY);
    }

    @Override
    protected void onDraw(Canvas myCanvas) {
        Paint myPaint = new Paint();
        myPaint.setColor(Color.BLUE);
        myPaint.setStrokeWidth(20);
        myCanvas.drawText("자동차입니다", 100, 100, myPaint);

        myPaint.setColor(Color.YELLOW);
        myCanvas.drawRect(100, 200, 600, 400, myPaint);

        myPaint.setColor(Color.RED);
        myCanvas.drawCircle(200, 470, 50, myPaint);

        myPaint.setColor(Color.BLUE);
        myCanvas.drawLine(200, 250, 500, 350, myPaint);

        Path myPath = new Path();
        Paint my2Paint = new Paint();

        my2Paint.setStyle(Paint.Style.STROKE);

        myPath.moveTo(100, 600); // 위치만 옮김
        myPath.lineTo(200, 700); // 직선을 그리면서 움직임
        myPath.cubicTo(300, 600, 400, 800, 500, 700); // 현재의 위치를 포함하여 총 네 개의 좌표값으로 곡석을 그림
        myPath.lineTo(400, 1000);

        my2Paint.setColor(Color.RED);
        my2Paint.setStrokeWidth(30);

        myCanvas.drawPath(myPath, my2Paint);

        my2Paint.setStyle(Paint.Style.FILL);
        my2Paint.setTextSize(40);
        myCanvas.drawTextOnPath("이번에는 그래픽 작업과 함께 글자를 패스를 따라 기술하는 예입니다.",
                myPath, 0, 0, my2Paint);
    }
}

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MyCustomView myView = new MyCustomView(this);
        setContentView(myView);
    }
}