본문 바로가기

안드로이드 Android

[모바일프로그래밍] 7-3 이미지 처리

package com.example.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static class MyCustomView extends View {
        public MyCustomView(Context myContext) {
            super(myContext);
        }

        protected void onDraw(Canvas myCanvas) {
            super.onDraw(myCanvas);
            Bitmap myPicture = BitmapFactory.decodeResource(getResources(), R.drawable.cats);

            int centerX = this.getWidth() / 2;
            int centerY = this.getHeight() / 2;
            int pic_x = (this.getWidth() - myPicture.getWidth()) / 2;
            int pic_y = (this.getHeight() - myPicture.getWidth()) / 2;

            myCanvas.drawBitmap(myPicture, pic_x, pic_y, null);

            myCanvas.translate(-150, 200);
            myCanvas.drawBitmap(myPicture, pic_x, pic_y, null);

            myCanvas.rotate(45, centerX, centerY);
            myCanvas.drawBitmap(myPicture, pic_x, pic_y, null);

            myCanvas.scale(0.5f, 2, centerX, centerY);
            myCanvas.drawBitmap(myPicture, pic_x, pic_y, null);

            myCanvas.scale(0.4f, 0.4f);
            myCanvas.drawBitmap(myPicture, pic_x, pic_y, null);
            myPicture.recycle();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyCustomView(this));
    }
}